473,326 Members | 2,095 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,326 software developers and data experts.

Events, Reflection, and Anonymous Methods

I am trying to subscribe to an event using reflection. The guarantee is
that the the event delegate will be of type EventHandler or at least the e
parameter inherits from EventArgs. Hard coding this is possible, for
example in the example below I am subscribing TestMethod to Event and the
EventArgs parameter differs. But I need to do this same thing through
reflection using an anonymous method. It seems like it should be possible,
but I can't find a way. Is this possible?

I think the core problem is that this doesn't compile:

EventHandler<EventArgs2testDelegate2 = delegate(Object sender,
EventArgs blah) { };

However, it compiles fine when not using an anonymous method.
using System;
using System.Reflection;

class EventArgs2 : EventArgs { }

class Program
{
public static event EventHandler<EventArgs2Event;

public static void TestMethod(Object sender, EventArgs blah)
{
return;
}

public static void Main(
String[] args)
{
EventInfo eventInfo = typeof(Program).GetEvent("Event");

// Compiles fine even though the TestMethod parameters do
// not match /exactly/, though they do match logically.
EventHandler<EventArgs2testDelegate1 = new
EventHandler<EventArgs2>(TestMethod);

// These work fine
eventInfo.AddEventHandler(null, testDelegate2);
Event += testDelegate2;

// Try doing this with an anonymous method.
EventHandler testDelegate2 = delegate(Object sender, EventArgs blah)
{
return;
};

// Throws an exception because the delegate types don't match.
eventInfo.AddEventHandler(null, testDelegate2);

// Does not compile because the delegate parameters do
// not match /exactly/, though they do match logically.
Event += testDelegate2;

return;
}
}


May 7 '07 #1
0 1246

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

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
7
by: George Carl | last post by:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet I'd like to make publish/subscribe scenario where the publishing object is...
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...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
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...
5
by: puzzlecracker | last post by:
I took a closer look at delegates and events, and it became apparent that events don't offer any extra functionalities over delegates. I don't even see it as syntactical sugar over "overtly...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.