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"));
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Comments

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#

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.