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 类型的替换代码。 该示例发出名为 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)
已过时.

指定要封送为非托管代码的 LPArrayLPArray 的长度在运行时由实际的已封送数组的大小确定。

DefineSafeArray(UnmanagedType)
已过时.

指定要封送为非托管代码的 SafeArray

DefineUnmanagedMarshal(UnmanagedType)
已过时.

指定要封送为非托管代码的给定类型。

Equals(Object)
已过时.

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()
已过时.

作为默认哈希函数。

(继承自 Object)
GetType()
已过时.

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()
已过时.

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()
已过时.

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅