reflection - get all fields and datatypes of a c# class iteratively -
i have huge c# classes defined many fields/variables. each variable simple datatype or list or class ....
i want dump variables , datatype iteratively going thru child classes well. there simple c# function/code this?
ps: not looking @ run time object values. extract of name , datatype enough.
rk
you try create following setup. getdata class take single parameter assembly, want scan different types. class take classes , child classes can find, fields , create structured xml file hold data.
class getdata { public getdata(assembly assembly) { var xml = new xdocument(new xelement("root")); foreach (var type in assembly.gettypes()) { var typeelement = new xelement("class", new xelement("name", type.name)); foreach (var field in type.getfields()) { typeelement.add(new xelement("field", new xelement("name", field.name), new xelement("type", field.fieldtype))); } xml.root.add(typeelement); } console.writeline(xml); xml.save("dump.xml"); } }
you can create test class inner class can sure work on kind of setup.
public class dbclass { public int id; public string name; public class dbchildclass { public int childid; public string company; } }
to types executing assembly, execute following lines of code.
class program { static void main() { new getdata(assembly.getexecutingassembly()); console.read(); } }
Comments
Post a Comment