当前位置: 代码迷 >> C# >> 接口如何还能重写?IOleCommandTarget是mshtmlDocument实现的接口吗
  详细解决方案

接口如何还能重写?IOleCommandTarget是mshtmlDocument实现的接口吗

热度:45   发布时间:2016-05-05 04:24:35.0
接口怎么还能重写?IOleCommandTarget是mshtmlDocument实现的接口吗?
近日在读一开源项目,对一些代码很困惑.....
直接给代码吧,请看红字注释的疑问:
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var htmlDocument = wb.Document;//wb为WebBrowser
    if (!htmlDocument.IsNull())
    {
document = (mshtmlDocument)htmlDocument.DomDocument;
_doc = (mshtmlIHTMLDocument2)htmlDocument.DomDocument;

    }
    int hResult;
    // try to obtain the command target for the web browser document
    try
    {
// cast the document to a command target
var target = (IOleCommandTarget)document;//下面代码显示,IOleCommandTarget是一重新定义或者自定义的接口,还能给document补上一自定义接口吗,此处语法我感到很困惑,哪位大侠能解释下吗?,还有IOleCommandTarget的用途是什么?
// set the appropriate no url fixups on paste
hResult = target.Exec(ref CommandGroup.CGID_MSHTML, (int)CommandId.IDM_NOFIXUPURLSONPASTE, (int)CommandOption.OLECMDEXECOPT_DONTPROMPTUSER, ref EMPTY_PARAMETER, ref EMPTY_PARAMETER);
    }
    // catch any exception and map back to the HRESULT
    catch (Exception ex)
    {
hResult = Marshal.GetHRForException(ex);
    }
}


 #region COM Interop Interface Definitions

    // NOTE
    // The implementations uses the PreserveSig attribute to prevent exception throwing
    // thus allowing the caller to analyze the HRESULT.
    // To revert to exception throwing remove the attribue and change the return type to void 

    /// <summary>
    /// IOleCommandTarget interface 
    /// </summary>
    [ComVisible(true),  ComImport(), Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown) ]//这些特征的意义我也不懂,请也顺便帮忙解释下吧
    internal interface IOleCommandTarget//IOleCommandTarget是重定义的还是作者自创的?
    {
        [PreserveSig]
        int QueryStatus(
            ref Guid pguidCmdGroup,
            uint cCmds,
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] OLECMD prgCmds,
            ref IntPtr pCmdText);

        [PreserveSig]
        int Exec(
            ref Guid pguidCmdGroup,
            uint nCmdID,
            uint nCmdExecOpt,
            ref object pvaIn,
            ref object pvaOut);

    } //IOleCommandTarget
------解决思路----------------------
引用
var target = (IOleCommandTarget)document;//下面代码显示,IOleCommandTarget是一重新定义或者自定义的接口,还能给document补上一自定义接口吗,此处语法我感到很困惑,哪位大侠能解释下吗?,还有IOleCommandTarget的用途是什么?


还能给document补上一自定义接口吗?
答:能啊。可以写其他的接口,让Document 继承并实现它就可以了。

IOleCommandTarget的用途是什么?
答:是告诉编译器,你要调用这个接口的方法了。 如果不这么写的话,无法调用target对象的Exec方法。
就好比,超人会飞,但是他要是不告诉别人他是超人,别人是不知道他会飞的。

引用
InterfaceType(ComInterfaceType.InterfaceIsIUnknown) ]//这些特征的意义我也不懂,请也顺便帮忙解释下吧


答:那就当这是一个固定的语法,就要求这么写。

引用
nternal interface IOleCommandTarget//IOleCommandTarget是重定义的还是作者自创的?

答:不知道你说的重定义是什么意思?接口没有这个概念。这个应该是作者自己写的接口。

------解决思路----------------------
这哪里是自定义的接口, 这是将 COM 接口用C#语法包装而已.
  相关解决方案