views:

140

answers:

3

Hi,

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

+1  A: 

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

Frederik Gheysels
+5  A: 

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

var query = dc.GetTable(Type.GetType("namespace.type, assembly"));
Darin Dimitrov
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
@dalbornoz - Your persistence layer needs to know what table to load the data from. You can't avoid that.
Isaac Cambron
A: 

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#

Bryan