Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MethodImplAttribute(InternalCall,Runtime)对COM互操作接口的方法有什么作用?_C#_.net_Com_Com Interop - Fatal编程技术网

C# MethodImplAttribute(InternalCall,Runtime)对COM互操作接口的方法有什么作用?

C# MethodImplAttribute(InternalCall,Runtime)对COM互操作接口的方法有什么作用?,c#,.net,com,com-interop,C#,.net,Com,Com Interop,在中,COM互操作接口的许多方法都用MethodImplAttribute修饰,例如: internal interface IShellItem { [PreserveSig] [MethodImpl( MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] HResult BindToHandler( [In] IntPtr pb

在中,COM互操作接口的许多方法都用
MethodImplAttribute
修饰,例如:

internal interface IShellItem
{
    [PreserveSig]
    [MethodImpl(
        MethodImplOptions.InternalCall,
        MethodCodeType = MethodCodeType.Runtime)]
    HResult BindToHandler(
        [In] IntPtr pbc, [In] ref Guid bhid, [In] ref Guid riid,
        [Out, MarshalAs(UnmanagedType.Interface)] out IShellFolder ppv);
价值文档说明:

该调用是内部的,也就是说,它调用在公共语言运行库中实现的方法

文件说明:

指定方法实现由运行时提供

但是,如果我理解正确,这两种说法都不正确,根据MSDN。shell32.dll不是CLR的一部分。有趣的是,并非所有方法都具有
methodimpl属性
<代码>IShellFolder执行,
IShellLinkW
执行,
IShellLibrary
执行,
IPersistStream
执行,等等


为什么将
MethodImplAttribute
应用于某些COM互操作接口?在这种情况下,它意味着什么?它是如何修改互操作行为的?

这只是Microsoft程序员获取接口声明方式的副作用。一些shell接口在类型库中可用,如IShellItem,因此将它们放入源代码的最简单方法是运行Tlbimp.exe以生成互操作库,并使用反汇编程序将程序集反编译为C#代码。这也带来了Tlbimp.exe生成的额外属性,如[MethodImpl]

其他接口在类型库中不可用,如IShellLinkW和IPersistStream,也不能从IDL生成类型库,因此程序员别无选择,只能自己键入它们。他跳过了不必要的东西

不,[MethodImpl]在这里并不重要。这些是接口类型,因此无论如何都没有方法。Tlbimp.exe只是在这方面不够成熟