PreserveSigAttribute 類別


指示應該要抑制 COM Interop 呼叫期間發生的 HRESULT 或 retval 簽章轉換。Indicates that the HRESULT or retval signature transformation that takes place during COM interop calls should be suppressed.

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


下列範例顯示在將元件匯出至 COM 類型C#程式庫時,tlbexp.exe 如何轉換方法,而不 PreserveSigAttributeThe following example shows how Tlbexp.exe converts a C# method without PreserveSigAttribute when exporting an assembly to a COM type library.

受控簽章:Managed signature:

int DoSomething (long l);  

非受控簽章:Unmanaged signature:

HRESULT DoSomething ([in] long l, [out, retval] int * i);  

當您將 PreserveSigAttribute 套用至相同C#的方法並匯出元件時,方法轉換與先前的範例不同。When you apply PreserveSigAttribute to the same C# method and export the assembly, the method conversion differs from the previous example. 請注意,Tlbexp.exe 會移除 HRESULT 和 [out,retval] 參數修飾詞。Notice that Tlbexp.exe removes the HRESULT and the [out, retval] parameter modifier.

受控簽章:Managed signature:

[PreserveSig] int DoSomething (long l);  

非受控簽章:Unmanaged signature:

int DoSomething ([in] long l);  


您可以將此屬性套用至方法。You can apply this attribute to methods.

根據預設, tlbexp.exe (類型程式庫匯出工具)可確保傳回 S_OK 的 HRESULT 的呼叫會進行轉換,讓 [out,retval] 參數當做函數傳回值使用。By default, the Tlbexp.exe (Type Library Exporter) ensures that a call that returns an HRESULT of S_OK is transformed such that the [out, retval] parameter is used as the function return value. S_OK HRESULT 會被捨棄。The S_OK HRESULT is discarded. 對於 S_OK 以外的 Hresult,執行時間會擲回例外狀況,並捨棄 [out,retval] 參數。For HRESULTs other than S_OK, the runtime throws an exception and discards the [out, retval] parameter. 當您將 PreserveSigAttribute 套用至 managed 方法簽章時,屬性化方法的 managed 和非受控簽章都相同。When you apply the PreserveSigAttribute to a managed method signature, the managed and unmanaged signatures of the attributed method are identical.

如果成員傳回一個以上的成功 HRESULT 值,而您想要偵測不同的值,則必須保留原始方法簽章。Preserving the original method signature is necessary if the member returns more than one success HRESULT value and you want to detect the different values. 因為大部分的 COM 成員會藉由套用 PreserveSigAttribute來傳回 HRESULT,所以您可以取出代表成功或失敗 HRESULT 的整數。Since most COM member return an HRESULT, by applying the PreserveSigAttribute, you can retrieve an integer representing the success or failure HRESULT. Tlbexp.exe 會將任何 [out,retval] 參數保留為 managed 簽章中的 out 參數。Tlbexp.exe preserves any [out, retval] parameters as out parameters in the managed signature.

Tlbimp.exe (類型程式庫匯入工具)也會套用此屬性;它會在匯入類型程式庫時,將屬性套用至介面。The Tlbimp.exe (Type Library Importer) also applies this attribute; it applies the attribute to dispinterfaces when it imports a type library.


當 COM 對 managed 程式碼進行交互操作時,PreserveSigAttribute 類別不支援傳回類型的 CurrencyGuidObject,而且 managed 程式碼會以 PreserveSigAttribute 類別標記。Return types of Currency, Guid, and Object are not supported by the PreserveSigAttribute class when interoperation occurs from COM to managed code and the managed code is marked with the PreserveSigAttribute class. 當您嘗試在這些情況下使用其中一個傳回類型搭配 PreserveSigAttribute 類別時,會擲回 TypeLoadExceptionWhen you attempt to use one of these return types with the PreserveSigAttribute class during these conditions, a TypeLoadException is thrown.



初始化 PreserveSigAttribute 類別的新執行個體。Initializes a new instance of the PreserveSigAttribute class.



在衍生類別中實作時,取得這個 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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)