UnmanagedMarshal クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class UnmanagedMarshal
[SerializableAttribute] [ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")] [ComVisibleAttribute(true)] public sealed class UnmanagedMarshal
[SerializableAttribute] [ObsoleteAttribute(L"An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")] [ComVisibleAttribute(true)] public ref class UnmanagedMarshal sealed

![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、MayLeakOnAbort です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
マーシャリングは、パラメータのパッケージ化およびアンパッケージ化の処理であるため、リモート プロシージャ呼び出しが発生することがあります。マネージ型の書式が、対応するアンマネージ型の書式と異なる場合は、マーシャリング中にフィールドが書式変換されます。たとえば、String 型はアンマネージ型の BSTR にマーシャリングします。いくつかの書式変換は、実行時で自動的に処理されます。既定の動作をオーバーライドするには、UnmanagedMarshal クラスを使用して書式変換を定義する必要があります。

旧式の UnmanagedMarshal 型の代替手段となるコード例を次に示します。この例では、Sample という名前の型が含まれる、EmitMarshalAs.dll という単一モジュールのアセンブリが出力されます。この型には、String 型のパラメータを 1 つ受け取る、Test という名前のメソッドが含まれています。コード例では、MarshalAsAttribute を UnmanagedType.BStr にして、パラメータに適用しています。
MSIL 逆アセンブラ (Ildasm.exe) を使用すると、出力されたアセンブリをチェックし、パラメータが marshal(bstr) としてマークされていることを確認できます。
Imports System Imports System.Reflection Imports System.Reflection.Emit Imports System.Runtime.InteropServices Public Class Example Public Shared Sub Main() Dim myDomain As AppDomain = AppDomain.CurrentDomain Dim myAsmName As New AssemblyName("EmitMarshalAs") Dim myAssembly As AssemblyBuilder = _ myDomain.DefineDynamicAssembly(myAsmName, _ AssemblyBuilderAccess.RunAndSave) Dim myModule As ModuleBuilder = _ myAssembly.DefineDynamicModule(myAsmName.Name, _ myAsmName.Name & ".dll") Dim myType As TypeBuilder = _ myModule.DefineType("Sample", TypeAttributes.Public) Dim myMethod As MethodBuilder = _ myType.DefineMethod("Test", MethodAttributes.Public, _ Nothing, new Type() { GetType(String) }) ' Get a parameter builder for the parameter that needs the ' attribute, using the HasFieldMarshal attribute. In this ' example, the parameter is at position 0 and has the name ' "arg". Dim pb As ParameterBuilder = _ myMethod.DefineParameter(0, _ ParameterAttributes.HasFieldMarshal, "arg") ' Get the MarshalAsAttribute constructor that takes an ' argument of type UnmanagedType. ' Dim ciParameters() As Type = { GetType(UnmanagedType) } Dim ci As ConstructorInfo = _ GetType(MarshalAsAttribute).GetConstructor(ciParameters) ' Create a CustomAttributeBuilder representing the attribute , ' specifying the necessary unmanaged type. In this case, ' UnmanagedType.BStr is specified. ' Dim ciArguments() As Object = { UnmanagedType.BStr } Dim cabuilder As New CustomAttributeBuilder(ci, ciArguments) ' Apply the attribute to the parameter. ' pb.SetCustomAttribute(cabuilder) ' Emit a dummy method body. Dim il As ILGenerator = myMethod.GetILGenerator() il.Emit(OpCodes.Ret) myType.CreateType() myAssembly.Save(myAsmName.Name & ".dll") End Sub End Class
using System; using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; public class Example { public static void Main() { AppDomain myDomain = AppDomain.CurrentDomain; AssemblyName myAsmName = new AssemblyName("EmitMarshalAs"); AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAsmName.Name, myAsmName.Name + ".dll"); TypeBuilder myType = myModule.DefineType("Sample", TypeAttributes.Public); MethodBuilder myMethod = myType.DefineMethod("Test", MethodAttributes.Public, null, new Type[] { typeof(string) }); // Get a parameter builder for the parameter that needs the // attribute, using the HasFieldMarshal attribute. In this // example, the parameter is at position 0 and has the name // "arg". ParameterBuilder pb = myMethod.DefineParameter(0, ParameterAttributes.HasFieldMarshal, "arg"); // Get the MarshalAsAttribute constructor that takes an // argument of type UnmanagedType. // ConstructorInfo ci = typeof(MarshalAsAttribute).GetConstructor( new Type[] { typeof(UnmanagedType) }); // Create a CustomAttributeBuilder representing the attribute , // specifying the necessary unmanaged type. In this case, // UnmanagedType.BStr is specified. // CustomAttributeBuilder cabuilder = new CustomAttributeBuilder( ci, new object[] { UnmanagedType.BStr }); // Apply the attribute to the parameter. // pb.SetCustomAttribute(cabuilder); // Emit a dummy method body. ILGenerator il = myMethod.GetILGenerator(); il.Emit(OpCodes.Ret); Type finished = myType.CreateType(); myAssembly.Save(myAsmName.Name + ".dll"); } }
using namespace System; using namespace System::Reflection; using namespace System::Reflection::Emit; using namespace System::Runtime::InteropServices; void main() { AppDomain^ myDomain = AppDomain::CurrentDomain; AssemblyName^ myAsmName = gcnew AssemblyName("EmitMarshalAs"); AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess::RunAndSave); ModuleBuilder^ myModule = myAssembly->DefineDynamicModule(myAsmName->Name, myAsmName->Name + ".dll"); TypeBuilder^ myType = myModule->DefineType("Sample", TypeAttributes::Public); MethodBuilder^ myMethod = myType->DefineMethod("Test", MethodAttributes::Public, nullptr, gcnew array<Type^> { String::typeid }); // Get a parameter builder for the parameter that needs the // attribute, using the HasFieldMarshal attribute. In this // example, the parameter is at position 0 and has the name // "arg". ParameterBuilder^ pb = myMethod->DefineParameter(0, ParameterAttributes::HasFieldMarshal, "arg"); // Get the MarshalAsAttribute constructor that takes an // argument of type UnmanagedType. // //Type^ maattrType = MarshalAsAttribute::typeid; ConstructorInfo^ ci = (MarshalAsAttribute::typeid)->GetConstructor( gcnew array<Type^> { UnmanagedType::typeid }); // Create a CustomAttributeBuilder representing the attribute, // specifying the necessary unmanaged type. In this case, // UnmanagedType.BStr is specified. // CustomAttributeBuilder^ cabuilder = gcnew CustomAttributeBuilder( ci, gcnew array<Object^> { UnmanagedType::BStr }); // Apply the attribute to the parameter. // pb->SetCustomAttribute(cabuilder); ILGenerator^ il = myMethod->GetILGenerator(); il->Emit(OpCodes::Ret); Type^ finished = myType->CreateType(); myAssembly->Save(myAsmName->Name + ".dll"); }

System.Reflection.Emit.UnmanagedMarshal


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


UnmanagedMarshal プロパティ

名前 | 説明 | |
---|---|---|
![]() | BaseType | アンマネージ基本型を取得します。このプロパティは読み取り専用です。 |
![]() | ElementCount | 要素の数を取得します。このプロパティは読み取り専用です。 |
![]() | GetUnmanagedType | アンマネージ型を示します。このプロパティは読み取り専用です。 |
![]() | IIDGuid | グローバル一意識別子 (GUID: Globally Unique Indentifier) を取得します。このプロパティは読み取り専用です。 |

UnmanagedMarshal メソッド

名前 | 説明 | |
---|---|---|
![]() | DefineByValArray | アンマネージ コードにマーシャリングする固定長の配列 (ByValArray) を指定します。 |
![]() | DefineByValTStr | アンマネージ コードにマーシャリングする固定長の配列バッファ (ByValTStr) の文字列を指定します。 |
![]() | DefineLPArray | アンマネージ コードにマーシャリングする LPArray を指定します。LPArray の長さは、実際にマーシャリングされた配列のサイズによって実行時に決定されます。 |
![]() | DefineSafeArray | アンマネージ コードにマーシャリングする SafeArray を指定します。 |
![]() | DefineUnmanagedMarshal | アンマネージ コードにマーシャリングする型を指定します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

UnmanagedMarshal メンバ
マネージ コードからアンマネージ コードにフィールドをマーシャリングする方法を記述するクラスを表します。このクラスは継承できません。
UnmanagedMarshal データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | BaseType | アンマネージ基本型を取得します。このプロパティは読み取り専用です。 |
![]() | ElementCount | 要素の数を取得します。このプロパティは読み取り専用です。 |
![]() | GetUnmanagedType | アンマネージ型を示します。このプロパティは読み取り専用です。 |
![]() | IIDGuid | グローバル一意識別子 (GUID: Globally Unique Indentifier) を取得します。このプロパティは読み取り専用です。 |

名前 | 説明 | |
---|---|---|
![]() | DefineByValArray | アンマネージ コードにマーシャリングする固定長の配列 (ByValArray) を指定します。 |
![]() | DefineByValTStr | アンマネージ コードにマーシャリングする固定長の配列バッファ (ByValTStr) の文字列を指定します。 |
![]() | DefineLPArray | アンマネージ コードにマーシャリングする LPArray を指定します。LPArray の長さは、実際にマーシャリングされた配列のサイズによって実行時に決定されます。 |
![]() | DefineSafeArray | アンマネージ コードにマーシャリングする SafeArray を指定します。 |
![]() | DefineUnmanagedMarshal | アンマネージ コードにマーシャリングする型を指定します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からUnmanagedMarshalを検索する場合は、下記のリンクをクリックしてください。

- UnmanagedMarshalのページへのリンク