如何在vb.net中迭代对象的属性?

时间:2011-03-23 12:09:25

标签: .net 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

???

如何?

1 个答案:

答案 0 :(得分: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