ManagedToNativeComInteropStubAttribute 類別

定義

在 Managed 至 COM Interop 案例中,提供使用者自訂 Interop Stub 的支援。

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 Attribute
Public NotInheritable Class ManagedToNativeComInteropStubAttribute
Inherits Attribute
繼承
ManagedToNativeComInteropStubAttribute
屬性

備註

此屬性可讓具有封送處理和 Interop 呼叫內部運作知識的 Interop 開發人員執行下列動作:

  • 在建置階段利用自訂 Interop 存根,而不是執行時間。

  • 偵錯自訂 Interop 存根。

  • 提供執行時間未提供之存根內的封送處理功能。

  • 提供使用者特定的方法分派功能。

您可以在介面中的方法上套用 屬性,以指定 Managed 到原生 COM Interop 的對應存根方法。

如果在執行時間期間遇到這個屬性,Common Language Runtime 不會動態產生 Interop 存根。 相反地,它會叫用在建置時間建立的自訂存根。

屬性 ManagedToNativeComInteropStubAttribute 具有下列特性:

  • 屬性只能在標示 [ComImport] 的介面上使用。 如果屬性套用至非介面類別型,執行時間就會忽略它。

  • 屬性只能在介面中的相同方法上使用一次。 如果使用多次,編譯器會產生重複的屬性錯誤。

  • 屬性無法繼承自基底介面。 衍生介面必須明確指派 屬性。

  • 包含屬性方法的元件也必須包含自訂存根。

多載存根方法有效。 雖然您只指定 stub 方法的類型和名稱,但執行時間會探索對應的存根。 其做法是檢查介面方法上的所有引數,然後使用明確 this 指標來執行完整簽章比對。

介面中的多個方法也可以共用相同的存根方法;不過,當您使用共用存根時,應該小心。

注意

存根方法必須是靜態的。

您可以通知執行時間在建置時間使用自訂 Interop Stub,而不是使用下列 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

取得包含所需 Stub 方法的類別。

MethodName

取得 Stub 方法的名稱。

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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於