I am using reflection to add an event handler to an event :
var eventInfo = type.GetEvent(eventName);
MethodInfo mi = GetType().GetMethod("TestMethod",
BindingFlags.Instance | BindingFlags.NonPublic);
var delegateForMethod = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, mi);
eventInfo.AddEventHandler(this, delegateForMethod);
This successfully calls my test method when the event occurs which is great, but now I need to know the name of the event that ended up calling this method...
void TestMethod(object sender, EventArgs e)
{
// I know the sender, but which event was fired on the sender?
}
The reason I need this is because I have this generic register method which hooks up handlers to different types, and different events and channels them all to one method, while also making a note of what was attached. Once the test method fires, I need to pull out that note and use the info to notify the correct object that their "desired" event has fired. -- but to know this I need to know the event name as well as the type.
For example, in register I added Event A in type X for object O.... now when I see it triggered in the test method, I need to know it was Event A in type X, so I can notify object O by a certain interface method on it.
There is no problem that canot be solved by introducing an additional level of abstraction!
Bind event like this:
or by reflection:
And in handler you can access event name from parameter:
Crazy thinking here.
To every event you are going to add
TestMethod
,before TestMethod
you add ananonimous method
, wich I will callSetEventName
for understanding purposes.That
SetEventName
just sets someGlobalEventName
variable in your class takingeventName
. (I suppose there's a way to use a lambda expression for that).So, every time the event is fired.
SetEventName
is called. YourGlobalEventName
is set. ThenTestMethod
is called. And insideTestMethod
, use theGlobalEventName
to do your job.See an idea (sorry, I don't understand reflection quite well to put a ready-to-use code, but that's the idea)
And your testmethod: