Assembly Browser: Browsing a .NET Assembly

We will create windows form to browse through a .Net assembly. The program lists the methods contained in the assembly selected by the user and the parameter name and parameter type for each assembly.
 
The example also illustrates the use of the File control in helping the user to select a particular type of file.
 
Step 1: Create the User Interface
 
Create a new Windows Application Project of type Visual C#. In the default new windows form, drag and drop the following controls from the ToolBox:
 
Refer to Figure 1.Label:
  1. Change Text property to Assembly
  2. TextBox: Change Name property to txtFile
  3. Button: Change Text property to 
  4. Button: Change Text property to Reflect
  5. Label: Change Text property to Reflection
  6. TreeView: Change the Name property to AssemblyBox
  7. OpenFileDialog
AsmBrwImg1.gif
 
Figure 1: User Interface for our form
 
Step 2: Import Namespaces
 
We will need to import these namespaces for accessing the reflection classes.
  1. using System.Reflection;  
  2. using System.Reflection.Emit; 
Step 3: Allow the user to select the Assembly File
 
The user inputs in this case are a valid assembly file name and a type name. The file name should be a valid file - with the extension .dll or .exe. We use the OpenFileDialog control to get the user to select a file name of the specified type and display the selected value in the textbox adjacent to it.
 
Function to allow the user to select a file is shown below: Double click on button1 and add the following code to its OnClick event handler.
  1. private void button1_Click(object sender, System.EventArgs e) {  
  2.       openFileDialog1.CheckFileExists = true;  
  3.       openFileDialog1.CheckPathExists = true;  
  4.       openFileDialog1.Filter = "Exe files (*.exe)|*.exe|Dll files (*.dll)|*.dll|All files (*.*) *.*"  
  5.       openFileDialog1.Multiselect = false;  
  6.       openFileDialog1.ReadOnlyChecked = true;  
  7.       if (openFileDialog1.ShowDialog() == DialogResult.OK) {  
  8.             txtFile.Text = openFileDialog1.FileName;  
  9.       }  
Step 4: Code to dissect the assembly
 
Let us focus on the function that does all the work of verifying the assembly specified by the user in the form input. The user interface accepts a valid file name of type .dll or .exe and a type name. When the user clicks OK, the tree in the user interface is populated with the Fields, Properties, and Methods in the type. We start by loading the assembly and querying for all types within the assembly. Using Reflection classes, we get the information on the fields, properties, methods, and their parameters for each type found in the assembly.
 
In our sample code listing, we have included a dummy class TestClass and a dummy field nMember in the main class. For testing the sample, you can run the sample file and select the executable file created for our sample itself and see how it looks (Figure 2).
 
Comments are included in the code listing below to explain the approach at every step.
  1. private void Reflect() {  
  2.       Assembly assembly;  
  3.       Type[] types;  
  4.       int nIndex;  
  5.       AssemblyBox.BeginUpdate();  
  6.       //Clear the tree  
  7.       AssemblyBox.Nodes.Clear();  
  8.       try {  
  9.             //Load the assembly and get all the types.  
  10.             assembly = Assembly.LoadFrom(txtFile.Text);  
  11.             types = assembly.GetTypes();  
  12.       }  
  13.       //Display error if unable to create/load the assembly.  
  14.       catch (FileNotFoundException) {  
  15.             MessageBox.Show("Could not load Assembly: {0}", txtFile.Text);  
  16.             return;  
  17.       } catch (TypeLoadException) {  
  18.             MessageBox.Show("Error in loading types");  
  19.             return;  
  20.       } catch (Exception ex) {  
  21.             MessageBox.Show("Error occured" + ex.Message);  
  22.             return;  
  23.       }  
  24.       nIndex = 0;  
  25.       AssemblyBox.Nodes.Add("Assembly");  
  26.       for (int i = 0; i < types.Length; i++) {  
  27.             //Add the root node of the tree as the type name.  
  28.             AssemblyBox.Nodes[0].Nodes.Add(new TreeNode(types[i].Name));  
  29.             //Add the child node of the root with the text "Fields".  
  30.             AssemblyBox.Nodes[0].Nodes[i].Nodes.Add(new TreeNode("Fields"));  
  31.             //Fetch the Fields of the type in the specified assembly.  //Note: This will fetch only the public methods  
  32.             FieldInfo[] fields = types[i].GetFields();  
  33.             //Add the fields of the type as child nodes of the Fields node.  
  34.             if (fields == null) {  
  35.                   AssemblyBox.Nodes[0].Nodes[i].Nodes[0].Nodes.Add(new TreeNode("No Fields Found"));  
  36.             } else {  
  37.                   foreach(FieldInfo f in fields) {  
  38.                         AssemblyBox.Nodes[0].Nodes[i].Nodes[0].Nodes.Add(new TreeNode(f.Name + "(" + f.FieldType + ")"));  
  39.                   }  
  40.             }  
  41.             //Add the child node for Properties.  
  42.             ssemblyBox.Nodes[0].Nodes[i].Nodes.Add(new TreeNode("Properties"));  
  43.             //Fetch the properties of the type.  
  44.             PropertyInfo[] properties = types[i].GetProperties();  
  45.             //Add the Properties of the type in the assembly as child nodes of the Properties node in the tree.  
  46.             if (properties == null) {  
  47.                   AssemblyBox.Nodes[0].Nodes[i].Nodes[1].Nodes.Add(new TreeNode("No Properties Found"));  
  48.             } else {  
  49.                   foreach(PropertyInfo p in properties) {  
  50.                         AssemblyBox.Nodes[0].Nodes[i].Nodes[1].Nodes.Add(new TreeNode(p.Name + "(" + p.PropertyType + ")"));  
  51.                   }  
  52.             }  
  53.             //Add the Methods node in the tree.  
  54.             AssemblyBox.Nodes[0].Nodes[i].Nodes.Add(new TreeNode("Methods"));  
  55.             //Fetch the Methods of the type in the assembly.  
  56.             MethodInfo[] methods = types[i].GetMethods();  
  57.             //Add the Methods of the type in the assembly as child nodes.  
  58.             if (methods == null) {  
  59.                   AssemblyBox.Nodes[0].Nodes[i].Nodes[2].Nodes.Add(new TreeNode("No Methods Found"));  
  60.             } else {  
  61.                   nIndex = 0;  
  62.                   foreach(MethodInfo m in methods) {  
  63.                         AssemblyBox.Nodes[0].Nodes[i].Nodes[2].Nodes.Add(new TreeNode(m.Name));  
  64.                         //For each method, fetch the parameters.  
  65.                         ParameterInfo[] parameters = m.GetParameters();  
  66.                         foreach(ParameterInfo param in parameters) {  
  67.                               //Add each of the parameters in the tree.  
  68.                               AssemblyBox.Nodes[0].Nodes[i].Nodes[2].Nodes[nIndex].Nodes.Add(new TreeNode param.Name + "(" + param.ParameterType + ")"));  
  69.                   }  
  70.                   nIndex++;  
  71.             }  
  72.       }  
  73.       AssemblyBox.EndUpdate();  
  74. }  
AsmBrwImg2.gif
 
Figure 2: The sample in action