运行未知类型的泛型列表有谁知道,如果该列表的类型不知道,直到运行时如何遍历一个泛型列表?迭代通过在VB.Net
例如,假设obj1
被传递给函数作为Object
:
Dim t As Type = obj1.GetType
If t.IsGenericType Then
Dim typeParameters() As Type = t.GetGenericArguments()
Dim typeParam As Type = typeParameters(0)
End If
如果obj
被作为List(Of String)
传递然后使用上述我可以确定一个通用的列表(t
)传递和那它的类型为String
(typeParam
)。我知道我正在做一个大的假设,那就是只有一个泛型参数,但这对于这个简单的例子来说很好。
我想知道的是,基于上述情况,我怎么做这样的事情:
For Each item As typeParam In obj1
'do something with it here
Next
甚至一些为获得obj1.Count()
一样简单。