PreserveSigAttribute PreserveSigAttribute PreserveSigAttribute PreserveSigAttribute Class

定义

指示应取消在 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)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class PreserveSigAttribute : Attribute
type PreserveSigAttribute = class
    inherit Attribute
Public NotInheritable Class PreserveSigAttribute
Inherits Attribute
继承
PreserveSigAttributePreserveSigAttributePreserveSigAttributePreserveSigAttribute
属性

示例

下面的示例演示如何在将程序集导出C#到 COM 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 Currency Guid Object 到托管PreserveSigAttribute代码进行互操作, 并且托管代码标记为类时,类不支持、和的返回类型。PreserveSigAttributeReturn 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 TypeLoadException下, 当你尝试在类中使用这些返回类型之一时, 将引发。When you attempt to use one of these return types with the PreserveSigAttribute class during these conditions, a TypeLoadException is thrown.

构造函数

PreserveSigAttribute() PreserveSigAttribute() PreserveSigAttribute() PreserveSigAttribute()

初始化 PreserveSigAttribute 类的新实例。Initializes a new instance of the PreserveSigAttribute class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

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

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

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

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)

显式界面实现

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

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

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

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

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

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

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(Inherited from Attribute)

属性

TypeId TypeId TypeId TypeId

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

(Inherited from Attribute)

适用于

另请参阅