Click here to Skip to main content
Click here to Skip to main content

Assembly Spy; A Reflection Sample

By , 30 Mar 2010
Rate this:
Please Sign up or sign in to vote.

I created this application since several years ago when I was discovering .NET for the first time. Honestly, the UI of this sample originated from a VB.NET book.  I really know neither the name of that book nor the name of its author. However, a big “Thank You” to the author and the book.

Introduction

Assembly Spy is a very nice simple application written in VB.NET that uses reflection to dynamically inspect assemblies and list the containing types and members of the selected type.

The following figure shows application’s only window loading .NET 2.0 mscorlib.dll and lists members of System.String.

Background

Here are some things that should be kept in your mind:

  • All types (classes, structures, enumerations, etc.) inherit -directly or indirectly- from System.Type, and System.Type, in turn, inherits from System.Object. Therefore, System.Type can represent any other type.
  • Reflection, as described in MSDN, is a CLR feature that allows you to obtain information about assemblies and types defined within them dynamically (on the fly).
  • All types related to reflection are grouped together inside the namespace System.Reflection in the core COR (Common Object Runtime) assembly, mscorlib.dll.
  • Reflection represents type members as objects inherit from System.Reflection.MemberInfo class. We will see how soon. 
  • Visual Studio’s Object Browser and Class Viewer (WinCV) are the most illustrative examples of tools that use reflection to obtain information about types in a given assembly.

Sample Code

It allows you to select an assembly, and it lists types defined within that assembly it in the left pane. When you select a type, it automatically lists type members in the right pane of the window. 

After loading the assembly using the shared method System.Reflection.LoadFrom(), the application retrieves an array of all types defined inside this assembly (accurately, module) and calls our core function ListModuleTypes() to list the types into the List Box:

Private Sub ListModuleTypes(ByRef tTypes() As System.Type)
    Dim tType As System.Type

    For Each tType In tTypes
        If (tType.IsPublic) Then
            lbNamespaces.Items.Add(tType.FullName)
        End If
    Next
End Sub

When a user selects a type, the code clears out members tree view and adds the members of the newly selected type. Those members are categorized into those categories:

  1. Static Fields:
    Shared fields. Retrieved using System.Type.GetFields() function.
  2. Static Properties:
    Shared properties. Retrieved using System.Type.GetProperties() function.
  3. Static Events:
    Shared events. Retrieved using System.Type.GetEvents() function.
  4. Static Methods:
    Shared methods. Retrieved using System.Type.GetMethods() function.
  5. Static Constructors:
    Shared constructors. Retrieved using System.Type.GetContructors() function.
  6. Instance Fields:
    Retrieved using System.Type.GetFields() function with BindingFlags.Instance flag set.
  7. Instance Properties:
    Retrieved using System.Type.GetProperties() function with BindingFlags.Instance flag set. 
  8. Instance Events:
    Retrieved using System.Type.GetEvents() function with BindingFlags.Instance flag set.
  9. Instance Methods:
    Retrieved using System.Type.GetMethods() function with BindingFlags.Instance flag set.
  10. Instance Constructors:
    Retrieved using System.Type.GetContructors() function with BindingFlags.Instance flag set.

Every one of our System.Type.GetXXX() functions returns an array of types inherited from System.Reflection.MemberInfo class. This allows us to polymorphically enumerate the array and work with all class members the same way. The following illustration should give you a basic understanding of how types are related to each other.

The code uses the following function add members to the members tree view:

Private Sub ListMembers(ByVal miElements() As System.Reflection.MemberInfo)
    Dim miElement As System.Reflection.MemberInfo

    For Each miElement In miElements
        tnNode = New TreeNode
        tnNode.Text = System.Convert.ToString(miElement)
        tvMembers.SelectedNode.Nodes.Add(tnNode)
    Next
End Sub

Download

The code sample and application are attached with the article. The setup file installs both the application and its source code.


Filed under: CodeProject, Geming Corporation, Reflection Tagged: .NET, CodeProject, Reflection, Samples, VB.NET

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohammad Elsheimy
Technical Writer Just Like a Magic
Egypt Egypt
Independent technical writer from Egypt born in 1991.
 
I am a student of The Holy Quranic Sciences Institute. There, we study the Holy Quranic sciences and Islamic legislation.
 
I currently run two sites:
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 30 Mar 2010
Article Copyright 2010 by Mohammad Elsheimy
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid