MarshalAsAttribute クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
マネージド コードとアンマネージド コードの間でデータをマーシャリングする方法を示します。
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 を適用します。
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
注釈
この属性は、パラメーター、フィールド、または戻り値に適用できます。
各データ型には既定のマーシャリング動作が設定されているため、この属性は省略可能です。 この属性は、特定の型を複数の型にマーシャリングできる場合にのみ必要です。 たとえば、文字列を LPStr、 LPWStr、 LPTStr、または BStrとしてアンマネージ コードにマーシャリングできます。 既定では、共通言語ランタイムは、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 は、入力タイプ ライブラリで指定されている型に関係なく、 MarshalAsAttribute を常に String 型と Object 型に適用してわかりやすくします。
Note
MarshalAsAttributeでは、ジェネリック型のマーシャリングはサポートされていません。
コンストラクター
| 名前 | 説明 |
|---|---|
| MarshalAsAttribute(Int16) |
指定したMarshalAsAttribute値を使用して、UnmanagedType クラスの新しいインスタンスを初期化します。 |
| MarshalAsAttribute(UnmanagedType) |
指定したMarshalAsAttribute列挙メンバーを使用して、UnmanagedType クラスの新しいインスタンスを初期化します。 |
フィールド
| 名前 | 説明 |
|---|---|
| ArraySubType |
アンマネージ LPArray または ByValArrayの要素型を指定します。 |
| IidParameterIndex |
COM で使用されるアンマネージ |
| MarshalCookie |
カスタム マーシャラーに追加情報を提供します。 |
| MarshalType |
カスタム マーシャラーの完全修飾名を指定します。 |
| MarshalTypeRef |
MarshalTypeを型として実装します。 |
| SafeArraySubType |
SafeArrayの要素の種類を示します。 |
| SafeArrayUserDefinedSubType |
SafeArrayのユーザー定義要素型を示します。 |
| SizeConst |
固定長配列内の要素の数、またはインポートする文字列の文字数 (バイト数ではない) を示します。 |
| SizeParamIndex |
COM の |
プロパティ
| 名前 | 説明 |
|---|---|
| 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) |