简体   繁体   English

MethodImplAttribute(InternalCall,Runtime)对COM Interop接口的方法做了什么?

[英]What does MethodImplAttribute(InternalCall, Runtime) do for methods of COM Interop interfaces?

In Windows API Code Pack for .NET Framework , many methods of COM Interop interfaces are decorated with MethodImplAttribute , for example: Windows API Code Pack for .NET Framework中 ,COM Interop接口的许多方法都使用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);

Documentation for MethodImplOptions.InternalCall value says: MethodImplOptions.InternalCall值的文档说:

The call is internal, that is, it calls a method that is implemented within the common language runtime. 调用是内部的,也就是说,它调用在公共语言运行库中实现的方法。

Documentation for MethodCodeType.Runtime says: MethodCodeType.Runtime文档说:

Specifies that the method implementation is provided by the runtime. 指定方法实现由运行时提供。

But, if I understand correctly, neither statement is correct. 但是,如果我理解正确,这两种说法都是正确的。 IShellItem is implemented in shell32.dll , according to MSDN. 根据MSDN, IShellItemshell32.dll中实现 shell32.dll is not a part of CLR. shell32.dll不是CLR的一部分。 What's interesting, not all methods have the MethodImplAttribute . 有趣的是,并非所有方法都有MethodImplAttribute IShellFolder does, IShellLinkW doesn't, IShellLibrary does, IPersistStream doesn't etc. IShellFolder没有, IShellLinkW没有, IShellLibrary没有, IPersistStream没有等等。

Why is MethodImplAttribute applied to some of the COM Interop interfaces? 为什么MethodImplAttribute应用于某些COM Interop接口? What does it mean in this case and how does it modify behavior of interop? 在这种情况下它意味着什么?它如何修改互操作的行为?

It is just a side-effect from the way the Microsoft programmer obtained the interface declaration. 这只是微软程序员获得接口声明方式的副作用。 Some of the shell interfaces are available in a type library, like IShellItem, so the easiest way to get them into source code is to run Tlbimp.exe to generate the interop library and a disassembler to decompile the assembly into C# code. 一些shell接口在类型库中可用,例如IShellItem,因此将它们放入源代码的最简单方法是运行Tlbimp.exe以生成互操作库和反汇编程序以将程序集反编译为C#代码。 Which also brings in the extra attributes that Tlbimp.exe generates, like [MethodImpl]. 这也带来了Tlbimp.exe生成的额外属性,如[MethodImpl]。

Other interfaces are not available in a type library, like IShellLinkW and IPersistStream, nor can a type library be generated from the IDL so the programmer had no choice but to type them in himself. 其他接口在类型库中不可用,如IShellLinkW和IPersistStream,也不能从IDL生成类型库,因此程序员别无选择,只能在自己中键入它们。 He skipped the unnecessary stuff. 他跳过了不必要的东西。

And no, [MethodImpl] isn't critical here. 不,[MethodImpl]在这里并不重要。 These are interface types so there is no method anyway. 这些是接口类型,所以无论如何都没有方法。 Tlbimp.exe just isn't terribly sophisticated about it. Tlbimp.exe对它来说并不是非常复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM