2

i'm using linq. All my queries looks like var query = dc.GetTable<myType>().

I wish i could choose "myType" using a string parameter. I tried to create a Type object using reflection, but the compiler doesn't recognize Type objects as class definitions.

Any suggestions? Thanks

3 Answers 3

5

There's a GetTable(Type) extension method which does exactly what you are looking for:

var query = dc.GetTable(Type.GetType("namespace.type, assembly"));
2
  • It's a good aproach, but I'm designing an application in three layers. All of them use generic logic. I would like to customize the class of work by using text strings in the presentation layer, and that information is spreading to the persistence layer. Thanks
    – user253501
    Jan 18, 2010 at 21:03
  • @dalbornoz - Your persistence layer needs to know what table to load the data from. You can't avoid that.
    – user24359
    Jan 19, 2010 at 6:04
1

Why do you want that ? Using the generic method like you do now, gives you compile time checking support, whereas a string parameter not.

1

You can create an query instance as a generic Table<> object, but it won't be recognized in compile time. see Using Type objects as Type Parameters for Generics in C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.