2017-01-21 69 views
12

在C#中,如何在運行時使用.NET Core發出新類型?我所能找到的.NET 6的所有示例在.NET核心中似乎都不起作用(它們都是以獲取當前的AppDomain開始的,而現在的AppDomain不再存在於.NET核心中)。如何在.NET Core中發佈類型

如果可能的話,我將不勝感激一個例子,涉及創建一個類型和添加一個屬性的類型。

回答

20

下面是SO張貼關於在.NET創建動態類型4.

How to dynamically create a class in C#?

而在接受的答案是隻有單一用途的AppDomain

AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); 

這裏是另一個SO張貼關於在.NET核心更換DefineDynamicAssembly功能。

Is there any replace of AssemblyBuilder.DefineDynamicAssembly in .NET Core?

這就是:

AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()),AssemblyBuilderAccess.Run); 

"System.Reflection.Emit": "4.0.1"的NuGet。

此外,TypeBuilder有所不同。 CreateType函數不再存在,而是用於創建類型,我們應該使用CreateTypeInfo。是的,它是從SO後。

CreateType missing from TypeBuilder. How to port this?

這裏正在創建的類型和添加屬性的類型的變形例(用於.NET芯)。

using System; 
using System.Collections.Generic; 
using System.Reflection.Emit; 
using System.Reflection; 

namespace ConsoleApp1 
{ 
    public class FieldDescriptor 
    { 
     public FieldDescriptor(string fieldName, Type fieldType) 
     { 
      FieldName = fieldName; 
      FieldType = fieldType; 
     } 
     public string FieldName { get; } 
     public Type FieldType { get; } 
    } 

    public static class MyTypeBuilder 
    { 
     public static object CreateNewObject() 
     { 
      var myTypeInfo = CompileResultTypeInfo(); 
      var myType = myTypeInfo.AsType(); 
      var myObject = Activator.CreateInstance(myType); 

      return myObject; 
     } 

     public static TypeInfo CompileResultTypeInfo() 
     { 
      TypeBuilder tb = GetTypeBuilder(); 
      ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); 

      var yourListOfFields = new List<FieldDescriptor>() 
      { 
       new FieldDescriptor("YourProp1",typeof(string)), 
       new FieldDescriptor("YourProp2", typeof(int)) 
      }; 
      foreach (var field in yourListOfFields) 
       CreateProperty(tb, field.FieldName, field.FieldType); 

      TypeInfo objectTypeInfo = tb.CreateTypeInfo(); 
      return objectTypeInfo; 
     } 

     private static TypeBuilder GetTypeBuilder() 
     { 
      var typeSignature = "MyDynamicType"; 
      var an = new AssemblyName(typeSignature); 
      var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run); 
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule"); 
      TypeBuilder tb = moduleBuilder.DefineType(typeSignature, 
        TypeAttributes.Public | 
        TypeAttributes.Class | 
        TypeAttributes.AutoClass | 
        TypeAttributes.AnsiClass | 
        TypeAttributes.BeforeFieldInit | 
        TypeAttributes.AutoLayout, 
        null); 
      return tb; 
     } 

     private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType) 
     { 
      FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private); 

      PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null); 
      MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes); 
      ILGenerator getIl = getPropMthdBldr.GetILGenerator(); 

      getIl.Emit(OpCodes.Ldarg_0); 
      getIl.Emit(OpCodes.Ldfld, fieldBuilder); 
      getIl.Emit(OpCodes.Ret); 

      MethodBuilder setPropMthdBldr = 
       tb.DefineMethod("set_" + propertyName, 
        MethodAttributes.Public | 
        MethodAttributes.SpecialName | 
        MethodAttributes.HideBySig, 
        null, new[] { propertyType }); 

      ILGenerator setIl = setPropMthdBldr.GetILGenerator(); 
      Label modifyProperty = setIl.DefineLabel(); 
      Label exitSet = setIl.DefineLabel(); 

      setIl.MarkLabel(modifyProperty); 
      setIl.Emit(OpCodes.Ldarg_0); 
      setIl.Emit(OpCodes.Ldarg_1); 
      setIl.Emit(OpCodes.Stfld, fieldBuilder); 

      setIl.Emit(OpCodes.Nop); 
      setIl.MarkLabel(exitSet); 
      setIl.Emit(OpCodes.Ret); 

      propertyBuilder.SetGetMethod(getPropMthdBldr); 
      propertyBuilder.SetSetMethod(setPropMthdBldr); 
     } 
    } 
} 
相關問題