当前位置: 首页>>代码示例>>C#>>正文


C# FieldBuilder.SetOffset方法代码示例

本文整理汇总了C#中System.Reflection.Emit.FieldBuilder.SetOffset方法的典型用法代码示例。如果您正苦于以下问题:C# FieldBuilder.SetOffset方法的具体用法?C# FieldBuilder.SetOffset怎么用?C# FieldBuilder.SetOffset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.FieldBuilder的用法示例。


在下文中一共展示了FieldBuilder.SetOffset方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateType

//引入命名空间
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);
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:78,代码来源:FieldBuilder.SetOffset


注:本文中的System.Reflection.Emit.FieldBuilder.SetOffset方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。