如何动态创建类?

如何动态创建类?,第1张

如何动态创建类?

是的,您可以

System.Reflection.Emit
为此使用命名空间。如果您没有经验,这不是直截了当的,但肯定是可能的。

编辑: 此代码可能有缺陷,但是它将为您提供总体思路,并有望为实现该目标提供良好的开端

using System;using System.Reflection;using System.Reflection.Emit;namespace TypeBuilderNamespace{    public static class MyTypeBuilder    {        public static void CreateNewObject()        { var myType = CompileResultType(); var myObject = Activator.CreateInstance(myType);        }        public static Type CompileResultType()        { TypeBuilder tb = GetTypeBuilder(); ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); // NOTE: assuming your list contains Field objects with fields FieldName(string) and FieldType(Type) foreach (var field in yourListOfFields)     CreateProperty(tb, field.FieldName, field.FieldType); Type objectType = tb.CreateType(); return objectType;        }        private static TypeBuilder GetTypeBuilder()        { var typeSignature = "MyDynamicType"; var an = new AssemblyName(typeSignature); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, 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);        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存