c# - Dynamically create an object from list of properties known at run-time -
i have results (xml format, <prop0>
, <prop1>
, ...) coming api saving database table. have mapping of props table columns. create object on fly property names coming mapping , values coming api results.
i use mapping dictionary , api results directly save values in database, insert functions (remote application) designed use list of objects.
i have explored system.dynamic.expandoobject 1 cannot add properties on fly (from, say, dictionary). currently, using following generic typed solution, attributes need present in class.
internal t fetchpopulatedobject<t>(dictionary<string, string> apitotablemapping, dictionary<string, string> apiresultskeyval, t objecttopopulate) t : new() { if (objecttopopulate == null) objecttopopulate = new t(); propertyinfo currprop; type curractualtype; string valtoset = string.empty; int valtosetnumeric = 0; object finalvaltoset = new object(); foreach (keyvaluepair<string, string> currmap in apitotablemapping) { valtoset = string.empty; currprop = (typeof(t).getproperty(currmap.value)); if (currprop == null) { log.info("could not find property table column " + currmap.value + ". moving on."); continue; } bool valresult = apiresultskeyval.trygetvalue(currmap.key, out valtoset); if (!valresult) { log.info("could not find value in api results property: " + currmap.key + "."); continue; } curractualtype = nullable.getunderlyingtype(currprop.propertytype) ?? currprop.propertytype; if (string.isnullorempty(valtoset)) { log.info("property " + currprop.name + " of " + currprop.propertytype.name + " type api attribute " + currmap.key + " returned incompatible or empty value " + valtoset + ". skipping field."); continue; } else { finalvaltoset = int32.tryparse(valtoset, out valtosetnumeric) ? convert.changetype(valtosetnumeric, curractualtype) : convert.changetype(valtoset, curractualtype); } currprop.setvalue(objecttopopulate, finalvaltoset, null); } return objecttopopulate; }
however, infeasible, since api results keep changing , adding new attributes.
can suggest how accomplish same without having have properties in class t?
other emit, can think of dynamic compiled type, example:
static type getmeatype( string mytypename, string mystringpropertyname ) { string codetocompile = "using system; public class " + mytypename + " {public string " + mystringpropertyname + " { get; set; } }"; string[] references = { "system.dll", "system.core.dll" }; compilerparameters compilerparams = new compilerparameters(); compilerparams.generateinmemory = true; compilerparams.treatwarningsaserrors = false; compilerparams.generateexecutable = false; compilerparams.compileroptions = "/optimize"; compilerparams.referencedassemblies.addrange( references ); csharpcodeprovider provider = new csharpcodeprovider(); compilerresults compile = provider.compileassemblyfromsource( compilerparams, codetocompile ); module dynamicmodule = compile.compiledassembly.getmodules()[ 0 ]; var type = dynamicmodule.gettype( mytypename ); return type; }
Comments
Post a Comment