UnmanagedMarshal クラス

定義

注意事項

An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202

マネージド コードからアンマネージド コードにフィールドをマーシャリングする方法を記述するクラスを表します。 このクラスは継承できません。

public ref class UnmanagedMarshal sealed
[System.Serializable]
public sealed class UnmanagedMarshal
[System.Serializable]
[System.Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class UnmanagedMarshal
[<System.Serializable>]
type UnmanagedMarshal = class
[<System.Serializable>]
[<System.Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type UnmanagedMarshal = class
Public NotInheritable Class UnmanagedMarshal
継承
UnmanagedMarshal
属性

次のコード例は、古い UnmanagedMarshal 型の置換コードを示しています。 この例では、 という名前の型Sampleを含む という名前EmitMarshalAs.dllの単一モジュール アセンブリを出力します。 型には、 という名前 Testのメソッドがあり、 型 Stringのパラメーターが 1 つです。 コード例では、 パラメーターに MarshalAsAttribute with UnmanagedType.BStr を適用します。

Ildasm.exe (IL 逆アセンブラー) を使用して、出力されたアセンブリを調べ、パラメーターが とマークmarshal(bstr)されていることを確認できます。

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");
}
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");
    }
}
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

注釈

コード例は、この古い型の回避策を示しています。

マーシャリングは、リモート プロシージャ 呼び出しが発生できるように、パラメーターをパッケージ化およびアンパックするプロセスです。 マーシャリング中に、マネージド型の形式が対応するアンマネージド型の形式と異なる場合、フィールドの形式変換が行われる場合があります。 たとえば、型をアンマネージド BSTR としてマーシャリング String できます。 一部の形式変換は、ランタイムによって自動的に処理されます。 既定の動作をオーバーライドするには、 クラスを UnmanagedMarshal 使用して書式変換を定義する必要があります。

プロパティ

BaseType
古い.

アンマネージ基本型を取得します。 このプロパティは読み取り専用です。

ElementCount
古い.

要素の数を取得します。 このプロパティは読み取り専用です。

GetUnmanagedType
古い.

アンマネージ型を示します。 このプロパティは読み取り専用です。

IIDGuid
古い.

グローバル一意識別子 (GUID: Globally Unique Indentifier) を取得します。 このプロパティは読み取り専用です。

メソッド

DefineByValArray(Int32)
古い.

アンマネージ コードにマーシャリングする固定長の配列 (ByValArray) を指定します。

DefineByValTStr(Int32)
古い.

アンマネージ コードにマーシャリングする固定長の配列バッファー (ByValTStr) の文字列を指定します。

DefineLPArray(UnmanagedType)
古い.

アンマネージ コードにマーシャリングする LPArray を指定します。 LPArray の長さは、実際にマーシャリングされた配列のサイズによって実行時に決定されます。

DefineSafeArray(UnmanagedType)
古い.

アンマネージ コードにマーシャリングする SafeArray を指定します。

DefineUnmanagedMarshal(UnmanagedType)
古い.

アンマネージ コードにマーシャリングする型を指定します。

Equals(Object)
古い.

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()
古い.

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()
古い.

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()
古い.

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()
古い.

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください