Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

UnmanagedMarshal Class

NOTE: This class is now obsolete.

Represents the class that describes how to marshal a field from managed to unmanaged code. This class cannot be inherited.

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")] 
public sealed class UnmanagedMarshal
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202") */ 
public final class UnmanagedMarshal
SerializableAttribute 
ComVisibleAttribute(true) 
ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202") 
public final class UnmanagedMarshal
Not applicable.

The code example shows the workaround for this obsolete type.

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. For example, you might want to marshal a String type as an unmanaged BSTR. Some format conversions are handled automatically by the runtime. To override the default behavior, you must use the UnmanagedMarshal class to define the format conversion.

The following code example shows replacement code for the obsolete UnmanagedMarshal type. The example emits a single-module assembly named EmitMarshalAs.dll, containing a type named Sample. The type has a method named Test, with one parameter of type String. The code example applies the MarshalAsAttribute with UnmanagedType.BStr to the parameter.

You can use the MSIL Disassembler (Ildasm.exe) to examine the emitted assembly and observe that the parameter is marked marshal(bstr).

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");
    }
}

System.Object
  System.Reflection.Emit.UnmanagedMarshal

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 1.0, 1.1, 2.0
Obsolete (compiler warning) in 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft