Dynamic event binding

G

Gerard

Hi,

I'm looking for any info on binding dynamic events in C# for CF. I need
to create a delegate at runtime that associates

an unknown runtime event with a handler. In the standard framework, I
used the following to bind the event to the

'ProcessEvent" operation,

EventInfo eventInfo = value;
object EventPublisher = someEventSource;
MethodInfo mi = typeof(this).GetMethod("ProcessEvent",
BindingFlags.NonPublic | BindingFlags.Instance);
Delegate eventDelegate =
Delegate.CreateDelegate(eventInfo.EventHandlerType, this, mi);
eventInfo.AddEventHandler(eventPublisher, eventDelegate);


This works fine, however, Delegate.CreateDelegate is not supported in
CF, so I tried this alternative :-

EventHandler eHT = new EventHandler(this.ProcessEvent);
eventInfo.AddEventHandler(eventPublisher, eHT);


The problem with this is that it only works with System.EventHandler.
In other words, if I try and use this to tie up

an event which uses a subclass EventHandler :-
i.e.,

public delegate void ContinueEventHandler(object sender,
ContinueEventArgs eventArgs);
public virtual event ContinueEventHandler Continue;

instead of
public event EventHandler Fired; //works


then I get a runtime exception :-

ArgumentException
Object of type "System.EventHandler" cannot be converted to type
ContinueEventHandler


Is there an alternative mechanism to acheive what I'm trying to do?

thanks
 
G

Gerard

Thanks for this Sergey. Unfortunately I tried the alternative approach
and I think it falls into the same problem - I get the same
ArgumentException as I described above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top