Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
XML RSS Feed
 
 
 
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleUse reflection to list an object's methods and their descriptions in VB .NET
DescriptionThis example shows how to use reflection to list an object's methods and their descriptions in Visual Basic .NET. It uses a type's GetMethods method to get method information. It then iterates through the method information displaying each method's name and description.
Keywordsreflection, method, VB.NET
CategoriesVB.NET
 
The program gives its ListView control two column headers: Method and Description. It gets the Form1 type and uses GetMethods to get an array of MethodInfo objects describing the methods.

For each of those objects, the code adds the method's name and description to the ListView.

After it has created a ListView row for each method, 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.
    lvwMethods.Columns.Clear()
    lvwMethods.Columns.Add("Method", 10, _
        HorizontalAlignment.Left)
    lvwMethods.Columns.Add("Description", 10, _
        HorizontalAlignment.Left)

    ' List the methods.
    ' Use the class you want to study instead of Form1.
    Dim methods_info As MethodInfo() = _
        GetType(Form1).GetMethods()
    lvwMethods.Items.Clear()
    For i As Integer = 0 To methods_info.Length - 1
        With methods_info(i)
            ListViewMakeRow(lvwMethods, _
                .Name, .ToString)
        End With
    Next i

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

    ' Size the form.
    Dim new_wid As Integer = _
        lvwMethods.Columns(0).Width + _
        lvwMethods.Columns(1).Width + _
        30
    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-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated