PreserveSigAttribute 类

定义

指示在 COM 互操作调用期间发生的 HRESULT 签名转换应被禁止。

public ref class PreserveSigAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class PreserveSigAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class PreserveSigAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type PreserveSigAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type PreserveSigAttribute = class
    inherit Attribute
Public NotInheritable Class PreserveSigAttribute
Inherits Attribute
继承
PreserveSigAttribute
属性

示例

以下示例演示在将程序集导出到 COM 类型库时,Tlbexp.exe如何在不 PreserveSigAttribute 转换 C# 方法的情况下。

托管签名:

int DoSomething (long l);  

非托管签名:

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

应用于 PreserveSigAttribute 同一 C# 方法并导出程序集时,方法转换与前面的示例不同。 请注意,Tlbexp.exe删除 HRESULT 和 [out, retval] 参数修饰符。

托管签名:

[PreserveSig] int DoSomething (long l);  

非托管签名:

int DoSomething ([in] long l);  

注解

可以将此属性应用于方法。

默认情况下, Tlbexp.exe (类型库导出程序) 确保转换返回 S_OK HRESULT 的调用,以便将 [out, retval] 参数用作函数返回值。 放弃S_OK HRESULT。 对于除 S_OK 以外的 HRESULT,运行时会引发异常并放弃 [out, retval] 参数。 将 应用于 PreserveSigAttribute 托管方法签名时,特性化方法的托管签名和非托管签名是相同的。

如果成员返回多个成功 HRESULT 值,并且你想要检测不同的值,则必须保留原始方法签名。 由于大多数 COM 成员返回 HRESULT,因此通过应用 PreserveSigAttribute,可以检索表示成功或失败 HRESULT 的整数。 Tlbexp.exe将任何 [out, retval] 参数保留为托管签名中的 out 参数。

Tlbimp.exe (类型库导入程序) 也应用此属性;它在导入类型库时将 特性应用于 dispinterface。

备注

当从 COM 到托管代码进行互操作并且托管代码使用 类标记PreserveSigAttribute时, 类不支持 PreserveSigAttribute 、 和 Object 的返回类型CurrencyGuid。 在这些条件下,尝试将其中一个返回类型与 类一起使用 PreserveSigAttribute 时, TypeLoadException 将引发 。

构造函数

PreserveSigAttribute()

初始化 PreserveSigAttribute 类的新实例。

属性

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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅