ManagedToNativeComInteropStubAttribute 类


为用户在托管与 COM 互操作方案中自定义互操作存根提供支持。Provides support for user customization of interop stubs in managed-to-COM interop scenarios.

public ref class ManagedToNativeComInteropStubAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class ManagedToNativeComInteropStubAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class ManagedToNativeComInteropStubAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class ManagedToNativeComInteropStubAttribute : Attribute
type ManagedToNativeComInteropStubAttribute = class
    inherit Attribute
Public NotInheritable Class ManagedToNativeComInteropStubAttribute
Inherits Attribute


此特性允许具有有关封送的知识的互操作开发人员和互操作调用的内部工作原理来执行以下操作:This attribute enables interop developers who have knowledge about marshaling and the inner workings of interop calls to do the following:

  • 在生成时(而非运行时)利用自定义的互操作存根。Take advantage of customized interop stubs at build time instead of run time.

  • 调试自定义的互操作存根。Debug customized interop stubs.

  • 在运行时不提供的存根内提供封送处理功能。Provide marshaling capabilities within a stub that the runtime does not provide.

  • 提供用户特定的方法调度功能。Provide user-specific method dispatch capabilities.

对接口中的方法应用属性,以指定托管到本机 COM 互操作的相应存根方法。You apply the attribute on a method in an interface to specify a corresponding stub method for managed-to-native COM interop.

如果在运行时遇到此属性,则公共语言运行时不会动态生成互操作存根。If this attribute is encountered during run time, the common language runtime does not dynamically generate an interop stub. 相反,它调用生成时创建的自定义存根。Instead, it invokes the custom stub that was created at build time.

ManagedToNativeComInteropStubAttribute 特性具有以下特征:The ManagedToNativeComInteropStubAttribute attribute has the following characteristics:

  • 特性仅可用于标记为 [ComImport]的接口中的方法。The attribute can be used only on methods from interfaces that are marked [ComImport]. 如果该特性应用于非接口类型,则运行时将忽略该特性。If the attribute is applied to non-interface types, it is ignored by the runtime.

  • 特性只能在接口中的同一方法上使用一次。The attribute can be used only once on the same method in an interface. 如果多次使用该方法,则编译器会生成重复的属性错误。If it is used more than once, the compiler generates a duplicated attribute error.

  • 该特性不能从基接口继承。The attribute is not inheritable from a base interface. 派生接口必须显式分配属性。Derived interfaces must explicitly assign the attribute.

  • 包含特性化方法的程序集还必须包含自定义的存根。The assembly that contains the attributed method must also contain the customized stub.

重载的存根方法是有效的。Overloaded stub methods are valid. 尽管仅指定存根方法的类型和名称,但运行时将发现相应的存根。Although you specify only the type and the name of the stub method, the runtime will discover the corresponding stub. 它通过检查接口方法上的所有参数,然后使用显式 this 指针执行完全签名匹配,来实现此功能。It does this by examining all the arguments on the interface method, and then performing full signature matching by using an explicit this pointer.

接口中的多个方法也可能共享相同的存根(stub)方法;但是,当你使用共享存根时,应小心。It is also possible for multiple methods in an interface to share the same stub method; however, you should be careful when you use shared stubs.


存根方法必须是静态的。Stub methods must be static.

可以通知运行时在生成时使用自定义的互操作存根,而不是在运行时C#使用以下代码:You could inform the runtime to use a customized interop stub at build time instead of run time with the following C# code:

interface IMyInterface  
    void GetString (string arg);  

然后,可以使用以下代码声明相应的存根方法:You could then use the following code to declare the corresponding stub method:

class TestStubClass  
    internal static void ForwardTestStub(IMyInterface thisObject,  
             string arg) {…}  


ManagedToNativeComInteropStubAttribute(Type, String)

使用指定的类类型和方法名称初始化 ManagedToNativeComInteropStubAttribute 类的新实例。Initializes a new instance of the ManagedToNativeComInteropStubAttribute class with the specified class type and method name.



获取包含所需存根方法的类。Gets the class that contains the required stub method.


获取存根方法的名称。Gets the name of the stub method.


在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)



返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)


_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)