c# - Calling a generic function when type is only known at execution time -
i have generic function looks this:
public static class excelimport { public static list<t> test<t>(string filename, string worksheetname) t : class { return new list<t>(excelimport.parse<t>(filename, worksheetname)); } ... }
if know on type of objects perform test function, can call way (for example):
list<oee.data.equipment> result = excelimport.test<oee.data.equipment>(filename.text, worksheetname.text);
but fact test can applied class of oee.data namespace , users select right class in combobox before launching test function.
i use switch link different call each combobox value means i'll have had new cases anytime i'll add classes oee.data. so, how can give type dynamically? code below not working:
list<type.gettype("oee.data.equipment")> result = excelimport.test<type.gettype("oee.data.equipment")>(filename.text, worksheetname.text);
thanks in advance!
simon
edit: in answer comment under dishold's response, here code that's behind call test method:
public static list<t> test<t>(string filename, string worksheetname) t : class { return new list<t>(excelimport.parse<t>(filename, worksheetname)); } private static ienumerable<k> parse<k>(string filename, string worksheetname) k : class { ienumerable<k> list = new list<k>(); string connectionstring = string.format("provider=microsoft.jet.oledb.4.0; data source={0};extended properties=excel 8.0;", filename); string query = string.format("select * [{0}]", worksheetname); dataset data = new dataset(); using (oledbconnection con = new oledbconnection(connectionstring)) { con.open(); oledbdataadapter adapter = new oledbdataadapter(query, con); adapter.fill(data); list = populatedata<k>(data); } return list; } private static list<t> populatedata<t>(dataset data) t : class { list<t> dtos = new list<t>(); foreach (datarow row in data.tables[0].rows) { t dto = activator.createinstance<t>(); populatefieldsfromdatarows(row, dto); dtos.add(dto); } return dtos; }
the new problem in method populatedata, because can't create instance of system.runtimetype here:
t dto = activator.createinstance<t>();
try this:
type type = type.gettype("oee.data.equipment"); methodinfo genericmethod = typeof(excelimport).getmethod("test").makegenericmethod(new type[]{type}); object result = genericmethod.invoke(null, new[]{filename.text, worksheetname.text});
update:
in example using sql database, can convert oledb. have sql database has table customer.
it has: id uniqueidentifier, name varchar(100), age int, email varchar(100).
then created class called customer:
public class customer { public guid id { get; set; } public string name { get; set; } public string email { get; set; } public int age { get; set; } }
i think know kind of result query. create class has properties matches query fields(like customer). can assign using linq. let me know if have trouble understanding code.
sqlconnection conn = new sqlconnection("data source=localhost;initial catalog=test;integrated security=true"); conn.open(); sqlcommand command = new sqlcommand("select * customer", conn); dataset dataset = new dataset(); sqldataadapter adapter = new sqldataadapter(command); adapter.fill(dataset); var = (from d in dataset.tables[0].rows.cast<datarow>() select new customer() { id = (guid)d[0], name = d[1].tostring(), age = convert.toint32(d[2]), email = d[3].tostring() }).tolist();
i hope helps,
Comments
Post a Comment