هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.
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.
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.
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:
- Static Fields:
Shared fields. Retrieved using System.Type.GetFields() function.
- Static Properties:
Shared properties. Retrieved using System.Type.GetProperties() function.
- Static Events:
Shared events. Retrieved using System.Type.GetEvents() function.
- Static Methods:
Shared methods. Retrieved using System.Type.GetMethods() function.
- Static Constructors:
Shared constructors. Retrieved using System.Type.GetContructors() function.
- Instance Fields:
Retrieved using System.Type.GetFields() function with BindingFlags.Instance flag set.
- Instance Properties:
Retrieved using System.Type.GetProperties() function with BindingFlags.Instance flag set.
- Instance Events:
Retrieved using System.Type.GetEvents() function with BindingFlags.Instance flag set.
- Instance Methods:
Retrieved using System.Type.GetMethods() function with BindingFlags.Instance flag set.
- 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.
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
Code sample and application are attached with the article. The setup file installs both the application and its source code.
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.