PC Review


Reply
Thread Tools Rate Thread

Connecting to events via Reflection

 
 
dmcdougald@openspan.com
Guest
Posts: n/a
 
      26th Apr 2007
I posted a month or so ago and never got a response. So I'm trying
again.

I just moved over to C#2. In 1.1 I implemented a generic event
handler strategy by emitting a handler of the correct signature type.
However, with Covariance/Contravariance in C#2 I hoped to clean
things
up.

Since this works in 2.0:

button1.Click += GenericEventHandler; // Just a normal EventHandler
button1.MouseDown += GenericEventHandler; // a MouseEventHandler


private void GenericEventHandler(object sender, EventArgs args)
{
}


I had hoped to be able to do something similar to the above code via
Reflection. So I tried this:

eventInfo.AddEventHandler(instance, new
EventHandler(GenericEventHandler);

But if the eventInfo.EventHandlerType is not a basic EventHandler I
receive the following error:

Object of type 'System.EventHandler' cannot be converted to type
'System.Windows.Forms.MouseEventHandler'.

Is there any way to make this happen?

Thanks

 
Reply With Quote
 
 
 
 
Barry Kelly
Guest
Posts: n/a
 
      26th Apr 2007
(E-Mail Removed) wrote:

> I just moved over to C#2. In 1.1 I implemented a generic event
> handler strategy by emitting a handler of the correct signature type.
> However, with Covariance/Contravariance in C#2 I hoped to clean
> things
> up.
>
> Since this works in 2.0:
>
> button1.Click += GenericEventHandler; // Just a normal EventHandler
> button1.MouseDown += GenericEventHandler; // a MouseEventHandler
>
> private void GenericEventHandler(object sender, EventArgs args)


You've got the location of the variance wrong in your mental model. The
variance is located at the point of binding the method to the delegate,
not the point of attaching the delegate to the event.

The above code is equivalent to:

button1.MouseDown += new MouseEventHandler(GenericEventHandler);

> I had hoped to be able to do something similar to the above code via
> Reflection. So I tried this:
>
> eventInfo.AddEventHandler(instance, new
> EventHandler(GenericEventHandler);


So, you need to create a MouseEventHandler rather than an EventHandler,
but GenericEventHandler should fit into MouseEventHandler's constructor.

You can use Delegate.CreateDelegate to make it more general, of course,
and possibly EventInfo to discover the correct delegate type.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to connect to events generically via Reflection dmcdougald@openspan.com Microsoft C# .NET 2 20th Feb 2007 02:54 PM
Firing Events using reflection mswlogo Microsoft C# .NET 2 22nd Nov 2006 05:20 PM
Reflection Vs Events - Performance =?Utf-8?B?SEw=?= Microsoft C# .NET 3 9th Dec 2005 08:20 AM
Use reflection to remove and add events niteshs@yahoo.com Microsoft C# .NET 2 8th Oct 2005 08:30 AM
Dynamically firing events using reflection. Tim Haughton Microsoft C# .NET 4 3rd Jun 2004 05:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:13 PM.