How to get a call list of any event

How to get a delegate list form event of a control in WPF.

I tried the following code, but it will return the field information as null

TextBox cont = new TextBox();
cont.TextChanged += new TextChangedEventHandler(cont_TextChanged);
FieldInfo fi = cont.GetType().GetField("TextChanged", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
Delegate del = (Delegate)fi.GetValue(cont);
+5
source share
2 answers

I don’t know why people say this is impossible:

Suppose you want to disable any temporary event, you can create a method like this:

static Delegate[] DisableEvents(this Control ctrl, string eventName)
{
        PropertyInfo propertyInfo = ctrl.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
        EventHandlerList eventHandlerList = propertyInfo.GetValue(ctrl, new object[] { }) as EventHandlerList;
        FieldInfo fieldInfo = typeof(Control).GetField("Event"+eventName, BindingFlags.NonPublic | BindingFlags.Static);

        object eventKey = fieldInfo.GetValue(ctrl);
        var eventHandler = eventHandlerList[eventKey] as Delegate;
        Delegate[] invocationList = eventHandler.GetInvocationList();
        foreach (EventHandler item in invocationList)
        {
            ctrl.GetType().GetEvent(eventName).RemoveEventHandler(ctrl, item);
        }
        return invocationList;

}|

You can call it like this:

var events = textbox1.DisableEvents("GotFocus")

If you want to add them again, you just need to view the list of events.

+5
source

, , . , , , , . Dictionary. : ( #)

0

All Articles