473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Connecting to events via Reflection

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

Apr 26 '07 #1
1 2466
dm********@openspan.com 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/
Apr 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sgt. Sausage | last post by:
NOTE: I'm new to c#, but not to programming. For reasons unimportant to the discussion below, I need to be able to dynamically, at run time, discover the set of all objects listening to another...
9
by: Erik Frey | last post by:
Hi there, Just curious as to whether there's a clever way to see the events a control/object is firing off, perhaps written out to the debug console. It would be really handy to know which...
3
by: VJ | last post by:
I can't find my posting that had replies to events at runtime.. hence I am posting as new. I did earlier read all your replies. Thanks for the information. I still have not tried them. My...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
4
by: ABC | last post by:
I want to check the form's controls have or not the specific properties or events. How to determine or gather the properties list under the run-time environment?
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: mswlogo | last post by:
I looked high and low for code to do this and finally found some VB code that did it right. This is a C# flavor of it. public event EventHandler<EventArgsMyEventToBeFired; public void...
6
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and...
0
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.