Created
December 11, 2019 17:39
-
-
Save amitkhare/a4365eba6db696c8e4c4db57b0ee3ebb to your computer and use it in GitHub Desktop.
C# Remove all handlers from any event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void RemoveEvent(object item, string eventStr = "DoubleClick") | |
{ | |
FieldInfo f1 = typeof(Control).GetField("Event"+ eventStr, | |
BindingFlags.Static | BindingFlags.NonPublic); | |
object obj = f1.GetValue(item); | |
PropertyInfo pi = item.GetType().GetProperty("Events", | |
BindingFlags.NonPublic | BindingFlags.Instance); | |
EventHandlerList list = (EventHandlerList)pi.GetValue(item, null); | |
list.RemoveHandler(obj, list[obj]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE