I found a method here on stack overflow that strips all event handlers from a given event. However, when I copied the code into my program it gave me a “Reference not set to an instance” error.
Here is the code in question:
FieldInfo f1 = typeof(Control).GetField("DocumentCompleted",
BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(browser);
PropertyInfo pi = browser.GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(browser, null);
list.RemoveHandler(obj, list[obj]);
The error happened on this line:
object obj = f1.GetValue(browser);
The browser object is an instance of WebBrowser, and it has been instantized at this point.
Edit: Came up with a different way of removing the handlers.
DocumentCompleted is a public event, not a private static field, so f1 is null and cause your error