3

如果我有清单...

dim l as List(of MyClass) = new List(of MyClass)

我想获取列表中包含的对象的类型,我该怎么做?

显而易见的答案,从我的实际实现中似乎是不可能的,就是做这样的事情......

public function GetType(byval AList as IList(of GenericType)) as System.Type
  dim lResult as system.type = nothing
  if AList.Count > 0 then lResult = AList(0).GetType
  return lResult
end function

但是如果列表是空的并且我仍然想知道它包含的类型怎么办?

4

1 回答 1

3

在MSDN上有一篇很好的文章,这里

基本上,您可以使用 GetGenericArguments() 来获取作为泛型类型参数提供的类型数组。在列表的情况下,只有一个参数,所以你会得到你需要的东西,例如

dim l as List(of MyClass) = new List(of MyClass)
dim t as Type = (l.GetGenericArguments())(0)
于 2010-08-11T23:42:24.373 回答