开发者

C# Generics and Reflection

开发者 https://www.devze.com 2022-12-17 21:13 出处:网络
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 th

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 th开发者_如何学Ce compiler doesn't recognize Type objects as class definitions.

Any suggestions? Thanks


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

var query = dc.GetTable(Type.GetType("namespace.type, assembly"));


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


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#

0

精彩评论

暂无评论...
验证码 换一张
取 消