Given a string which holds the name of a class, how can I then call a generic method using that class as the type parameter in C#? -
i using createtable method in sqlite-net, takes type argument specify kind of table being created. example:
database.createtable<client>(); where client defined as:
[table("client")] public class client { [primarykey] public int clientid { get; set; } public string name { get; set; } public string type { get; set; } } would create table schema defined in client class, having clientid, name, , type columns.
i use string array, holding names of tables want create, run createtable on of classes named in array. i'm unsure on how use string type parameter in generic method.
it this:
string[] tables = new string[]{"class1","class2"}; for(int = 0; < tables.length; i++){ database.createtable<tables[i]>(); } which same thing this:
database.createtable<class1>(): database.createtable<class2>(); i've tried this:
type tabletype = type.gettype("client"); database.createtable<tabletype>(); but error says "the type or namespace name 'tabletype' not found". tables defined classes in same namespace.
thanks.
generic type arguments have actual type names. can't expressions evaluate type objects.
sqlite-net has non-generic overload of createtable should use case:
type tabletype = type.gettype("client"); database.createtable(tabletype); or
string[] tables = new[] { "class1", "class2" }; for(int = 0; < tables.length; i++) { type tabletype = type.gettype(tables[i]); database.createtable(tabletype); } in more general case, you'd have use reflection makegenericmethod invoke method type comes expression.
Comments
Post a Comment