FieldBuilder.SetMarshal 方法

注意:此方法现在已过时。

描述该字段的本机封送处理。

**命名空间:**System.Reflection.Emit
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. https://go.microsoft.com/fwlink/?linkid=14202")> _
Public Sub SetMarshal ( _
    unmanagedMarshal As UnmanagedMarshal _
)
用法
Dim instance As FieldBuilder
Dim unmanagedMarshal As UnmanagedMarshal

instance.SetMarshal(unmanagedMarshal)
[ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. https://go.microsoft.com/fwlink/?linkid=14202")] 
public void SetMarshal (
    UnmanagedMarshal unmanagedMarshal
)
[ObsoleteAttribute(L"An alternate API is available: Emit the MarshalAs custom attribute instead. https://go.microsoft.com/fwlink/?linkid=14202")] 
public:
void SetMarshal (
    UnmanagedMarshal^ unmanagedMarshal
)
/** @attribute ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. https://go.microsoft.com/fwlink/?linkid=14202") */ 
public void SetMarshal (
    UnmanagedMarshal unmanagedMarshal
)
ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom attribute instead. https://go.microsoft.com/fwlink/?linkid=14202") 
public function SetMarshal (
    unmanagedMarshal : UnmanagedMarshal
)

参数

  • unmanagedMarshal
    指定该字段的本机封送处理的说明符。

异常

异常类型 条件

ArgumentNullException

unmanagedMarshal 为 空引用(在 Visual Basic 中为 Nothing)。

InvalidOperationException

已经使用 CreateType 创建了该包含类型。

备注

下面的代码阐释了如何使用 SetMarshal

Imports System
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 'Main
End Class 'FieldBuilder_Sample
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;
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   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);
      }
   }

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

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:1.0、1.1
在 2.0 中过时(编译器警告)

请参见

参考

FieldBuilder 类
FieldBuilder 成员
System.Reflection.Emit 命名空间