ManagedToNativeComInteropStubAttribute 类     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为用户在托管与 COM 互操作方案中自定义互操作存根提供支持。
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)]
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class ManagedToNativeComInteropStubAttribute : Attribute[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type ManagedToNativeComInteropStubAttribute = class
    inherit Attribute[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type ManagedToNativeComInteropStubAttribute = class
    inherit Attribute[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
type ManagedToNativeComInteropStubAttribute = class
    inherit AttributePublic NotInheritable Class ManagedToNativeComInteropStubAttribute
Inherits Attribute- 继承
- 属性
注解
此属性使了解封送处理和互操作调用内部工作的互操作开发人员能够执行以下操作:
- 在生成时(而不是运行时)利用自定义互操作存根。 
- 调试自定义互操作存根。 
- 在运行时不提供的存根内提供封送处理功能。 
- 提供特定于用户的方法调度功能。 
将 特性应用于接口中的方法,以便为托管到本机 COM 互操作指定相应的存根方法。
如果在运行时遇到此属性,公共语言运行时不会动态生成互操作存根。 相反,它会调用在生成时创建的自定义存根。
属性 ManagedToNativeComInteropStubAttribute 具有以下特征:
- 特性只能用于标记为 - [ComImport]的接口中的方法。 如果属性应用于非接口类型,运行时将忽略它。
- 特性只能在接口中的同一方法上使用一次。 如果多次使用它,编译器将生成重复的属性错误。 
- 属性不可从基接口继承。 派生接口必须显式分配 特性。 
- 包含特性化方法的程序集还必须包含自定义存根。 
重载存根方法有效。 尽管仅指定存根方法的类型和名称,但运行时将发现相应的存根。 它通过检查接口方法上的所有参数,然后使用显式 this 指针执行完整签名匹配来执行此操作。
接口中的多个方法也可以共享相同的存根方法;但是,在使用共享存根时应小心。
注意
存根方法必须是静态的。
可以使用以下 C# 代码通知运行时在生成时而不是运行时使用自定义互操作存根:
[ComImport]
interface IMyInterface
{
    [ManagedToNativeComInteropStubAttribute(typeof(TestStubClass),
              "ForwardTestStub")]
    void GetString (string arg);
}
然后,可以使用以下代码声明相应的存根方法:
class TestStubClass
{
    internal static void ForwardTestStub(IMyInterface thisObject,
             string arg) {…}
}
构造函数
| ManagedToNativeComInteropStubAttribute(Type, String) | 使用指定的类类型和方法名称初始化 ManagedToNativeComInteropStubAttribute 类的新实例。 | 
属性
| ClassType | 获取包含所需存根方法的类。 | 
| MethodName | 获取存根方法的名称。 | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 返回一个值,该值指示此实例是否与指定的对象相等。(继承自 Attribute) | 
| GetHashCode() | 返回此实例的哈希代码。(继承自 Attribute) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |