从 GetType 获取通用 Class

get Generic Class from GetType

我尝试使用 GetType 从数据库中获取数据。

VB.net中的示例:

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

肯定是这段代码出错了。但是我不知道使用 class 名称的字符串从数据库中检索数据。

我的项目是,我尝试创建动态文档合并。

你需要做 Me.UnitOfWork.GetAll(of MyNamespace.Student)()。如果 class 在编译时未知,那么您需要通过反射

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

有关详细信息,请参阅此答案: How do I use reflection to call a generic method?