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

注釈

この属性は、パラメーター、フィールド、または戻り値に適用できます。

各データ型には既定のマーシャリング動作が設定されているため、この属性は省略可能です。 この属性は、特定の型を複数の型にマーシャリングできる場合にのみ必要です。 たとえば、文字列を、または BStrとしてLPStrLPWStrLPTStrアンマネージド コードにマーシャリングできます。 既定では、共通言語ランタイムは、文字列パラメーターを COM メソッドに として BStr マーシャリングします。 属性を個々のMarshalAsAttributeフィールドまたはパラメーターに適用して、その特定のBStr文字列を ではなく としてLPStrマーシャリングできます。 Tlbexp.exe (タイプ ライブラリ エクスポーター) は、マーシャリング設定を共通言語ランタイムに渡します。

一部のパラメーターと戻り値は、COM 相互運用またはプラットフォーム呼び出しで使用される場合、既定のマーシャリング動作が異なります。 既定では、ランタイムは、プラットフォーム呼び出しメソッドまたは関数に文字列パラメーター (および値型のフィールド) を として LPStr マーシャリングします。 詳細については、「 既定のマーシャリング動作」を参照してください。

ほとんどの場合、 属性は、次の C# シグネチャに示すように、 列挙を使用して UnmanagedType アンマネージ データの形式を識別するだけです。

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

列挙メンバーの中には UnmanagedType 、追加情報が必要なものもあります。 たとえば、 が の場合 UnmanagedType は、追加情報が必要です LPArray。 配列でこの属性を使用する方法の詳細については、「配列の 既定のマーシャリング」を参照してください。

Tlbimp.exe (タイプ ライブラリ インポーター) は、この属性をパラメーター、フィールド、および戻り値にも適用して、入力タイプ ライブラリ内のデータ型が対応するマネージド データ型の既定の型ではないことを示します。 Tlbimp.exeは、入力タイプ ライブラリでMarshalAsAttributeString指定された型に関係なく、 型と Object 型を常に適用してわかりやすくします。

注意

では MarshalAsAttribute 、ジェネリック型のマーシャリングはサポートされていません。

コンストラクター

MarshalAsAttribute(Int16)

指定した UnmanagedType 値を使用して、MarshalAsAttribute クラスの新しいインスタンスを初期化します。

MarshalAsAttribute(UnmanagedType)

指定した MarshalAsAttribute 列挙体メンバーを使用して、UnmanagedType クラスの新しいインスタンスを初期化します。

フィールド

ArraySubType

アンマネージの LPArray または ByValArray の要素の型を示します。

IidParameterIndex

COM で使用されるアンマネージ iid_is 属性のパラメーター インデックスを指定します。

MarshalCookie

カスタム マーシャラーに追加の情報を提供します。

MarshalType

カスタム マーシャラーの完全修飾名を指定します。

MarshalTypeRef

MarshalType を型として実装します。

SafeArraySubType

SafeArray の要素の型を示します。

SafeArrayUserDefinedSubType

SafeArray のユーザー定義要素の型を示します。

SizeConst

固定長配列の要素の数、またはインポートする文字列の文字数 (バイト数ではない) を示します。

SizeParamIndex

COM の size_is のように、0 から始まる配列要素のカウントを格納しているパラメーターを示します。

プロパティ

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)

適用対象

こちらもご覧ください