當前位置: 首頁>>代碼示例>>C#>>正文


C# FieldBuilder類代碼示例

本文整理匯總了C#中System.Reflection.Emit.FieldBuilder的典型用法代碼示例。如果您正苦於以下問題:C# FieldBuilder類的具體用法?C# FieldBuilder怎麽用?C# FieldBuilder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


FieldBuilder類屬於System.Reflection.Emit命名空間,在下文中一共展示了FieldBuilder類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CreateType

//引入命名空間
using System;
using System.Reflection;
using System.Reflection.Emit;

public class FieldBuilder_Sample
{
   private static Type CreateType()
   {
      // Create an assembly.
      AssemblyName assemName = new AssemblyName();
      assemName.Name = "DynamicAssembly";
      AssemblyBuilder assemBuilder =
                     AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run);
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("DynamicModule");
      // Define a public class named "DynamicClass" in the assembly.
      TypeBuilder typBuilder = modBuilder.DefineType("DynamicClass", TypeAttributes.Public);

      // Define a private String field named "DynamicField" in the type.
      FieldBuilder fldBuilder = typBuilder.DefineField("DynamicField",
          typeof(string), FieldAttributes.Private | FieldAttributes.Static);
      // Create the constructor.
      Type[] constructorArgs = { typeof(String) };
      ConstructorBuilder constructor = typBuilder.DefineConstructor(
         MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = constructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Ldarg_0);
      constructorIL.Emit(OpCodes.Ldarg_1);
      constructorIL.Emit(OpCodes.Stfld, fldBuilder);
      constructorIL.Emit(OpCodes.Ret);

      // Create the DynamicMethod method.
      MethodBuilder methBuilder= typBuilder.DefineMethod("DynamicMethod",
                           MethodAttributes.Public,typeof(String),null);
      ILGenerator methodIL = methBuilder.GetILGenerator();
      methodIL.Emit(OpCodes.Ldarg_0);
      methodIL.Emit(OpCodes.Ldfld, fldBuilder);
      methodIL.Emit(OpCodes.Ret);

      Console.WriteLine($"Name               : {fldBuilder.Name}");
      Console.WriteLine($"DeclaringType      : {fldBuilder.DeclaringType}");
      Console.WriteLine($"Type               : {fldBuilder.FieldType}");
      return typBuilder.CreateType();
   }

   public static void Main()
   {
      Type dynType = CreateType();
      try
      {
         // Create an instance of the "HelloWorld" class.
         Object helloWorld = Activator.CreateInstance(dynType, new Object[] { "HelloWorld" });
         // Invoke the "DynamicMethod" method of the "DynamicClass" class.
         Object obj  = dynType.InvokeMember("DynamicMethod",
                        BindingFlags.InvokeMethod, null, helloWorld, null);
         Console.WriteLine($"DynamicClass.DynamicMethod returned: \"{obj}\"");
      }
      catch(MethodAccessException e)
      {
         Console.WriteLine($"{e.GetType().Name}: {e.Message}");
      }
  }
}
開發者ID:.NET開發者,項目名稱:System.Reflection.Emit,代碼行數:67,代碼來源:FieldBuilder


注:本文中的System.Reflection.Emit.FieldBuilder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。