【问题标题】:VB.Net - how can i get the type of the object contained in an ListVB.Net - 我如何获取列表中包含的对象的类型
【发布时间】:2011-03-28 16:03:22
【问题描述】:

如果我有一个清单...

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

但是如果列表是空的,我还想知道它包含的类型呢?

【问题讨论】:

    标签: vb.net ilist gettype


    【解决方案1】:

    MSDN, here 上有一篇很好的文章

    基本上,您可以使用 GetGenericArguments() 来获取作为泛型类型参数提供的类型数组。在列表的情况下,只有一个参数,因此您将获得所需的内容,例如

    dim l as List(of MyClass) = new List(of MyClass)
    dim t as Type = (l.GetGenericArguments())(0)
    

    【讨论】:

    • 感谢您的快速响应。我已经完成了这个项目——我的客户设定的典型的过于乐观的日程安排。我能够在一些 LINQ 中使用您的示例。 MSDN 文章的链接提供了更多的想法。结果很好。再次感谢您的帮助!
    • 感谢您的反馈,我的脸上露出了笑容 :) 如果您发现我的回答有帮助,您可以通过单击左侧的大勾号将其设置为已接受的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2013-11-09
    • 2020-06-10
    • 1970-01-01
    相关资源
    最近更新 更多