Compartilhar via


FieldBuilder.SetMarshal(UnmanagedMarshal) Método

Definição

Cuidado

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

Descreve o marshaling nativo do campo.

public:
 void SetMarshal(System::Reflection::Emit::UnmanagedMarshal ^ unmanagedMarshal);
public void SetMarshal (System.Reflection.Emit.UnmanagedMarshal unmanagedMarshal);
[System.Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public void SetMarshal (System.Reflection.Emit.UnmanagedMarshal unmanagedMarshal);
member this.SetMarshal : System.Reflection.Emit.UnmanagedMarshal -> unit
[<System.Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")>]
member this.SetMarshal : System.Reflection.Emit.UnmanagedMarshal -> unit
Public Sub SetMarshal (unmanagedMarshal As UnmanagedMarshal)

Parâmetros

unmanagedMarshal
UnmanagedMarshal

Um descritor que especifica o marshaling nativo desse campo.

Atributos

Exceções

unmanagedMarshal é null.

O tipo recipiente foi criado usando CreateType().

Comentários

O exemplo de código a seguir ilustra o uso de SetMarshal.

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::CompilerServices;
Type^ CreateType( AppDomain^ currentDomain )
{
   // Create an assembly.
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "DynamicAssembly";
   AssemblyBuilder^ myAssembly = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );

   // Create a dynamic module in Dynamic Assembly.
   ModuleBuilder^ myModuleBuilder = myAssembly->DefineDynamicModule( "MyModule", "MyModule.mod" );

   // Define a public class named S"MyClass" in the assembly.
   TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "MyClass", TypeAttributes::Public );
   TypeBuilder^ myTypeBuilder2 = myModuleBuilder->DefineType( "MyClass2", static_cast<TypeAttributes>(TypeAttributes::Public | TypeAttributes::BeforeFieldInit | TypeAttributes::SequentialLayout | TypeAttributes::AnsiClass | TypeAttributes::Sealed) );
   FieldBuilder^ myFieldBuilder1 = myTypeBuilder2->DefineField( "myBytes1", Byte::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder2 = myTypeBuilder2->DefineField( "myBytes2", Byte::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder3 = myTypeBuilder2->DefineField( "myErrorCode", short::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder4 = myTypeBuilder2->DefineField( "myReserved1", short::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder5 = myTypeBuilder2->DefineField( "myReserved2", short::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder6 = myTypeBuilder2->DefineField( "myPathName", array<char>::typeid,FieldAttributes::Public );
   myFieldBuilder6->SetMarshal( UnmanagedMarshal::DefineByValArray( 128 ) );
   myFieldBuilder6->SetOffset( 4 );
   Type^ myType1 = myTypeBuilder2->CreateType();

   // Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
   array<Type^>^myParameters = {String::typeid,myType1,UInt32::typeid};
   MethodBuilder^ myMethodBuilder = myTypeBuilder->DefinePInvokeMethod( "OpenFile", "kernel32.dll", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static | MethodAttributes::HideBySig), CallingConventions::Standard, IntPtr::typeid, myParameters, CallingConvention::Winapi, CharSet::None );
   Type^ myAttributeType = MethodImplAttribute::typeid;
   array<Type^>^type1 = {MethodImplOptions::typeid};
   ConstructorInfo^ myConstructorInfo = myAttributeType->GetConstructor( type1 );
   array<Object^>^obj1 = {MethodImplOptions::PreserveSig};
   CustomAttributeBuilder^ myAttributeBuilder = gcnew CustomAttributeBuilder( myConstructorInfo,obj1 );
   myMethodBuilder->SetCustomAttribute( myAttributeBuilder );
   ParameterBuilder^ myParameterBuilder2 = myMethodBuilder->DefineParameter( 2, ParameterAttributes::Out, "myClass2" );
   Type^ myType = myTypeBuilder->CreateType();
   myAssembly->Save( "EmittedAssembly.dll" );
   return myType;
}

int main()
{
   try
   {
      Type^ myType = CreateType( Thread::GetDomain() );
      Type^ myClass2 = myType->Module->GetType( "MyClass2" );
      Object^ myParam2 = Activator::CreateInstance( myClass2 );
      UInt32 myUint = 0x00000800;
      array<Object^>^myArgs = {"MyFile.Txt",myParam2,myUint};
      Object^ myObject = myType->InvokeMember( "OpenFile", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::InvokeMethod | BindingFlags::Static), nullptr, nullptr, myArgs );
      Console::WriteLine( "MyClass::OpenFile method returned: \"{0}\"", myObject );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Caught {0}", e->Message );
   }
}
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
using System.Runtime.CompilerServices;

public class FieldBuilder_Sample
{
   public static Type CreateType(AppDomain currentDomain)
   {
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
         currentDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder myModuleBuilder=myAssembly.DefineDynamicModule("MyModule","MyModule.mod");
      // Define a public class named "MyClass" in the assembly.
      TypeBuilder myTypeBuilder= myModuleBuilder.DefineType("MyClass",TypeAttributes.Public);
      TypeBuilder myTypeBuilder2 = myModuleBuilder.DefineType("MyClass2",
         TypeAttributes.Public|TypeAttributes.BeforeFieldInit|TypeAttributes.SequentialLayout|TypeAttributes.AnsiClass|TypeAttributes.Sealed);
      FieldBuilder myFieldBuilder1= myTypeBuilder2.DefineField("myBytes1",
                                    typeof(byte),FieldAttributes.Public);
      FieldBuilder myFieldBuilder2= myTypeBuilder2.DefineField("myBytes2",
                                    typeof(byte),FieldAttributes.Public);
      FieldBuilder myFieldBuilder3= myTypeBuilder2.DefineField("myErrorCode",
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder4= myTypeBuilder2.DefineField("myReserved1",
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder5= myTypeBuilder2.DefineField("myReserved2",
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder6= myTypeBuilder2.DefineField("myPathName",
                                    typeof(char[]),FieldAttributes.Public);
      myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128));
      myFieldBuilder6.SetOffset(4);
      Type myType1 = myTypeBuilder2.CreateType();
      // Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
      Type[] myParameters={ typeof(string), myType1 ,typeof(uint)};
      MethodBuilder myMethodBuilder= myTypeBuilder.DefinePInvokeMethod("OpenFile",
                                     "kernel32.dll",MethodAttributes.Public|MethodAttributes.Static|MethodAttributes.HideBySig,
                                       CallingConventions.Standard,typeof(IntPtr),
                                       myParameters,CallingConvention.Winapi,CharSet.None);
      Type myAttributeType = typeof(MethodImplAttribute);
      ConstructorInfo myConstructorInfo =
         myAttributeType.GetConstructor(new Type[1]{typeof(MethodImplOptions)});
      CustomAttributeBuilder myAttributeBuilder = new CustomAttributeBuilder(myConstructorInfo,
                                                   new object[1]{MethodImplOptions.PreserveSig});
      myMethodBuilder.SetCustomAttribute(myAttributeBuilder);
      ParameterBuilder myParameterBuilder2=myMethodBuilder.DefineParameter(2,
                                            ParameterAttributes.Out,"myClass2");
      Type myType=myTypeBuilder.CreateType();
      myAssembly.Save("EmittedAssembly.dll");
      return myType;
   }

   public static void Main()
   {
      try
      {
         Type myType = CreateType(Thread.GetDomain());
         Type myClass2 = myType.Module.GetType("MyClass2");
         object myParam2 = Activator.CreateInstance(myClass2);
         uint myUint=0x00000800;
         object[] myArgs= {"MyFile.Txt",myParam2,myUint};
         Object myObject  = myType.InvokeMember("OpenFile",BindingFlags.Public |
            BindingFlags.InvokeMethod | BindingFlags.Static,null,null,myArgs);
         Console.WriteLine("MyClass.OpenFile method returned: \"{0}\"", myObject);
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception Caught "+e.Message);
      }
   }
}
Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions
Imports System.Runtime.CompilerServices

Public Class FieldBuilder_Sample

   Public Shared Function CreateType(ByVal currentDomain As AppDomain) As Type

      ' Create an assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "DynamicAssembly"
      Dim myAssembly As AssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
                                          AssemblyBuilderAccess.RunAndSave)
      ' Create a dynamic module in Dynamic Assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule", _
                                            "MyModule.mod")
      ' Define a public class named "MyClass" in the assembly.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass", _
                                         TypeAttributes.Public)
      Dim myTypeBuilder2 As TypeBuilder = myModuleBuilder.DefineType("MyClass2", _
         TypeAttributes.Public Or TypeAttributes.BeforeFieldInit Or _
         TypeAttributes.SequentialLayout Or TypeAttributes.AnsiClass Or TypeAttributes.Sealed)
      Dim myFieldBuilder1 As FieldBuilder = myTypeBuilder2.DefineField("myBytes1", _
         GetType(Byte), FieldAttributes.Public)
      Dim myFieldBuilder2 As FieldBuilder = myTypeBuilder2.DefineField("myBytes2", _
         GetType(Byte), FieldAttributes.Public)
      Dim myFieldBuilder3 As FieldBuilder = myTypeBuilder2.DefineField("myErrorCode", _
         GetType(Short), FieldAttributes.Public)
      Dim myFieldBuilder4 As FieldBuilder = myTypeBuilder2.DefineField("myReserved1", _
         GetType(Short), FieldAttributes.Public)
      Dim myFieldBuilder5 As FieldBuilder = myTypeBuilder2.DefineField("myReserved2", _
         GetType(Short), FieldAttributes.Public)
      Dim myFieldBuilder6 As FieldBuilder = myTypeBuilder2.DefineField("myPathName", _
         GetType(Char()), FieldAttributes.Public)
      myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128))
      myFieldBuilder6.SetOffset(4)
      Dim myType1 As Type = myTypeBuilder2.CreateType()
      ' Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
      Dim myParameters As Type() = {GetType(String), myType1, GetType(System.UInt32)}
      Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefinePInvokeMethod("OpenFile", _
         "kernel32.dll", MethodAttributes.Public Or MethodAttributes.Static Or _
         MethodAttributes.HideBySig, CallingConventions.Standard, GetType(IntPtr), _
         myParameters, CallingConvention.Winapi, CharSet.None)
      Dim myAttributeType As Type = GetType(MethodImplAttribute)
      Dim myConstructorInfo As ConstructorInfo = myAttributeType.GetConstructor(New Type(0) _
         {GetType(MethodImplOptions)})
      Dim myAttributeBuilder As New CustomAttributeBuilder(myConstructorInfo, _
         New Object() {MethodImplOptions.PreserveSig})
      myMethodBuilder.SetCustomAttribute(myAttributeBuilder)
      Dim myParameterBuilder2 As ParameterBuilder = myMethodBuilder.DefineParameter(2, _
         ParameterAttributes.Out, "myClass2")
      Dim myType As Type = myTypeBuilder.CreateType()
      myAssembly.Save("EmittedAssembly.dll")
      Return myType
   End Function 'CreateType

   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Try
         Dim myType As Type = CreateType(Thread.GetDomain())
         Dim myClass2 As Type = myType.Module.GetType("MyClass2")
         Dim myParam2 As Object = Activator.CreateInstance(myClass2)
         Dim myUint As System.UInt32
         myUint.Parse("800")

         Dim myArgs As Object() = {"MyFile.Txt", myParam2, myUint}
         Dim myObject As Object = myType.InvokeMember("OpenFile", _
                                    BindingFlags.Public Or BindingFlags.InvokeMethod Or _
                                    BindingFlags.Static, Nothing, Nothing, myArgs)
         Console.WriteLine("MyClass.OpenFile method returned: '{0}'", myObject)
      Catch e As Exception
         Console.WriteLine("Exception Caught: " & e.Message)
      End Try
   End Sub
End Class

Aplica-se a