Make a "Type explorer"

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Make a "Type explorer"
MrAksel
Just going to show you quick how you can browse types and methods in an assembly.
Add some controls to your form:
  • 2 listviews named Listbox1 & listbox2
    1 Button named Button1, text="Refresh
    1 Button named button2, text="Load"
To start with Import System.Reflection and System.Text. Then you can copy and paste this code, its commented so you should understand it:
Code: Select all
Public Class Form1
    Dim types() As Type 'Its for browsing types

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim opf As New OpenFileDialog 'Makes a file dialog
        opf.FileName = ""
        opf.Title = "Open Assembly"
        opf.Filter = "All files|*.*"
        If opf.ShowDialog = Windows.Forms.DialogResult.OK Then 'Checks what the result is
            LoadItems(opf.FileName) 'Loads the assembly and types
        End If
    End Sub

    Private Sub LoadItems(ByVal _Assembly As String)
        Try
            Dim a As Assembly = Assembly.LoadFile(_Assembly) 'Loads the file
            types = a.GetTypes 'Gets the types
            ListBox1.Items.Clear() 'Clears items in listbox1
            For Each t As Type In types 'Loops through the types
                ListBox1.Items.Add(t.Name) 'Adds the typename
            Next
        Catch ex As Exception
            MsgBox(ex.Message) 'Throws the error message if any
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form1_Load(Nothing, Nothing) 'Refreshes the list
    End Sub

    Private Function GetParams(ByVal ParamInfo As ParameterInfo()) As String
        Dim sb As New StringBuilder 'Creates a string builder
        sb.Append("(") 'Appends a (
        For Each inf As ParameterInfo In ParamInfo 'Loops through the parameters
            sb.Append(inf.ParameterType.Name & " " & inf.Name) 'Appends the parameter name & type
        Next
        Dim res As String = sb.ToString
        If res.EndsWith(", ") Then 'checks what the result ends with
            res.Remove(res.Length - 2, 2) 'if it ends with , remove it
        End If
        res &= ")" 'append a closing bracket
        Return res 'returns the result
    End Function

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            Dim t As Type = types(ListBox1.SelectedItems(0).Index) 'gets the selected type
            ListBox2.Items.Clear() 'clears the items
            For Each m As MethodInfo In t.GetMethods() 'loops through the methods
                ListBox2.Items.Add(m.Name & GetParams(m.GetParameters)).Tag = m 'Adds the method and it parameters and sets it tag to the method info object
            Next
        Catch
        End Try
    End Sub

    Private Sub ListBox2_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.DoubleClick
        Try
            Dim item As ListViewItem = ListBox2.SelectedItems(0) 'Gets the selected item
            Dim t As MethodInfo = CType(item.Tag, MethodInfo) 'Gets the method info
            ViewForm.LoadType(t) ' Views the info
        Catch ex As Exception
            MsgBox(ex.Message) 'throws the exception if any
        End Try
    End Sub
End Class
That was one code file.
Add a new form to your project and name it 'ViewForm' its used to display the info
Add some controls:
  • Label1 Text="Name: "
    Label2 Text="Inside Type: "
    Label3 Text= "Namespace: "
    Label4 Text="Variables: "
    Label5 Text="Type: "
    Label6 Text="Pubic: "
Replace the code with this:
Code: Select all
Imports System.Reflection

Public Class ViewForm

    Friend Sub LoadType(ByVal t As MethodInfo)
        Text = t.DeclaringType.Namespace & "." & t.DeclaringType.Name & "." & t.Name 'Sets the form text
        Label1.Text = "Name: " & t.Name 'Displays the Method name
        Label2.Text = "Inside Type: " & t.DeclaringType.Name 'Displays the type it is in
        Label3.Text = "Namespace: " & t.DeclaringType.Namespace 'Displays the namespace it is in
        Label4.Text = "Variables: " & t.GetMethodBody().LocalVariables.Count 'displays the local variable count
        Label5.Text = "Type: " & t.ReturnType.Name 'Displays the ReturnType name
        Label6.Text = "Public: " & t.IsPublic.ToString().Replace("False", "No").Replace("True", "Yes") 'Displays if it is public or not
        Show() 'Shows the form
    End Sub
End Class
Thats it, the source if free after the contest is closed:
You do not have the required permissions to view the files attached to this post.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
1 post Page 1 of 1
Return to “Tutorials”