A Reflection Sample with Assembly spy

No.of Views996
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  10 Sep 2010 07:09:20
Tag : .NET Frameworks , Miscellaneous
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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

This article is also available in my blog, Just Like a Magic.

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.

Image Loading

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 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 inherit 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.

Image Loading

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

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

Download source files -276 kb

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 'hank You to the author and the book.

 
Sign Up to vote for this article
 
About Author
 
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Location-Egypt
Joined date-30 Jul 2009
Home Page-http://WithDotNet.net
Blog Page-http://JustLikeAMagic.com
Independent software developer, trainer, and technical writer from Egypt born in 1991
 
 
Other popularSectionarticles
    Let us start this article by a small chat between customer and developer. Scenario 1 Customer: - How’s your application performance? Subjective developer: - Well it’s speedy, it’s the best …huuh aaa ooh it’s a like rocket. Scenario 2 Customer: - How’s your application performance? Quantitative developer: - With 2 GB RAM , xyz processor and 20000 customer records the customer screen load in 20 secs. I am sure the second developer looks more promising than the first developer. In this
    Published Date : 10/May/2010
    Ask any developer which is the best place in a .NET class to clean unmanaged resources?, 70% of them will say the destructor. Although it looks the most promising place for cleanup it has a huge impact on performance and memory consumption. Writing clean up code in the destructor leads to double GC visits and thus affecting the performance multifold times. In order to validate the same we will first start with bit of theory and then we will actually see how GC algorithm performance is impacte
    Published Date : 06/May/2010
    One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET.
    Published Date : 05/May/2010
    This article will explain 6 important concepts Stack , heap , by val , by ref , boxing and unboxing. This article starts first explaining what happens internally when you declare a variable and then i
    Published Date : 02/May/2010
    Introduction Value Types in .Net Framework
    Published Date : 09/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top