I have a custom control (MenuButton) in WPF that subscribes to a Click event. I now want to use reflection to unsubscribe from the event. This is what my code currently looks like:
private void RemoveClickEvent(MenuButton mbButton)
{
FieldInfo fiFieldInfo = mbButton.GetType().GetField("Click", BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance |BindingFlags.Static | BindingFlags.GetField);
object objValue = fiFieldInfo.GetValue(mbButton);
PropertyInfo piProprtyInfo = mbButton.GetType().GetProperty("Events", BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static | BindingFlags.GetField | BindingFlags.GetProperty);
EventHandlerList ehEventHandlers = (EventHandlerList)piProprtyInfo.GetValue(mbButton, null);
ehEventHandlers.RemoveHandler(objValue, ehEventHandlers[objValue]);
}
I can successfully get the FieldInfo object, but the PropertyInfo object comes out as null. How do I fix my code so that the method works properly? For the record, I am targeting .NET Framework 4.
Aucun commentaire:
Enregistrer un commentaire