UnmanagedMarshal クラスとは? わかりやすく解説

UnmanagedMarshal クラス

メモ : このクラスは、互換性のために残されています。

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

名前空間: System.Reflection.Emit
アセンブリ: 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
Dim instance As 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
/** @attribute SerializableAttribute() */ 
/** @attribute ObsoleteAttribute("An alternate API is available: Emit the MarshalAs
 custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")
 */ 
/** @attribute ComVisibleAttribute(true) */ 
public final 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 final class 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.Object
  System.Reflection.Emit.UnmanagedMarshal
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnmanagedMarshal メンバ
System.Reflection.Emit 名前空間
Type
UnmanagedType
Guid



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「UnmanagedMarshal クラス」の関連用語

UnmanagedMarshal クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UnmanagedMarshal クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS