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
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