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 类型的替换代码。 The example emits a single-module assembly named EmitMarshalAs.dll, containing a type named Sample. 该类型具有一个命名 Test的方法,其类型 String为一个参数。 代码示例将 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

注解

该代码示例显示了此过时类型的解决方法。

封送处理是打包和解压缩参数的过程,以便远程过程调用可能发生。 封送处理期间,当托管类型的格式不同于相应非托管类型的格式时,字段可能会进行格式转换。 例如,你可能想要将类型封 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)

适用于

另请参阅