2012-11-09 156 views
1

我有一個名爲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 

回答

3

你想用Reflection,這只是意味着在裝配檢查類型。您可以通過System.Reflection命名空間完成此操作。

查看在VB.Net在MSDN雜誌下面的文章的反射的例子: http://msdn.microsoft.com/en-us/magazine/cc163750.aspx

遍歷該製品中的類型的成員的一個例子如下:

Dim t As Type = GetType(AcmeCorp.BusinessLogic.Customer) 
For Each member As MemberInfo In t.GetMembers 
    Console.WriteLine(member.Name) 
Next 
+0

我將如何實現像names.add(成員)?還是我以錯誤的方式接近這個? – donL

0

再次作爲以前的答案 - 你會使用反射。以List(Of T)作爲例子Add我會這樣做。

Public Class Test 
    Public Property SomeList As List(Of String) 
End Class 

然後使用下面的代碼來調用添加在List(Of String)

Dim pi As PropertyInfo = GetType(Test).GetProperty("SomeList") 
Dim mi As MethodInfo = GetType(List(Of String)).GetMethod("Add") 
Dim t As New Test() 
t.SomeList = New List(Of String) 
mi.Invoke(pi.GetValue(t, Nothing), New Object() {"Added through reflection"}) 
0

正如前面回答說,你需要使用的System.Reflection來獲取類的屬性。然後檢查屬性是你想要的類型。

希望這應該給你你想要的。如果您運行代碼,您將看到它只接受指定類型的屬性。如果您想擁有所有屬性,請刪除每個循環的where語句。

Imports System.Reflection 

Module Module1 

Sub Main() 

    ' Create a list to hold your properties 
    Dim myList As New List(Of MyProperty) 

    ' check each property for its type using the where statement below. Change integer to "Field" in your case 
    For Each el In GetType(Test).GetProperties.Where(Function(p) p.PropertyType = GetType(Integer)) 
     ' add each matching property to the list 
     myList.Add(New MyProperty With {.Name = el.Name, .GetMethod = el.GetGetMethod(), .SetMethod = el.GetSetMethod()}) 
     Console.WriteLine(el.Name & " has been added to myList") 
    Next 

    Console.Read() 
End Sub 

Public Class MyProperty 
    Public Property Name As String 
    Public Property GetMethod As MethodInfo 
    Public Property SetMethod As MethodInfo 
End Class 

Public Class Test 
    Private var1 As String 
    Private var2 As String 
    Private var3 As String 
    Private var4 As String 

    Public Property myInt1 As Integer 
    Public Property myInt2 As Integer 
    Public Property myInt3 As Integer 
    Public Property myInt4 As Integer 
End Class 
End Module 

希望幫助

+0

糾正我,如果我錯了,但這會創建一個重複對象不一樣的列表。所以如果我爲每一個修改了列表中的每個測試,我不會修改該類的實例的屬性。 – donL

+0

完全脫離了我的頭頂,但它應該起作用,因爲課堂上持有參考資料。您可以通過將上面的代碼轉儲到控制檯應用程序來嘗試。我不能做atm,否則會爲你驗證身份 –