Why does EventInfo.RemoveEventHandler throw a NullReferenceException?

I have some code that uses reflection to install .NET event handlers on MSHTML objects in Internet Explorer BHO, and it seems to work fine. However, I am having problems when I try to remove event handlers.

Here is the code for installing and removing the event handler:

public class HandlerExample {
    private static void Handler(IHTMLEventObj e) { ... }
    private static Delegate handlerDelegate;

    public static void InstallHandler(IHTMLElement target, string eventName)
    {
        // FindInterface() finds the correct event interface for the particular subclass of
        // IHTMLElement that target really is
        Type eventInterface = FindInterface(target);
        EventInfo eInfo = eventInterface.GetEvent(eventName);
        Type tHandler = eInfo.EventHandlerType;

        handlerDelegate = Delegate.CreateDelegate(tHandler, typeof(HandlerExample), "Handler");

        eInfo.AddEventHandler(target, handlerDelegate);
    }

    public static void RemoveHandler(IHTMLElement target, string eventName)
    {
        Type eventInterface = FindInterface(target);
        EventInfo eInfo = eventInterface.GetEvent(eventName);
        eInfo.RemoveEventHandler(target, handlerDelegate); // THIS LINE CRASHES
    }
}

The call InstallEventHandlerworks fine, and Handlerthen is called when the event fires in the browser. When I call RemoveEventHandlerwith the same arguments as the call InstallEventHandler, the last line throws TargetInvocationExceptionwith an internal exception NullReferenceException. I can't figure out what I'm doing wrong here, and stack tracing doesn't help much.

EDIT: , , , .

AddEventHandler RemoveEventHandler InstallHandler, .

:

System.Reflection.TargetInvocationException occurred
  Message="Exception has been thrown by the target of an invocation."
  Source="mscorlib"
  StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.EventInfo.RemoveEventHandler(Object target, Delegate handler)
       at RemoveHandler(IHTMLElement target, String eventName)
  InnerException: System.NullReferenceException
       Message="Object reference not set to an instance of an object."
       Source="Microsoft.mshtml"
       StackTrace:
            at mshtml.HTMLTextContainerEvents2_EventProvider.remove_onclick(HTMLTextContainerEvents2_onclickEventHandler )
            at mshtml.HTMLTextAreaElementClass.HTMLTextContainerEvents2_Event_remove_onclick(HTMLTextContainerEvents2_onclickEventHandler )
       InnerException: 

?

+3
1

, , handlerDelegate null, RemoveHandler. , , handlerDelegate RemoveEventHandler?

0

All Articles