i have c# method need pass various lists into. type of lists different often. method in different dll , cannot know class objects contained in lists (no references, no using statements). method needs read each of members of each of items in list , build , return string based off of that.
my question how can metadata for these various objects @ compiletime / runtime? reflection that?
also, once metadata, how use use variables?
thank you
edit: using following:
public string getdata(list<user> list){ //... list<rowdata> rows = new list<rowdata>(); foreach (user item in list) { rowdata row = new rowdata(); row.id = count++; row.cell = new string[3]; row.cell[0] = item.id.tostring(); row.cell[1] = item.name; row.cell[2] = item.age.tostring(); rows.add(row); } //... return new javascriptserializer().serialize(rows.toarray());
right specific. want replace generics can pass items other "user"
.gettype()
type
, can lot of descriptions type.
you can discover members of instances have using reflection
please precise want more ?
edit : here way, try making extenstion method better
public static list<rowdata> todatatable<t>(ienumerable<t> source) { system.reflection.propertyinfo[] properties = typeof(t).getproperties(); list<rowdata> rows = new list<jqgridrow>(); foreach (var item in source) { rowdata row = new rowdata(); row.cells = new string[properties.length]; int i=0; foreach (var prop in properties) { row.cells[i] = prop.getvalue(item, null);i++; } rows.add(row); } return rows; }
Comments
Post a Comment