lundi 1 mai 2017

GetField unable to obtain EventClick

I am trying to determine the method that Click event is subscribed to, in my form, and I follow the guide here.

Whereas the forum post above able to get the list that the Click event is subscribing to by following code

 hasClickEventHandler = HasEventHandler(buttonControl, "EventClick");
    Assert.AreEqual(hasClickEventHandler, true);


    private bool HasEventHandler(Control control, string eventName)
    {
        EventHandlerList events =
            (EventHandlerList)
            typeof(Component)
             .GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance)
             .GetValue(control, null);

        object key = typeof(Control)
            .GetField(eventName, BindingFlags.NonPublic | BindingFlags.Static)
            .GetValue(null);

        Delegate handlers = events[key];

        return handlers != null && handlers.GetInvocationList().Any();
    }

I can't.

I then use var keys = typeof(Control).GetFields(BindingFlags.NonPublic | BindingFlags.Static); to check, and I found that one of the Keys does seem to have the correct event name. ie,

keys[19].FullName=="System.Windows.Forms.Control.EventClick";
keys[19].Name=="EventClick";

So there is no reason why

object key = typeof(Control).GetField("EventClick", BindingFlags.NonPublic | BindingFlags.Static)

Returns null, and yet this is exactly what happen

Why is this so? What could have gone wrong?





Aucun commentaire:

Enregistrer un commentaire