UnmanagedMarshal 클래스

정의

주의

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

주의

An alternate API is available: Emit the MarshalAs custom attribute instead.

관리 코드에서 비관리 코드로 필드를 마샬링하는 방법을 설명하는 클래스를 나타냅니다. 이 클래스는 상속될 수 없습니다.

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]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
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
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead.")>]
type UnmanagedMarshal = class
Public NotInheritable Class UnmanagedMarshal
상속
UnmanagedMarshal
특성

예제

다음 코드 예제에서는 사용 되지 않는 항목에 대 한 대체 코드를 보여 줍니다. UnmanagedMarshal 형식입니다. 이 예제에서는 명명 된 단일 모듈 어셈블리를 내보냅니다 EmitMarshalAs.dll, 명명 된 형식을 포함 하 Sample합니다. 라는 메서드를 포함 하는 형식을 Test, 형식의 매개 변수 하나를 사용 하 여 String입니다. 적용 하는 코드 예제는 MarshalAsAttribute 사용 하 여 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

설명

코드 예제에서는이 사용 되지 않는 형식에 대 한 해결 방법을 보여 줍니다.

마샬링 되므로 프로세스 압축 하 고 패키지 매개 변수를 원격 프로시저 호출에서 발생할 수 있습니다. 마샬링하는 동안 관리 되는 형식의 형식을 관리 되지 않는 형식에 해당 형식과 다른 경우 필드 형식 변환이 거쳐야 할 수 있습니다. 마샬링할 하려는 하는 예를 들어를 String 는 관리 되지 않는 BSTR 형식입니다. 일부 형식 변환은 런타임에 의해 자동으로 처리 됩니다. 기본 동작을 재정의 하려면 사용 해야는 UnmanagedMarshal 형식 변환을 정의 하는 클래스입니다.

속성

BaseType

관리되지 않는 기본 형식을 가져옵니다. 이 속성은 읽기 전용입니다.

ElementCount

숫자 요소를 가져옵니다. 이 속성은 읽기 전용입니다.

GetUnmanagedType

관리되지 않는 형식을 나타냅니다. 이 속성은 읽기 전용입니다.

IIDGuid

GUID를 가져옵니다. 이 속성은 읽기 전용입니다.

메서드

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)

적용 대상

추가 정보