c# - How Can I Use Class Generated Dynamic In Generic Method -
i generated class dynamically , want use class in generic method.
and if can generate class dynamically in project code or add class dll project bin
folder , access in code
// method create class var mynewclass = mytypebuilder.compileresulttype(); // needed use class in generic list<mynewclass> returnobj = getdata(); // mytypebuilder class used generate class public class mytypebuilder { public static void createnewobject() { var mytype = compileresulttype(); var myobject = activator.createinstance(mytype); } static dictionary<string, type> _props = new dictionary<string, type>() { { "id", typeof(string) }, { "name", typeof(string) }, /*{ "productlists", typeof(string[]) }*/ }; public static type compileresulttype() { typebuilder tb = gettypebuilder(); constructorbuilder constructor = tb.definedefaultconstructor(methodattributes.public | methodattributes.specialname | methodattributes.rtspecialname); // note: assuming list contains field objects fields fieldname(string) , fieldtype(type) foreach (var field in _props) createproperty(tb, field.key, field.value); type objecttype = tb.createtype(); return objecttype; } private static typebuilder gettypebuilder() { var typesignature = "mydynamictype"; var = 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); } }
try list<dynamic> returnobj = getdata();
generic type parameters resolved @ compile-time, not possible use type constructed dynamically @ runtime parameter.
you can around using dynamic, though nto sure why need runtime type builder @ if can use dynamic.
in example provided, simple var returnobj = getdata();
may need.
Comments
Post a Comment