views:

34

answers:

2

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?

+1  A: 

The statement var events = GetType().GetEvents(); gets you a list of EventInfo objects associated with the current type, not the current instance per se. So the EventInfo object 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:

public class MyClass
{
    public event EventHandler MyEvent;

    public IEnumerable<MethodInfo> GetSubscribedMethods()
    {
        Func<EventInfo, FieldInfo> ei2fi =
            ei => this.GetType().GetField(ei.Name,
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.GetField);

        return from eventInfo in this.GetType().GetEvents()
               let eventFieldInfo = ei2fi(eventInfo)
               let eventFieldValue =
                   (System.Delegate)eventFieldInfo.GetValue(this)
               from subscribedDelegate in eventFieldValue.GetInvocationList()
               select subscribedDelegate.Method;
    }
}

So now your calling code can look like this:

class GetSubscribedMethodsExample
{
    public static void Execute()
    {
        var instance = new MyClass();
        instance.MyEvent += new EventHandler(MyHandler);
        instance.MyEvent += (s, e) => { };

        instance.GetSubscribedMethods()
            .Run(h => Console.WriteLine(h.Name));
    }

    static void MyHandler(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

The output from the above is:

MyHandler
<Execute>b__0

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.

Enigmativity
Thank you. That was very helpful.
BrunoLM
A: 

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.

Les