2011-03-23 92 views
2

我正在調試項目中一個複雜的計算對象,我想在文本框中顯示它的各種屬性和很多屬性,以使我的測試更加簡單。如何遍歷vb.net中的對象的屬性?

我可以這樣做

for each p as someKindOfProperty in MyObject1 
    debug.print(p.name & " - " & debug.print p.value) 
    textbox1.text = textbox1.text & vbcrlf & p.name & " - " & p.value 
next 

???

怎麼樣?

回答

5
Dim props As PropertyInfo() = GetType(Color).GetProperties(BindingFlags.[Static] Or BindingFlags.[Public]) 

For Each prop As PropertyInfo In props 
    Dim o As Object = prop.GetValue(Nothing, Nothing) 
    If o IsNot Nothing Then 
     textbox1.Text = Textbox1.text + Constants.vbcrlf + prop.Name + " - " + o.ToString() 
    End If 
Next 
+0

是的,思考是你的朋友在這裏。 – 2011-03-23 12:23:29

+0

謝謝。那很棒。 – Alex 2011-03-23 14:23:12