我有一個名爲ticket的VB.NET類,它有幾個類型爲'field'的公共屬性。我想有一種方法可以遍歷所有這些屬性(每個屬性都有一個),並對每個屬性執行特定的任務。我認爲也許最好的方法是創建一個列表(字段),並用該類的'field'屬性填充列表。我不知道該怎麼做是將屬性動態添加到列表中,以便在將來添加屬性時不必手動將它們輸入到列表中。有關我如何做這件事的任何想法?我嘗試搜索並找到了一些使用反射的例子,但我只能弄清楚如何得到屬性的名稱而不是屬性本身。通過類屬性迭代
這裏是一個類的實例:
Public Class ticket
Public Property location As New field
Public Property user As New field
Public Property callType As New field
Public Property dateOfCall As New field
Public Property tech As New field
Public Property description As New field
Public Property myFields As New List(Of field)
'What if field had a property of value and I wanted to increment all of the fields in this class by one
Public Sub plusOne()
For Each x As field In myFields()
x.value += 1
Next
End Sub
End Class
我將如何實現像names.add(成員)?還是我以錯誤的方式接近這個? – donL