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
属性

示例

下面的示例演示如何 Tlbexp.exe 将转换的 C# 方法而无需PreserveSigAttribute到 COM 类型库导出程序集时。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. 有关 Hresult,则为 S_OK 以外,在运行时引发异常,并放弃 [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 成员返回一个 HRESULT,通过应用PreserveSigAttribute,可以检索一个整数,表示成功或失败的 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.

备注

返回类型的CurrencyGuid,并Object不受PreserveSigAttribute类时执行从 COM 交互操作向托管代码和托管的代码被标记为与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类在这些条件,期间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)

适用于

另请参阅