MarshalAsAttribute 類別

定義

表示如何在 Managed 和 Unmanaged 程式碼之間封送處理資料。

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 套用至 Managed 型別中的欄位、方法參數和方法傳回值。

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

備註

您可以將此屬性套用至參數、欄位或傳回值。

這個屬性是選擇性的,因為每個資料類型都有預設封送處理行為。 只有在指定的型別可以封送處理至多個類型時,才需要這個屬性。 例如,您可以將字串封送處理至 Unmanaged 程式碼,做為 LPStrLPWStr 、、 LPTStrBStr 。 根據預設,Common Language Runtime 會將字串參數封送處理為 BStr COM 方法。 您可以將 屬性套用 MarshalAsAttribute 至個別欄位或參數,讓該特定字串封送處理為 LPStr ,而不是 BStrTlbexp.exe (型別程式庫匯出工具) 將您的封送處理喜好設定傳遞至 Common Language Runtime。

搭配 COM Interop 或平台叫用使用時,某些參數和傳回值有不同的預設封送處理行為。 根據預設,執行時間會將字串參數封送處理為平臺叫用方法或函式 (和實數值型別中的欄位) LPStr 。 如需詳細資訊,請參閱 預設封送處理行為

在大部分情況下,屬性只會使用 UnmanagedType 列舉來識別 Unmanaged 資料的格式,如下列 C# 簽章所示:

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

某些 UnmanagedType 列舉成員需要其他資訊。 例如,當 為 LPArrayUnmanagedType ,需要其他資訊。 如需如何搭配陣列使用此屬性的完整描述,請參閱 陣列的預設封送處理

Tlbimp.exe (型別程式庫匯入工具) 也會將此屬性套用至參數、欄位和傳回值,以指出輸入型別程式庫中的資料類型不是對應 Managed 資料類型的預設類型。 不論輸入類型程式庫中指定的類型為何,Tlbimp.exe一律會將 MarshalAsAttribute 套用至 StringObject 型別。

注意

MarshalAsAttribute不支援泛型型別的封送處理。

建構函式

MarshalAsAttribute(Int16)

使用指定的 UnmanagedType 值,初始化 MarshalAsAttribute 類別的新執行個體。

MarshalAsAttribute(UnmanagedType)

使用指定的 MarshalAsAttribute 列舉型別 (Enumeration) 成員,初始化 UnmanagedType 類別的新執行個體。

欄位

ArraySubType

指定 Unmanaged LPArrayByValArray 的元素型別。

IidParameterIndex

指定 COM 所使用之 Unmanaged 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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱