MarshalAsAttribute 类

定义

指示如何在托管代码与非托管代码之间封送数据。Indicates how to marshal the data between managed and unmanaged code.

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

示例

下面的示例将应用MarshalAsAttribute于托管类型中的字段、方法参数和方法返回值。The following example applies the MarshalAsAttribute to a field, a method parameter, and a method return value in a managed type.

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属性应用于属性:The following example applies the MarshalAsAttribute attribute to a property:

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

注解

可以将此特性应用于参数、字段或返回值。You can apply this attribute to parameters, fields, or return values.

此属性是可选的, 因为每个数据类型都有默认的封送处理行为。This attribute is optional, as each data type has a default marshaling behavior. 仅当给定类型可以封送到多种类型时, 此属性才是必需的。This attribute is only necessary when a given type can be marshaled to multiple types. 例如,你可以将字符串封送为非托管代码,如作为 LPStrLPWStrLPTStrBStrFor example, you can marshal a string to unmanaged code as either a LPStr, a LPWStr, a LPTStr, or a BStr. 默认情况下,公共语言运行时将字符串参数作为 BStr 封送为 COM 方法。By default, the common language runtime marshals a string parameter as a BStr to COM methods. 您可以将 MarshalAsAttribute 特性应用于单个字段或参数, 以使该特定字符串作为 LPStr 而不是 BStr 封送。You can apply the MarshalAsAttribute attribute to an individual field or parameter to cause that particular string to be marshaled as a LPStr instead of a BStr. Tlbexp.exe (类型库导出程序)将封送首选项传递到公共语言运行时。The Tlbexp.exe (Type Library Exporter) passes your marshaling preferences to the common language runtime.

与 COM 互操作或平台调用一起使用时, 某些参数和返回值具有不同的默认封送处理行为。Some parameters and return values have different default marshaling behavior when used with COM interop or platform invoke. 默认情况下, 运行时将字符串参数 (和值类型中的字段) LPStr封送为平台调用方法或函数。By default, the runtime marshals a string parameter (and fields in a value type) as a LPStr to a platform invoke method or function. 有关其他信息, 请参阅默认封送行为For additional information, see Default Marshaling Behavior.

在大多数情况下, 属性只使用UnmanagedType枚举标识非托管数据的格式, 如以下C#签名所示:In most cases, the attribute simply identifies the format of the unmanaged data using the UnmanagedType enumeration, as shown in the following C# signature:

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

某些UnmanagedType枚举成员需要额外的信息。Some UnmanagedType enumeration members require additional information. 例如, 当UnmanagedTypeLPArray时, 需要其他信息。For example, additional information is needed when the UnmanagedType is LPArray. 有关如何将此属性与数组一起使用的完整说明, 请参阅数组的默认封送处理For a complete description of how to use this attribute with arrays, see Default Marshaling for Arrays.

Tlbimp.exe (类型库导入程序)还将此特性应用于参数、字段和返回值, 以指示输入类型库中的数据类型不是对应的托管数据类型的默认类型。The Tlbimp.exe (Type Library Importer) also applies this attribute to parameters, fields, and return values to indicate that the data type in the input type library is not the default type for the corresponding managed data type. 无论在输入类型库中MarshalAsAttribute指定StringObject类型如何, tlbimp.exe 始终将应用于和类型。Tlbimp.exe always applies the MarshalAsAttribute to String and Object types for clarity, regardless of the type specified in the input type library.

备注

MarshalAsAttribute不支持对泛型类型进行封送处理。The MarshalAsAttribute does not support marshaling of generic types.

构造函数

MarshalAsAttribute(Int16)

使用指定的 UnmanagedType 值初始化 MarshalAsAttribute 类的新实例。Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.

MarshalAsAttribute(UnmanagedType)

使用指定的 UnmanagedType 枚举成员初始化 MarshalAsAttribute 类的新实例。Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType enumeration member.

字段

ArraySubType

指定非托管 LPArrayByValArray 的元素类型。Specifies the element type of the unmanaged LPArray or ByValArray.

IidParameterIndex

指定 COM 使用的非托管 iid_is 特性的参数索引。Specifies the parameter index of the unmanaged iid_is attribute used by COM.

MarshalCookie

向自定义封送处理程序提供附加信息。Provides additional information to a custom marshaler.

MarshalType

指定自定义封送处理程序的完全限定名。Specifies the fully qualified name of a custom marshaler.

MarshalTypeRef

MarshalType 作为类型实现。Implements MarshalType as a type.

SafeArraySubType

指示 SafeArray 的元素类型。Indicates the element type of the SafeArray.

SafeArrayUserDefinedSubType

指示用户定义的 SafeArray 元素类型。Indicates the user-defined element type of the SafeArray.

SizeConst

指示固定长度数组中的元素数,或要导入的字符串中的字符(不是字节)数。Indicates the number of elements in the fixed-length array or the number of characters (not bytes) in a string to import.

SizeParamIndex

指示从零开始的参数,该参数包含数组元素的计数,与 COM 中的 size_is 类似。Indicates the zero-based parameter that contains the count of array elements, similar to size_is in COM.

属性

TypeId

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

(继承自 Attribute)
Value

获取 UnmanagedType 值,数据将被作为该值封送。Gets the UnmanagedType value the data is to be marshaled as.

方法

Equals(Object)

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

(继承自 Attribute)
GetHashCode()

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

(继承自 Attribute)
GetType()

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

(继承自 Object)
IsDefaultAttribute()

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

(继承自 Attribute)
Match(Object)

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

(继承自 Attribute)
MemberwiseClone()

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

(继承自 Object)
ToString()

返回表示当前对象的字符串。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)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(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)

适用于

另请参阅