c# - Get type programmatically and use it as a typeparam -


what want read in lex.db database. preferably paged pages of pre-defined size. have done following:

dbinstance database = getdatabase(); var tables = database.alltables(); foreach (var table in tables) {     string str = table.tostring();     str = str.replace("lex.db.dbtable`1[", string.empty);     str = str.replace("]", string.empty);     type t = type.gettype(str);      if (t != null)     {     var columns = database.readall<t>();     //handle columns     } } 

the problem function readall has typeparam. assumed use type typeparam, since represents class want results of.

however error:

"the type or namespace name 't' not found (are missing using directive or assembly reference?)".

so how can make actual type used typeparam instead of letter 't'?

i'm creating windows universal app windows 8.1 , windows phone 8.1

edit:

based on suggestions romkyns , stefan steinegger gave tried using reflection. have following code:

dbinstance database = dbinstance.getinstance();  system.type thistype = database.gettype(); typeinfo thistypeinfo = thistype.gettypeinfo(); methodinfo method = thistypeinfo.getdeclaredmethod("loadall");  var tables = database.alltables(); foreach (var table in tables) {     string str = table.tostring();     str = str.replace("lex.db.dbtable`1[", string.empty);     str = str.replace("]", string.empty);     type t = type.gettype(str);     if (t != null)     {         methodinfo generic = method.makegenericmethod(t);         object[] parameters = { this, null };         var columns = generic.invoke(database, parameters);          if (columns != null)         {             //handle columns         }     } } 

this works point invoke method called. @ point following exception occurs:

an exception of type 'system.reflection.targetexception' occurred in mscorlib.dll not handled in user code

additional information: object not match target type.

any clues on how resolve this?

edit ii:

the invoke method had called as:

var columns = generic.invoke(database, null); 

the way make call use reflection. <t> supposed known @ compile time, in case isn't, therefore cannot that.

such apis come overload takes type directly though. check if yours has it. might this:

database.readall(t); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -