0

Im trying to get all registered events of a standard control (in this example: a Button), aka all events that have self defined code behind it. So basically the same list you would see in the designer, without the "empty" events. In my case I want the events of a simple Button. The button has an "on-click" event and none other.

I tried countless methods i found on this site and scattered around the web but none of them seem to work for me. Most recently i tried this:

foreach (var e in typeof(Button).GetEvents()) //typeof(Button) just to make sure there is no error here
{
    FieldInfo fi = c.GetType().GetField(e.Name, BindingFlags.Public | BindingFlags.NonPublic |
                     BindingFlags.Static | BindingFlags.Instance |
                     BindingFlags.DeclaredOnly);
    if (fi != null)
    {
        object value = fi.GetValue(c);
        if (value == null)
        {
            message += e.Name;
        } 
    }
}

Given "c" is a normal Button with an "on_Click" event, "message" is a string.

Am I over-looking something? Is it not possible to get all registered events of a Control anyway? What am I doing wrong? Id greatly appreciate some help/pointers.

dcastro
  • 66,540
  • 21
  • 145
  • 155
Moonpaw
  • 55
  • 8
  • 1
    Possible duplicate: http://stackoverflow.com/questions/136975/has-an-event-handler-already-been-added – Paul Zahra Feb 24 '14 at 10:06
  • Events are tied to an instance, not a type. So first thing would be to get the information from a specific instance. – Johann Blais Feb 24 '14 at 10:13
  • Possible duplicate: http://stackoverflow.com/questions/1129517/c-sharp-how-to-find-if-an-event-is-hooked-up – Johann Blais Feb 24 '14 at 10:14
  • Will check them out and see if they are helpful. – Moonpaw Feb 24 '14 at 10:15
  • 1
    @JohannBlais I *think* he wants to list all the available events for a class rather than all the listeners for a specific event. But I could be wrong! – Matthew Watson Feb 24 '14 at 10:40
  • Doesnt seem to work a bit for me, sadly. I want all the events that are registered for a control that i click. not all available events that are available for the control in general. Only those that have a listener, and the name of the event if it has one. – Moonpaw Feb 24 '14 at 10:56
  • It is not supposed to work. The .NET Framework makes it extra difficult to get this information. For a very good reason, programs mis-behave undiagnosably once you start messing with this. You are otherwise making the standard mistake of not explaining why you think you need this. – Hans Passant Feb 24 '14 at 13:04

0 Answers0