BHO exposing javascript method works in IE 9+ but

2019-05-21 14:49发布

问题:

I'm making a BHO that exposes method to JavaScript.

It works okey in IE 9 and IE 10, but fails in IE 8 with RuntimeBinderException: "mshtml.HTMLWindow2Class" does not contain "signJson".

Code is mostly based on live reload IE extention.

Here is a way that function is injected into window:

    public void InjectScriptResource(dynamic window)
    {
        var windowEx = (IExpando)window;

        if (windowEx.GetProperty("signJson", BindingFlags.Default) == null)
        {
            windowEx.AddProperty("signJson");
            window.signJson = this;
        }
    }

What's different about about mshtml.HTMLWindow2Class in IE 8 from IE 9? What is a proper way to inject method into it?

回答1:

Found an answer on Stack Overflow. You just have to change code to this:

public void InjectScriptResource(dynamic window)
{
    var windowEx = (IExpando)window;

    if (windowEx.GetProperty("signJson", BindingFlags.Default) == null)
    {
        // windowEx.AddProperty("signJson");
        PropertyInfo p = windowEx.AddProperty("signJson");
        // window.signJson = this;
        p.SetValue(windowEx, this);
    }
}