简体   繁体   English

从GetType获取泛型类

[英]get Generic Class from GetType

I tried to get data from database by using GetType. 我试图通过使用GetType从数据库获取数据。

Example in VB.net: VB.net中的示例:

Dim genericObject = Me.UnitOfWork.GetAll(Of GetType("MyNamespace.Student"))()

For sure this code got error. 当然,此代码有错误。 But i don't have any idea to retrieve data from database by using string of class name. 但是我不知道使用类名字符串从数据库检索数据。

My project is, i tried to create dynamic document merging. 我的项目是,我尝试创建动态文档合并。

You need to do Me.UnitOfWork.GetAll(of MyNamespace.Student)() . 您需要执行Me.UnitOfWork.GetAll(of MyNamespace.Student)() If the class isn't known at compile time then you'll need to invoke it via reflection 如果在编译时不知道该类,则需要通过反射来调用它

Dim method As MethodInfo = Me.UnitOfWork.GetType().GetMethod("GetAll")
Dim generic As MethodInfo = method.MakeGenericMethod(GetType("MyNamespace.Student"))
generic.Invoke(Me.UnitOfWork, Nothing)

See this answer for more details: How do I use reflection to call a generic method? 有关更多详细信息,请参见此答案: 如何使用反射调用泛型方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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