I need to get all events from the current class, and find out the methods that subscribe to it. Here I got some answers on how to do that, but I don’t know how I can get the delegate when all I have is the EventInfo.
var events = GetType().GetEvents();
foreach (var e in events)
{
Delegate d = e./*GetDelegateFromThisEventInfo()*/;
var methods = d.GetInvocationList();
}
Is it possible to get a delegate with the EventInfo? How?
The statement
var events = GetType().GetEvents();gets you a list ofEventInfoobjects associated with the current type, not the current instance per se. So theEventInfoobject doesn’t contain information about the current instance and hence it doesn’t know about the wired-up delegates.To get the info you want you need to get the backing field for the event handler on your current instance. Here’s how:
So now your calling code can look like this:
The output from the above is:
I’m sure you can jig around with the code if you wish to return the delegate rather than the method info, etc.
I hope this helps.