简体   繁体   English

在VB.NET中的运行时强制转换类类型

[英]Cast Class Type at Runtime in VB.NET

I have looked at dozens of answers, and three of them seem really close to what I'm trying to achieve. 我已经看了几十个答案,和3 他们似乎真的接近我想要实现的。

I have a database with hundreds of views. 我有一个包含数百个视图的数据库。 I use dapper to query the results of a view. 我使用dapper查询视图的结果。 I present the results in a grid. 我将结果呈现在网格中。 This is a read-only operation. 这是只读操作。 The view results are already mapped to classes. 查看结果已映射到​​类。 Here is a typical query: 这是一个典型的查询:

queryResults = conn.Query(Of vw_EmployeeProductDetails)(queryString, Nothing)

Right now, I have a separate query for each view (each in its own function). 现在,我对每个视图都有单独的查询(每个视图都有自己的功能)。 I would prefer to pass in the queryString and a "ViewType" to a single function to get results. 我希望将queryString和“ ViewType”传递给单个函数以获取结果。

'pass Type as function parameter
Dim ViewType = GetType(vw_EmployeeProductDetails)
SelectView(queryString, ViewType)

'...

'in the generic query function:
queryResults = conn.Query(Of ViewType)(queryString, Nothing)

However, this will not compile and I receive the error "Type 'ViewType' is not defined." 但是,它将无法编译,并且会收到错误“未定义类型'ViewType'”。

I have tried creating an instance of the specific class, then passing that type. 我尝试过创建特定类的实例,然后传递该类型。 I have used Activator.CreateInstance in multiple configurations. 我已经在多种配置中使用了Activator.CreateInstance I tried using MakeGenericType . 我尝试使用MakeGenericType I tried explicitly putting the results in a list of "ViewType": 我尝试将结果明确地放在“ ViewType”列表中:

Dim ViewType = GetType(List(Of )).MakeGenericType(GetType(vw_EmployeeProductDetails))
Dim list = Activator.CreateInstance(ViewType)
list = conn.Query(queryString, Nothing)

None of the above (along with more dead-ends) worked. 以上所有方法(以及更多死胡同)均无效。 The code above got me back to square one (a generic object that can't easily be shown in a grid, even though the correct properties are all there, without throwing it back in a dataset/datatable). 上面的代码使我回到了平方(一个通用对象,即使所有正确的属性都无法轻易显示在网格中,而没有将其扔回到数据集/数据表中)。

I am reading more about generic types to see if I can get the compiler to recognize the Type in Query(Of ViewType) , but I still cannot quite grasp how to put it all together or if that approach would work (eg would all my ViewTypes inherit from the generic class?). 我正在阅读有关泛型类型的更多信息,以查看是否可以让编译器识别Query(Of ViewType)的类型,但是我仍然不太了解如何将所有类型放在一起,或者该方法是否可行(例如,我的所有ViewTypes是否都可以)从泛型类继承?)。

This code won't compile for the same reason: 出于相同原因,该代码将无法编译:

Dim ViewType= GetType(List(Of )).MakeGenericType(GetType(vw_EmployeeProductDetails))
Dim list = Activator.CreateInstance(ViewType)
Dim list2() As List(Of ViewType)

When creating list2 , ViewType is not defined. 创建list2 ,未定义ViewType I'm absolutely lost on how to define it, if it is even possible, or if I should scrap the entire approach. 如果有可能的话,或者如果我应该放弃整个方法的话,我绝对不会怎么定义它。

Can anyone offer insight on if this is possible? 谁能提供关于这是否有可能的见解? If so, how can I implement it. 如果是这样,我该如何实施。 If not, is there a suitable alternative in this situation? 如果没有,在这种情况下是否有合适的替代方法?

Your generic query function should accept a generic type parameter: 您的通用查询函数应接受通用类型参数:

 Public Function MyQueryFunction(Of T)(queryString As String) As List(Of T)
    return conn.Query(Of T)(queryString, Nothing)
 End Function

And you would call it like this: 您会这​​样称呼它:

  Dim myresults as List(Of vw_EmployeeProductDetails) = MyQueryFunction(Of vw_EmployeeProductDetails)(queryString)

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

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