MarshalAsAttribute 类

定义

指示如何在托管代码与非托管代码之间封送数据。

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

示例

以下示例将 应用于 MarshalAsAttribute 托管类型中的字段、方法参数和方法返回值。

using System;
using System.Text;
using System.Runtime.InteropServices;

class Program
{

//Applied to a parameter.
  public void M1([MarshalAs(UnmanagedType.LPWStr)]String msg) {}

//Applied to a field within a class.
  class MsgText {
                [MarshalAs(UnmanagedType.LPWStr)]
                public String msg = "Hello World";
                }

//Applied to a return value.
[return: MarshalAs(UnmanagedType.LPWStr)]
    public String GetMessage()
    {
        return "Hello World";
    }

static void Main(string[] args)
    {  }
}
Imports System.Runtime.InteropServices

Module Module1

    Sub Main()

    End Sub

    'Applied to a parameter.
    Public Sub M1(<MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal msg As String)
        msg = msg + "Goodbye"
    End Sub

    'Applied to a field within a class.
    Class MsgText
        <MarshalAsAttribute(UnmanagedType.LPWStr)> Public msg As String
    End Class

    'Applied to a return value.
    Public Function M2() As <MarshalAsAttribute(UnmanagedType.LPWStr)> String
        Return "Hello World"
    End Function

End Module

以下示例将 MarshalAsAttribute 属性应用于属性:

decimal _money;   

public decimal Money 
{
   [return: MarshalAs(UnmanagedType.Currency)]
   get { return this._money; }
   [param: MarshalAs(UnmanagedType.Currency)]
   set { this._money = value; }
}
Dim _money As Decimal   

Public Property Money As <MarshalAs(UnmanagedType.Currency)> Decimal 
   Get
      Return Me._money
   End Get
   Set(<MarshalAs(UnmanagedType.Currency)> value As Decimal)
      Me._money = value
   End Set   
End Property

注解

可以将此属性应用于参数、字段或返回值。

此属性是可选的,因为每种数据类型都有默认的封送处理行为。 仅当给定类型可以封送为多个类型时,才需要此属性。 例如,你可以将字符串封送为非托管代码,如作为 LPStrLPWStrLPTStrBStr。 默认情况下,公共语言运行时将字符串参数作为 BStr 封送为 COM 方法。 您可以将 MarshalAsAttribute 特性应用于单个字段或参数, 以使该特定字符串作为 LPStr 而不是 BStr 封送。 Tlbexp.exe (类型库导出程序) 将封送处理首选项传递给公共语言运行时。

与 COM 互操作或平台调用一起使用时,某些参数和返回值具有不同的默认封送处理行为。 默认情况下,运行时将值类型 (和字段的字符串参数封送为 LPStr 平台调用方法或函数) 。 有关详细信息,请参阅 默认封送处理行为

在大多数情况下, 属性只是使用 UnmanagedType 枚举标识非托管数据的格式,如以下 C# 签名所示:

public void SomeMethod([MarshalAs(UnmanagedType.LPStr)] String s)
Public Sub SomeMethod(<MarshalAs(UnmanagedType.LPStr)> s As String)

某些 UnmanagedType 枚举成员需要其他信息。 例如,当 为 LPArrayUnmanagedType,需要其他信息。 有关如何将此属性用于数组的完整说明,请参阅 数组的默认封送处理

Tlbimp.exe (类型库导入程序) 还会将此属性应用于参数、字段和返回值,以指示输入类型库中的数据类型不是相应托管数据类型的默认类型。 为清楚起见,Tlbimp.exe始终将 String 应用于 MarshalAsAttributeObject 类型,而不考虑输入类型库中指定的类型。

注意

MarshalAsAttribute不支持泛型类型的封送处理。

构造函数

MarshalAsAttribute(Int16)

使用指定的 UnmanagedType 值初始化 MarshalAsAttribute 类的新实例。

MarshalAsAttribute(UnmanagedType)

使用指定的 UnmanagedType 枚举成员初始化 MarshalAsAttribute 类的新实例。

字段

ArraySubType

指定非托管 LPArrayByValArray 的元素类型。

IidParameterIndex

指定 COM 使用的非托管 iid_is 特性的参数索引。

MarshalCookie

向自定义封送处理程序提供附加信息。

MarshalType

指定自定义封送处理程序的完全限定名。

MarshalTypeRef

MarshalType 作为类型实现。

SafeArraySubType

指示 SafeArray 的元素类型。

SafeArrayUserDefinedSubType

指示用户定义的 SafeArray 元素类型。

SizeConst

指示固定长度数组中的元素数,或要导入的字符串中的字符(不是字节)数。

SizeParamIndex

指示从零开始的参数,该参数包含数组元素的计数,与 COM 中的 size_is 类似。

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)
Value

获取 UnmanagedType 值,数据将被作为该值封送。

方法

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)

适用于

另请参阅