Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a delegate object from an EventInfo?

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?

like image 206
BrunoLM Avatar asked Sep 23 '10 23:09

BrunoLM


2 Answers

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.

like image 116
Enigmativity Avatar answered Oct 12 '22 12:10

Enigmativity


For my case, the field value (class ToolStripMenuItem, field EventClick) frustratingly is of type object, not delegate. I had to resort to the Events property that Les mentioned in his answer, in a way I got from here. The field EventClick in this case only holds the key to the EventHandlerList stored in this property.

Some other remarks:

  • The rule fieldName = "Event" + eventName I employed will not work in every case. Unfortunately there is no common rule for linking the event name to the field name. You might try to use a static dictionary for the mapping, similar to this article.
  • I'm never quite sure about the BindingFlags. In another scenario, you might have to adjust them.

    /// <summary>
    /// Gets the EventHandler delegate attached to the specified event and object
    /// </summary>
    /// <param name="obj">object that contains the event</param>
    /// <param name="eventName">name of the event, e.g. "Click"</param>
    public static Delegate GetEventHandler(object obj, string eventName)
    {
        Delegate retDelegate = null;
        FieldInfo fi = obj.GetType().GetField("Event" + eventName, 
                                               BindingFlags.NonPublic | 
                                               BindingFlags.Static |
                                               BindingFlags.Instance | 
                                               BindingFlags.FlattenHierarchy |
                                               BindingFlags.IgnoreCase);
        if (fi != null)
        {
            object value = fi.GetValue(obj);
            if (value is Delegate)
                retDelegate = (Delegate)value;
            else if (value != null) // value may be just object
            {
                PropertyInfo pi = obj.GetType().GetProperty("Events",
                                               BindingFlags.NonPublic |
                                               BindingFlags.Instance);
                if (pi != null)
                {
                    EventHandlerList eventHandlers = pi.GetValue(obj) as EventHandlerList;
                    if (eventHandlers != null)
                    {
                        retDelegate = eventHandlers[value];
                    }
                }
            }
        }
        return retDelegate;
    }
    
like image 29
Mike Fuchs Avatar answered Oct 12 '22 12:10

Mike Fuchs