我正在使用LINQ to Entities在VB.Net中生成类型化的结果集。我有一个通用的Report类如下:
Public Class Report
Public Property Name As String
Public Property SQL As String
Public Property Type As String
End Class
然后在调用范围中,我希望代码可以执行以下操作:
' create my data context
Dim context As New MyAppEntities
' get a report object out of a dropdown list
Dim rpt as Report = CType(aComboBox.SelectedItem, Report)
' create a result object based on the type stored in the object
Dim res As ObjectResult(Of rpt.Type) = context.ExecuteStoreQuery(Of rpt.Type)(rpt.SQL)
到目前为止,我发现.Net不会让我在运行时动态设置(Of Type)子句中的类型。
有谁知道怎么做?
我尝试将Report类的Type属性更改为Type类型的对象,但这也不起作用。
顺便说一句,我知道有其他方法可以完成同样的事情,但是我正在使用外部约束,这使得我现在必须使用它。无论如何,直觉上这样的事情应该有效,我想知道如何。答案 0 :(得分:1)
以下是C#中的答案:
Creating a Generic<T> type instance with a variable containing the Type
相关方法和代码:
var genericListType = typeof(List<>);
var specificListType = genericListType.MakeGenericType(typeof(double));
var list = Activator.CreateInstance(specificListType);
在这种情况下,您使用ObjectResult
代替List
。希望有所帮助。
答案 1 :(得分:0)
选项严格,你不能这样做。严格执行静态类型。使用静态类型,您必须坚持使用Object,但如果删除strict选项,则可以在vb.net中完成动态类型 请参阅this问题