在类中遍历属性并分配值(VB.Net)

时间:2019-05-22 14:35:45

标签: visual-studio vb.net

我当前的编码设置如下:

Public Class ListValue
   Public Property type As String
   Public Property values As List(Of Long)
End Class

Public Class DataStructures
   Public Property ListA as listValue
   Public Property ListB as listValue
   Public Property ListC as listValue
   Public Property ListD as listValue

   Public Property dataType as string
   Public Property otherData as long

End Class

Public Class Form1
   ...
   ...

   Private Sub addNumberToLists(number as Integer)

     '[Loop through all the lists and add the number to them]

   End Sub

End Class

基于上述内容,我希望'addNumberToLists'子遍历'DataStructures'类的所有属性(如果它是listValue类型)以及该类型内的值列表编号。

例如:

For each list as listValue in DataStructures
    list.values.add(Number)
    list.type = "Number"
Next 

环顾四周后,我发现了此方法,它确实为我提供了每个属性,但不太确定如何访问propertyInfo值,就像它是listValue类型一样:

Private Sub addNumberToLists(number as Integer)
  Dim _type as Type = DataStructures.GetType()
  Dim properties() As Reflection.PropertyInfo = _type.GetProperties()

  For Each property as Reflection.PropertyInfo in properties
    if property.GetType() = listValue

      [ADD EXTRA CODE HERE]

    end if
  Next
End Sub

0 个答案:

没有答案