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 クラスのページへのリンク