PreserveSigAttribute 类


指示应取消在 COM 互操作调用期间发生的 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


下面的示例演示在将程序集导出到C# COM 类型库时,tlbexp.exe 如何转换不 PreserveSigAttribute 的方法。The 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 应用于托管方法签名时,特性化方法的托管和非托管签名是相同的。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] 参数保留为托管签名中的 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 到托管代码进行互操作并使用 PreserveSigAttribute 类标记托管代码时,PreserveSigAttribute 类不支持返回类型的 CurrencyGuidObjectReturn 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)

返回一个表示当前对象的 string。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)