Search code examples
c#listpetapoco

Dynamic list from GetType?


 Type t = Type.GetType("classname");

I got 't' like as class classname. I need like this.

  List<t> list = db.Fetch<t>("select * from classname");

I get error:

"t" could not be found.

How can i use this?


Solution

  • Type t = Type.GetType("classname");

    • Gives you type details at run-time

    List<t> list = db.Fetch<t>("select * from classname");

    • Needs Type information at compile time, since generics are verified at compile time

    that's the reason its not working

    Your options, use:

    1. db.Fetch<dynamic>("select * from classname");

    which can be used to access all the properties of a given Type T, only point is if a property doesn't exist then with dynamic type its a runtime exception not compile time error

    If further required, you may use reflection to fill and object of Type T at run-time and where each property can be verified and filled and List can be created at run-time