1

Background: I'm using the standard WebBrowser control that is part of System.Windows.Forms which is actually a wrapper over the Internet Explorer COM control, in order to use functionality not exposed by the wrapper I'm using the ActiveXInstance property getter and casting it to the underlying COM type (or actually the COM interop class that Visual Studio automatically creates for you when referencing a COM type ~ Interop.ShDocVw.dll).

Now in order to avoid shipping the entire Interop.ShDocVw.dll I'm trying to compile only the salient sections of code for what I want to achieve, so I'm looking at a decompiled Interop.ShDocVw.dll and trying to replicate only those sections of code I need. The odd thing is that the code as decompiled by ILSpy doesn't actually compile, specifically these event accessors fail to compile:

[DefaultMember("Name"), ClassInterface((short)0), ComSourceInterfaces("SHDocVw.DWebBrowserEvents2\0SHDocVw.DWebBrowserEvents\0"), Guid("8856F961-340A-11D0-A96B-00C04FD705A2"), TypeLibType(34)]
[ComImport]
public class WebBrowserClass : IWebBrowser2, WebBrowser
{
    [MethodImpl(MethodImplOptions.InternalCall)]
    public extern WebBrowserClass();

    public virtual extern event DWebBrowserEvents2_NewWindow2EventHandler NewWindow2
    {
        [MethodImpl(MethodImplOptions.InternalCall)]
        add;
        [MethodImpl(MethodImplOptions.InternalCall)]
        remove;
    }

    [DispId(210)]
    public virtual extern string LocationName
    {
        [DispId(210)]
        [MethodImpl(MethodImplOptions.InternalCall)]
        [return: MarshalAs(UnmanagedType.BStr)]
        get;
    }
}

Despite the event accessor being marked as extern the compiler reports "An add or remove accessor must have a body". If I add an empty body them the comiler reports:

SHDocVw.WebBrowserClass.NewWindow2.add' cannot be extern and declare a body

So what is the correct way to attach an event to a COM implementation?

4

1 回答 1

1

如果您不使用访问器声明会怎样?

public virtual extern event DWebBrowserEvents2_NewWindow2EventHandler NewWindow2;
于 2013-07-15T15:34:12.523 回答