检索类的属性作为集合

时间:2012-05-26 11:05:33

标签: vb.net oop reflection collections vb.net-2010

我正在开展一个小项目并遇到一些麻烦试图保持OOP。

我有一个全局变量:

Public Stations As New Microsoft.VisualBasic.Collection()

2 classess:Station& Unit

Public Class Station

    Property name As String
    Property stype As String
    Property units As New Collection
End Class

Public Class unit
     Property name As String
     Property lbl As String
     Property ip As String
     Property utype As String
End Class

我猜你可以在这里看到层次结构:

Collection Stations -> Object Station -> Collection Units -> Object Unit

有一个代码可以从XML文件中获取数据,并根据它向上面的集合中添加对象。

但是,我没有想到如何根据Stations集合检索单位集合。 我试过这样的事情:

    Dim st = Stations.Item("The key of a specific object in the Stations collection")
    Dim stUnits = st.GetType().GetProperty("units")

但是当我试图检索stUnits集合时:

For Each unit In stUnits

它说stUnits不是集合。 我有点困惑,谢谢你们的任何帮助。

1 个答案:

答案 0 :(得分:0)

傻傻的我。 忘了将st设置为电台对象。

Dim st As Station = Stations.Item("The key of a specific object in the Stations collection")

然后,简单地说:

Dim stUnits = st.units