How do I create a generic .Net event handler
-
I am attempting to write code that sinks an event with only an object reference and the event name - no type information. I write a generic event handler function: public void ComponentEventHandler(object o, EventArgs e) { // handle generic event } Then I wrote some code that attaches any event to this event handler function protected void SinkControlEvent(Control ctrl, string eventName) { EventDescriptor event = TypeDescriptor.GetEvents(ctrl)[eventName]; Delegate del = Delegate.CreateDelegate(typeof(System.EventHandler), this, "ComponentEventHandler"); event .AddEventHandler(ctrl, del); } This works fine when the event I'm sinking has the standard function signature. However, I get an exception when the event has a derived EventArgs parameter. For example, trying to sink the following event would cause a type mismatch exception: public event void ValueChanged(object sender, ValueEventArgs e); The exception is "Invalid event handler for ValueChanged event". Of course 'ValueEventArgs' inherits from EventArgs, so I don't understand why this isn't a valid handler. Some things to note: 1. You can, in code, assign an untyped event handler to a typed event. Somehow the compiler figures it out. 2. I tried changing my code such that I attach an untyped function to a typed delegate, but that also threw an exception. Delegate del = Delegate.CreateDelegate(event.EventType, this, "ComponentEventHandler"); Thanks, Aaron Stibich
-
I am attempting to write code that sinks an event with only an object reference and the event name - no type information. I write a generic event handler function: public void ComponentEventHandler(object o, EventArgs e) { // handle generic event } Then I wrote some code that attaches any event to this event handler function protected void SinkControlEvent(Control ctrl, string eventName) { EventDescriptor event = TypeDescriptor.GetEvents(ctrl)[eventName]; Delegate del = Delegate.CreateDelegate(typeof(System.EventHandler), this, "ComponentEventHandler"); event .AddEventHandler(ctrl, del); } This works fine when the event I'm sinking has the standard function signature. However, I get an exception when the event has a derived EventArgs parameter. For example, trying to sink the following event would cause a type mismatch exception: public event void ValueChanged(object sender, ValueEventArgs e); The exception is "Invalid event handler for ValueChanged event". Of course 'ValueEventArgs' inherits from EventArgs, so I don't understand why this isn't a valid handler. Some things to note: 1. You can, in code, assign an untyped event handler to a typed event. Somehow the compiler figures it out. 2. I tried changing my code such that I attach an untyped function to a typed delegate, but that also threw an exception. Delegate del = Delegate.CreateDelegate(event.EventType, this, "ComponentEventHandler"); Thanks, Aaron Stibich
astibich2 wrote:
public void ComponentEventHandler(object o, EventArgs e)
Could you not change this to
public void ComponentEventHandler<T>(object sender, T e) where T : EventArgs
Deja View - the feeling that you've seen this post before.
-
astibich2 wrote:
public void ComponentEventHandler(object o, EventArgs e)
Could you not change this to
public void ComponentEventHandler<T>(object sender, T e) where T : EventArgs
Deja View - the feeling that you've seen this post before.
That looks a lot like the EventHandler< T> class ;)
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
That looks a lot like the EventHandler< T> class ;)
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.Well, I thought so, but I can't see what he's trying to do that you can't accomplish normally.
Deja View - the feeling that you've seen this post before.