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.

表示说明如何将字段从托管代码封送为非托管代码的类。Represents the class that describes how to marshal a field from managed to unmanaged code. 此类不能被继承。This class cannot be inherited.

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
属性

示例

下面的代码示例显示过时类型的替换代码 UnmanagedMarshalThe following code example shows replacement code for the obsolete UnmanagedMarshal type. 该示例发出名为的单模块程序集 EmitMarshalAs.dll ,其中包含一个名为的类型 SampleThe example emits a single-module assembly named EmitMarshalAs.dll, containing a type named Sample. 该类型具有一个名为 Test 的方法,该方法具有一个类型为的参数 StringThe type has a method named Test, with one parameter of type String. 此代码示例将和应用于 MarshalAsAttribute UnmanagedType.BStr 参数。The code example applies the MarshalAsAttribute with UnmanagedType.BStr to the parameter.

您可以使用 Ildasm.exe (IL 拆装器) 检查发出的程序集,并观察参数是否已标记 marshal(bstr)You can use the Ildasm.exe (IL Disassembler) to examine the emitted assembly and observe that the parameter is marked 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

注解

此代码示例演示了此过时类型的解决方法。The code example shows the workaround for this obsolete type.

封送是打包和 unpackaging 参数的过程,以便可以执行远程过程调用。Marshaling is the process of packaging and unpackaging parameters so remote procedure calls can occur. 在封送处理期间,当托管类型的格式不同于相应的非托管类型的格式时,字段可能会进行格式转换。During marshaling, a field might undergo a format conversion when the format of the managed type is different from the format of the corresponding unmanaged type. 例如,你可能需要将类型封送 String 为非托管 BSTR。For example, you might want to marshal a String type as an unmanaged BSTR. 某些格式转换由运行时自动处理。Some format conversions are handled automatically by the runtime. 若要重写默认行为,必须使用 UnmanagedMarshal 类来定义格式转换。To override the default behavior, you must use the UnmanagedMarshal class to define the format conversion.

属性

BaseType

获取非托管基类型。Gets an unmanaged base type. 此属性为只读。This property is read-only.

ElementCount

获取数字元素。Gets a number element. 此属性为只读。This property is read-only.

GetUnmanagedType

指示非托管类型。Indicates an unmanaged type. 此属性为只读。This property is read-only.

IIDGuid

获取 GUID。Gets a GUID. 此属性为只读。This property is read-only.

方法

DefineByValArray(Int32)

指定要封送为非托管代码的固定长度的数组 (ByValArray)。Specifies a fixed-length array (ByValArray) to marshal to unmanaged code.

DefineByValTStr(Int32)

指定要封送为非托管代码的固定数组缓冲区 (ByValTStr) 中的字符串。Specifies a string in a fixed array buffer (ByValTStr) to marshal to unmanaged code.

DefineLPArray(UnmanagedType)

指定要封送为非托管代码的 LPArraySpecifies an LPArray to marshal to unmanaged code. LPArray 的长度在运行时由实际的已封送数组的大小确定。The length of an LPArray is determined at runtime by the size of the actual marshaled array.

DefineSafeArray(UnmanagedType)

指定要封送为非托管代码的 SafeArraySpecifies a SafeArray to marshal to unmanaged code.

DefineUnmanagedMarshal(UnmanagedType)

指定要封送为非托管代码的给定类型。Specifies a given type that is to be marshaled to unmanaged code.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅