how can i evaluate generic type in runtime in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmfarahat
    New Member
    • Jul 2010
    • 3

    how can i evaluate generic type in runtime in c#

    i need to evaluate generic type at runtime using
    Code:
    Type.GetType()

    Code:
    Type t  = Type.GetType("className");
    Table<t> table = dataContext.GetTable<t>();
    any ideas ?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You've got a couple options...

    The first, and easiest, is to just make your table of a base type. Maybe you've got three classes... Line, Circle, and Rectangle, if they all inherit from the base type of Primitive, you can create your table of that type and away you go. If you need anything, you can use object. Type information is still available to you by calling GetType() on an object in the list.

    For something a little more involved, I believe you can use reflection for this. I don't know how off the top of my head, but I'm reasonably sure you can find some information here, at a handy-dandy tutorial for plugins in C#.



    I think you might still need some kind of base type (or interface, in this case), but maybe this is more what you're looking for? There might be another way to do exactly what you're looking for, but I don't know it. Perhaps someone else will.

    Good luck!

    Comment

    • mmfarahat
      New Member
      • Jul 2010
      • 3

      #3
      @GrayTexmo : thnx for ur reply

      i'll explain what i want to do exactly

      "i have n number of identical Tables in DB (identical in structure) , will be mapped to n class in DataModel i want to create a generic solution to work with all of them is that possible ?"

      thnx in advance

      Comment

      Working...