c# - Why Reflection List<T> is not generic? -
i can't understand why fieldtype not qualified generic list. tried use solution proposed below generic type of list. when reach fieldtype.isgenerictype == false, quite surprised , don't understand happening.
my goal able use method createtable() each field in context. context should have list fields.
this information found in object:
fieldtype = {name = "list`1" fullname = "system.collections.generic.list`1[[webapplication1.models.movie, webapplication1, version=1.0.0.0, culture=neutral, publickeytoken=null]]"} reference : c# generic list <t> how type of t?
my code example:
public class mediacontext { public list<movie> movies { get; set; } public list<subtitle> subtitles { get; set; } public mediacontext() { this.movies = new list<movie>(); this.subtitles = new list<subtitle>(); } } public void createdb(object context) { type type = context.gettype(); fieldinfo[] fields = type.getfields(bindingflags.instance | bindingflags.nonpublic); foreach (fieldinfo field in fields) { type generictype = this.getgenerictype(field); methodinfo method = this.gettype().getmethod("createtable"); methodinfo generic = method.makegenericmethod(generictype); generic.invoke(this, null); } foreach (fieldinfo field in fields) { type generictype = this.getgenerictype(field); methodinfo method = this.gettype().getmethod("addkeys"); methodinfo generic = method.makegenericmethod(generictype); generic.invoke(this, null); } } private type getgenerictype(fieldinfo field) { type fieldtype = field.gettype(); type generictype = null; // believe should generic. if (fieldtype.isgenerictype && fieldtype.getgenerictypedefinition() == typeof(list<>)) { generictype = fieldtype.getgenericarguments()[0]; } else { throw new exception("an array needed"); } return generictype; } public void createtable<t>() { stringbuilder query = new stringbuilder(); type type = typeof(t); query.append(string.format("create table {0} (", naminggeneration.plurializename(type.name))); query.append(this.addfields(typeof(t).getproperties())); query.append(")"); sqlcommand command = new sqlcommand(); command.commandtext = query.tostring(); sqlexecuterequest.instance.executenonquery(command); } private void addkeys<t>() { type type = typeof(t); propertyinfo[] properties = type.getproperties(); ienumerable<propertyinfo> keyedproperties = properties.where(x => x.name.contains("id")); foreach (propertyinfo property in keyedproperties) { if (property.name == "id") { this.addprimarykey(type.name, property.name); } else if (property.name.endswith("id")) { this.addforeignkey(type.name, property.name); } } }
to fix code have it, instead of
type fieldtype = field.gettype(); you want
type fieldtype = field.fieldtype; and work. fieldinfo pass through. when call gettype(), type you're getting fieldinfo type information. fieldinfo contains information field, , type information held in fieldinfo.fieldtype.
if step through code, see behavior. debugging useful.
Comments
Post a Comment