MarshalAsAttribute MarshalAsAttribute MarshalAsAttribute MarshalAsAttribute Class

定義

表示如何在 Managed 和 Unmanaged 程式碼之間封送處理資料。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
繼承
MarshalAsAttributeMarshalAsAttributeMarshalAsAttributeMarshalAsAttribute
屬性

範例

下列範例會套用MarshalAsAttribute欄位、 方法參數和方法傳回 managed 型別值。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. 比方說,您可以字串封送處理至 unmanaged 程式碼,可能是LPStr,則LPWStr,則LPTStr,或BStrFor example, you can marshal a string to unmanaged code as either a LPStr, a LPWStr, a LPTStr, or a BStr. 根據預設,通用語言執行平台封送處理字串參數,做為BStrCOM 方法。By default, the common language runtime marshals a string parameter as a BStr to COM methods. 您可以套用MarshalAsAttribute到個別的欄位或參數,讓封送處理為該特定的字串屬性LPStr而不是BStrYou 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 (類型程式庫匯出工具)傳遞您 common language runtime 的封送處理喜好設定。The Tlbexp.exe (Type Library Exporter) passes your marshaling preferences to the common language runtime.

某些參數和傳回值有不同的預設封送處理行為時搭配使用 COM interop 或平台叫用。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. 例如,需要其他資訊時UnmanagedTypeLPArrayFor 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 (型別程式庫匯入工具)也將此屬性套用至參數、 欄位和傳回值,表示輸入的型別程式庫中的資料類型不是對應的 managed 的資料類型的預設類型。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. Tlbimp.exe 一律會套用MarshalAsAttributeStringObject為了清楚起見,無論何種輸入型別程式庫中所指定類型的類型。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) MarshalAsAttribute(Int16) MarshalAsAttribute(Int16) MarshalAsAttribute(Int16)

使用指定的 UnmanagedType 值,初始化 MarshalAsAttribute 類別的新執行個體。Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.

MarshalAsAttribute(UnmanagedType) MarshalAsAttribute(UnmanagedType) MarshalAsAttribute(UnmanagedType) MarshalAsAttribute(UnmanagedType)

使用指定的 MarshalAsAttribute 列舉型別 (Enumeration) 成員,初始化 UnmanagedType 類別的新執行個體。Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType enumeration member.

欄位

ArraySubType ArraySubType ArraySubType ArraySubType

指定 Unmanaged LPArrayByValArray 的元素型別。Specifies the element type of the unmanaged LPArray or ByValArray.

IidParameterIndex IidParameterIndex IidParameterIndex IidParameterIndex

指定 COM 所使用之 Unmanaged iid_is 屬性的參數索引。Specifies the parameter index of the unmanaged iid_is attribute used by COM.

MarshalCookie MarshalCookie MarshalCookie MarshalCookie

提供其他資訊給自訂封送處理器。Provides additional information to a custom marshaler.

MarshalType MarshalType MarshalType MarshalType

指定自訂封送處理器的完整名稱。Specifies the fully qualified name of a custom marshaler.

MarshalTypeRef MarshalTypeRef MarshalTypeRef MarshalTypeRef

實作 MarshalType 為型別。Implements MarshalType as a type.

SafeArraySubType SafeArraySubType SafeArraySubType SafeArraySubType

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

SafeArrayUserDefinedSubType SafeArrayUserDefinedSubType SafeArrayUserDefinedSubType SafeArrayUserDefinedSubType

指示 SafeArray 的使用者定義的元素型別。Indicates the user-defined element type of the SafeArray.

SizeConst SizeConst SizeConst SizeConst

指示固定長度陣列中的元素數目或要匯出之字串中的字元數目 (非位元組)。Indicates the number of elements in the fixed-length array or the number of characters (not bytes) in a string to import.

SizeParamIndex SizeParamIndex SizeParamIndex SizeParamIndex

指示哪一個以零起始的參數含有陣列元素的計數,類似 COM 中的 size_isIndicates the zero-based parameter that contains the count of array elements, similar to size_is in COM.

屬性

TypeId TypeId TypeId TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Value Value Value Value

取得資料要封送處理成的 UnmanagedType 值。Gets the UnmanagedType value the data is to be marshaled as.

方法

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 的淺層複本 (Shallow Copy)。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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。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)

適用於

另請參閱