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
この属性は、パラメーター、フィールド、または戻り値に適用できます。
各データ型には既定のマーシャリング動作が設定されているため、この属性は省略可能です。 この属性は、特定の型を複数の型にマーシャリングできる場合にのみ必要です。 たとえば、文字列を、または 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 列挙メンバーには追加情報が必要です。 たとえば、 が の場合UnmanagedTypeLPArrayは追加情報が必要です。 配列でこの属性を使用する方法の詳細については、「配列の 既定のマーシャリング」を参照してください。
Tlbimp.exe (タイプ ライブラリ インポーター) は、この属性をパラメーター、フィールド、および戻り値にも適用して、入力タイプ ライブラリのデータ型が対応するマネージド データ型の既定の型ではないことを示します。 Tlbimp.exe は、入力タイプ ライブラリでMarshalAsAttribute指定された型にStringObject関係なく、明確にするために 常に 型と 型を適用します。
注意
では MarshalAsAttribute 、ジェネリック型のマーシャリングはサポートされていません。
Marshal |
指定した UnmanagedType 値を使用して、MarshalAsAttribute クラスの新しいインスタンスを初期化します。 |
Marshal |
指定した MarshalAsAttribute 列挙体メンバーを使用して、UnmanagedType クラスの新しいインスタンスを初期化します。 |
Array |
アンマネージの LPArray または ByValArray の要素の型を示します。 |
Iid |
COM で使用されるアンマネージ |
Marshal |
カスタム マーシャラーに追加の情報を提供します。 |
Marshal |
カスタム マーシャラーの完全修飾名を指定します。 |
Marshal |
MarshalType を型として実装します。 |
Safe |
SafeArray の要素の型を示します。 |
Safe |
SafeArray のユーザー定義要素の型を示します。 |
Size |
固定長配列の要素の数、またはインポートする文字列の文字数 (バイト数ではない) を示します。 |
Size |
COM の |
Type |
派生クラスで実装されると、この Attribute の一意の識別子を取得します。 (継承元 Attribute) |
Value |
マーシャリング後データの UnmanagedType 値を取得します。 |
Equals(Object) |
このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。 (継承元 Attribute) |
Get |
このインスタンスのハッシュ コードを返します。 (継承元 Attribute) |
Get |
現在のインスタンスの Type を取得します。 (継承元 Object) |
Is |
派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。 (継承元 Attribute) |
Match(Object) |
派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (継承元 Attribute) |
Memberwise |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
To |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
_Attribute. |
一連の名前を対応する一連のディスパッチ識別子に割り当てます。 (継承元 Attribute) |
_Attribute. |
オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。 (継承元 Attribute) |
_Attribute. |
オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 (継承元 Attribute) |
_Attribute. |
オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。 (継承元 Attribute) |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。