Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse reflection to list an object's properties and their values in VB .NET
DescriptionThis example shows how to use reflection to list an object's properties and their values in Visual Basic .NET. It uses a type's GetProperties method to get property information. It then iterates through the property information displaying each property's name, data type, and value.
Keywordsreflection, property, VB.NET
CategoriesVB.NET
 
The program gives its ListView control three column headers: Property, Type, and Value. It gets the Form1 type and calls its GetProperties method to get an array of PropertyInfo objects describing the properties.

For each of those objects, the code examines the GetIndexParameters array to see if the property requires parameters. If the property needs no parameters, the program uses GetValue to get the object holding the property value. The code displays the property's name, type and "" if the property has no value (the property object is Nothing if the property has the value Nothing) or the value object's string value.

If GetIndexParameters indicates that the property requires parameters, the program simply displays the property's name, type, and "".

After it has created a ListView row for each property, the code sizes the ListView's columns to fit their data and then sizes the form to fit the ListView control.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make column headers.
    lvwProperties.Columns.Clear()
    lvwProperties.Columns.Add("Property", 10, _
        HorizontalAlignment.Left)
    lvwProperties.Columns.Add("Type", 10, _
        HorizontalAlignment.Left)
    lvwProperties.Columns.Add("Value", 10, _
        HorizontalAlignment.Left)

    ' List the properties.
    ' Use the class you want to study instead of Form1.
    Dim property_value As Object
    Dim properties_info As PropertyInfo() = _
        GetType(Form1).GetProperties()
    lvwProperties.Items.Clear()
    For i As Integer = 0 To properties_info.Length - 1
        With properties_info(i)
            If .GetIndexParameters().Length = 0 Then
                property_value = .GetValue(Me, Nothing)
                If property_value Is Nothing Then
                    ListViewMakeRow(lvwProperties, _
                        .Name, _
                        .PropertyType.ToString, _
                        "<Nothing>")
                Else
                    ListViewMakeRow(lvwProperties, _
                        .Name, _
                        .PropertyType.ToString, _
                        property_value.ToString)
                End If
            Else
                ListViewMakeRow(lvwProperties, _
                    .Name, _
                    .PropertyType.ToString, _
                    "<array>")
            End If
        End With
    Next i

    ' Size the columns to fit the data.
    lvwProperties.Columns(0).Width = -2
    lvwProperties.Columns(1).Width = -2
    lvwProperties.Columns(2).Width = -2

    ' Size the form.
    Dim new_wid As Integer = 30
    For i As Integer = 0 To lvwProperties.Columns.Count - 1
        new_wid += lvwProperties.Columns(i).Width
    Next i
    Me.Size = New Size(new_wid, Me.Size.Height)
End Sub
 
Helper subroutine ListViewMakeRow makes an item and sub-items in a ListView control.
 
' Make a ListView row.
Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal _
    item_title As String, ByVal ParamArray subitem_titles() _
    As String)
    ' Make the item.
    Dim new_item As ListViewItem = lvw.Items.Add(item_title)

    ' Make the sub-items.
    For i As Integer = subitem_titles.GetLowerBound(0) To _
        subitem_titles.GetUpperBound(0)
        new_item.SubItems.Add(subitem_titles(i))
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated