简体   繁体   中英

C# Generics and Reflection

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

有一个GetTable(Type)扩展方法,它可以完全满足您的需求:

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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM