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 th
Similarly to Enigmativity, the invocation list can be found for other classes, not just the current class...
private void testit()
{
WithEvents we = new WithEvents();
we.myEvent += new EventHandler(we_myEvent);
we.myEvent += new EventHandler(we_myEvent2);
foreach (EventInfo ev in we.GetType().GetEvents())
{
FieldInfo fi = we.GetType().GetField(ev.Name, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
Delegate del = (Delegate)fi.GetValue(we);
var list = del.GetInvocationList();
foreach (var d in list)
{
Console.WriteLine("{0}", d.Method.Name);
}
}
}
void we_myEvent(object sender, EventArgs e)
{
}
void we_myEvent2(object sender, EventArgs e)
{
}
public class WithEvents
{
public event EventHandler myEvent;
}
... as long as the event handlers are declared in the class as we see above. But consider the Control class where the EventHandlerList is stored in the "Events" property and each event field name begins with "Event" followed by the event name. Then there is the Form derived classes that seem to manage events yet differently. Food for thought.