Reflection in .NET

Reflection is the mechanism of discovering class information solely at run time. Let's see how and where to use Reflection in C# and .NET.

Description


Reflection is the mechanism of discovering an assembly content at runtime including an assembly's metadata, its classes, class members, their types and scope. 
 
Wondering where it would be useful? Imagine, you are in Visual Studio IDE (Integrated Development Environment) and as you type "object.", you would see all the methods, properties and events associated with that object. Another example would be an Object browser.
 
The code attached in this article, loads all assemblies and displays each class, its constructors, methods, properties and events in a TreeView. The Form also have a command button (labeled More Reflection) that allows me to create an object of MyClass1 class based on the name of the class and invokes its methods during run time.
 
Here is an explanation of the code with information on various classes and methods used.

I have called the namespace MyReflection that has two classes, MyClass1 and MyClass2, which are used in the click event of More reflection Button.
 
Next, I have called my form as TestReflection and is inherited from System.Winforms.Form. Most of the assemblies are found in D:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2204\\. If this is not the path in your computer change it to appropriate path on your computer. I have stored all the paths of the assemblies in an array called arAssemblyPath. I have used System.Winforms.dll and System.dll and commented the rest, to speed up loading and unloading of application. I have declared an array of Types to store all the information of the assembly. I have a for loop that loads each assembly and gets its types. To do this, I had to use Assembly class in System.Reflection namespace.
 
Let us talk a little bit about this class before we move on.

System.Reflection.Assembly


It defines an assembly, which is a reusable, versionable and self describing building block of a Common Language Runtime. I will discuss here only the functions that I have used in my code.
  1. objAssembly=System.Reflection.Assembly.LoadFrom(str);  

LoadFrom function takes the path of the assembly to load and loads it and returns that Assembly.

After loading the assembly, we want to get all the types in that assembly. This class provides a function GetTypes() (It returns an array of System.Type objects in that assembly) which will do that for us and that line for the code is.

  1. arOfTypes=objAssembly.GetTypes();  

Now having touched System.Type class, Let us talk about it.

System.Type


This class represents type declarations, i.e., class types, interface types, array types, value types and enumeration types.

It is the root of all Reflection operations. It is the primary means by which we access metadata and it acts as a Gateway to Reflection API. It provides methods for obtaining information about a Type declaration, such as the constructors, properties, methods and Events.

Extracting Constructor Information 
  1. ConstructorInfo[] ctrInfo=t.GetConstructors();   
  2. //It is an object of type System.Type. This method returns an array of objects of type ConstructorInfo. This calss is part of System.Reflection Name space.  

Extracting Properties Information

  1. PropertyInfo[] pInfo = t.GetProperties() ;  
  2. //It is an object of type System.Type. This method returns an array of objects of type PropertyInfo. This calss is part of System.Reflection Name space.  

Extracting Method Information

  1. MethodInfo[] mInfo=t.GetMethods();    

Extracting Event Information

  1. EventInfo[] eInfo=t.GetEvents();  

Once I get all this info, I am presenting it in a TreeView control. This should fairly give a good idea of getting information about a Type.

Once you know all the information about a class, What do you want to do? Probably you want to create an instance of that class and execute one of its methods. I have created two classes MyClass1 and MyClass2 as part of this name space and in the click event of More reflection button I have created an instance of each of these classes and invoked its methods. I have put message boxes so that we would know that they are getting executed.

Let us look at this code.
  1. Type t= Type.GetType("MyReflection.MyClass1");  

I have passed the name of the class to static method GetType of Type class which would return me a type Object of type MyClass1. Once you get Type object, as mentioned before you could do lot of things with it. Let us look at Activator class and see what we can do with this class and Type class.

System.Activator


This class contains methods to create types of objects locally or remotely or obtain references to existing objects. I have used CreateInstance() of this class to create an instance of MyClass1. This method needs a Type object as argument to creat an instance of that type. It returns an object. Next I have used GetType() on object and then used InvokeMember() on the Type object to invoke method M1 of MyClass1.
 
Here is that piece of code.
  1. obj1=Activator.CreateInstance(t);  
  2. obj1.GetType().InvokeMember("M1",BindingFlags.Default |BindingFlags.InvokeMethod,null,obj1,new object[]{});  

What is needed to compile?

.NET SDK

How to Compile?

csc /r:System.dll /r:System.winforms.dll /r:Microsoft.win32.interop.dll
/r:System.Drawing.dll TestReflection.cs


Source Code

  1. namespace MyReflection {  
  2.     using System;  
  3.     using System.Drawing;  
  4.     using System.Collections;  
  5.     using System.ComponentModel;  
  6.     using System.WinForms;  
  7.     using System.Reflection;  
  8.     //  
  9.     //MyClass1 and MyClass2 are used to demonstrate the some other aspects of reflection  
  10.     //such as creating an instace of object at run time based on class name and invoking a method of that object  
  11.     //see cmdMoreReflection_Click() for these aspects  
  12.     class MyClass1 {  
  13.         public MyClass1() {  
  14.             //Console.WriteLine("In Constructor of MyClass1");  
  15.             MessageBox.Show("In Constructor of MyClass1");  
  16.         }  
  17.         public void M1() {  
  18.             //Console.WriteLine("In M1");   
  19.             MessageBox.Show("In M1");  
  20.         }  
  21.     }  
  22.     class MyClass2 {  
  23.         public MyClass2() {  
  24.             //Console.WriteLine("In Constructor of MyClass2");  
  25.             MessageBox.Show("In Constructor of MyClass2");  
  26.         }  
  27.         public void M2() {  
  28.             //Console.WriteLine("In M2");   
  29.             MessageBox.Show("In M2");  
  30.         }  
  31.     }  
  32.     //  
  33.     //  
  34.     /// <summary>  
  35.     /// Summary description for TestReflection.  
  36.     /// </summary>  
  37.     public class TestReflection: System.WinForms.Form {  
  38.         /// <summary>  
  39.         /// Required designer variable.  
  40.         /// </summary>  
  41.         private System.ComponentModel.Container components;  
  42.         private System.WinForms.Label label1;  
  43.         private System.WinForms.Button cmdReflect;  
  44.         private System.WinForms.TreeView tvwObjectBrowser;  
  45.         public TestReflection() {  
  46.             //  
  47.             // Required for Windows Form Designer support  
  48.             //  
  49.             InitializeComponent();  
  50.             //  
  51.             // TODO: Add any constructor code after InitializeComponent call  
  52.             //  
  53.             //common prefix for the path of assemblies.  
  54.             //If this is not same on your computers you need to change this  
  55.             String strPrefixForPath = "D:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2204\\";  
  56.             //Array for holding path names for all assemblies  
  57.             //If you have a new assembly Increase the size of this array   
  58.             //and add the path to the array  
  59.             string[] arAssemblyPath = {  
  60.                 strPrefixForPath + "System.Winforms.dll",  
  61.                 strPrefixForPath + "System.dll"  
  62.                 /*strPrefixForPath + "mscorlib.dll", 
  63.                 strPrefixForPath + "Microsoft.ComServices.dll", 
  64.                 strPrefixForPath + "Microsoft.Win32.Interop.dll", 
  65.                 strPrefixForPath + "System.ComponentModel.Design.dll", 
  66.                 strPrefixForPath + "System.configuration.dll", 
  67.                 strPrefixForPath + "System.configuration.Design.dll", 
  68.                 strPrefixForPath + "System.configuration.Install.dll", 
  69.                 strPrefixForPath + "System.Data.dll", 
  70.                 strPrefixForPath + "System.Data.Design.dll", 
  71.                 strPrefixForPath + "System.Diagnostics.Design.dll", 
  72.                 strPrefixForPath + "System.Diagnostics.dll", 
  73.                 strPrefixForPath + "System.DirectoryServices.dll", 
  74.                 strPrefixForPath + "System.dll", 
  75.                 strPrefixForPath + "System.Drawing.dll", 
  76.                 strPrefixForPath + "System.Drawing.Design.dll", 
  77.                 strPrefixForPath + "System.Drawing.Printing.Design.dll", 
  78.                 strPrefixForPath + "System.IO.dll", 
  79.                 strPrefixForPath + "System.Management.dll", 
  80.                 strPrefixForPath + "System.Messaging.dll", 
  81.                 strPrefixForPath + "System.Net.dll", 
  82.                 strPrefixForPath + "System.Runtime.Remoting.dll", 
  83.                 strPrefixForPath + "System.Runtime.Serialization.Formatters.Soap.dll", 
  84.                 strPrefixForPath + "System.Security.dll", 
  85.                 strPrefixForPath + "System.ServiceProcess.dll", 
  86.                 strPrefixForPath + "System.Text.RegularExpressions.dll", 
  87.                 strPrefixForPath + "System.Timers.dll", 
  88.                 strPrefixForPath + "System.Web.dll", 
  89.                 strPrefixForPath + "System.Web.UI.Design.dll", 
  90.                 strPrefixForPath + "System.XMl.dll", 
  91.                 strPrefixForPath + "System.Xml.Serialization.dll" */  
  92.             };  
  93.             //strPrefixForPath + "System.WebServices.Design.dll",  
  94.             Type[] arOfTypes; //Array of Types.This array would hold all the types from each assembly  
  95.             Assembly objAssembly; //An assembly object so that each assembly could be assinged to it  
  96.             //Load each assembly  
  97.             foreach(string str in arAssemblyPath) {  
  98.                 try //In case we dont find the essembly,capture the exception  
  99.                 {  
  100.                     objAssembly = System.Reflection.Assembly.LoadFrom(str);  
  101.                     arOfTypes = objAssembly.GetTypes();  
  102.                     //For Each type in arOfTypes get Properties,Methods and Events  
  103.                     foreach(Type t in arOfTypes) {  
  104.                         //Add the class as one of the roots of the treeview  
  105.                         TreeNode tn;  
  106.                         tn = tvwObjectBrowser.Nodes.Add(t.FullName);  
  107.                         //Get Constructors  
  108.                         ConstructorInfo[] ctrInfo = t.GetConstructors();  
  109.                         TreeNode tn2;  
  110.                         tn2 = tn.Nodes.Add("Constructors");  
  111.                         foreach(ConstructorInfo c in ctrInfo) {  
  112.                             tn2.Nodes.Add(c.ToString());  
  113.                         }  
  114.                         //Get Properties  
  115.                         try {  
  116.                             PropertyInfo[] pInfo = t.GetProperties();  
  117.                             tn2 = tn.Nodes.Add("Properties");  
  118.                             foreach(PropertyInfo p in pInfo) {  
  119.                                 tn2.Nodes.Add(p.ToString());  
  120.                             }  
  121.                         } catch (Exception e) {  
  122.                             tn2 = tn.Nodes.Add("Properties");  
  123.                             tn2.Nodes.Add(e.ToString());  
  124.                         }  
  125.                         //Get Methods  
  126.                         try {  
  127.                             MethodInfo[] mInfo = t.GetMethods();  
  128.                             tn2 = tn.Nodes.Add("Methods");  
  129.                             foreach(MethodInfo m in mInfo) {  
  130.                                 tn2.Nodes.Add(m.ToString());  
  131.                             }  
  132.                         } catch (Exception e) {  
  133.                             tn2 = tn.Nodes.Add("Methods");  
  134.                             tn2.Nodes.Add(e.ToString());  
  135.                         }  
  136.                         //Get Events  
  137.                         try {  
  138.                             EventInfo[] eInfo = t.GetEvents();  
  139.                             tn2 = tn.Nodes.Add("Events");  
  140.                             foreach(EventInfo e in eInfo) {  
  141.                                 tn2.Nodes.Add(e.ToString());  
  142.                             }  
  143.                         } catch (Exception e) {  
  144.                             tn2 = tn.Nodes.Add("Events");  
  145.                             tn2.Nodes.Add(e.ToString());  
  146.                         }  
  147.                     }  
  148.                 } catch (Exception e) {  
  149.                     MessageBox.Show(e.ToString());  
  150.                 }  
  151.             }  
  152.             arOfTypes = null;  
  153.             objAssembly = null;  
  154.         }  
  155.         /// <summary>  
  156.         /// Clean up any resources being used.  
  157.         /// </summary>  
  158.         public override void Dispose() {  
  159.             base.Dispose();  
  160.             components.Dispose();  
  161.         }  
  162.         /// <summary>  
  163.         /// Required method for Designer support - do not modify  
  164.         /// the contents of this method with the code editor.  
  165.         /// </summary>  
  166.         private void InitializeComponent() {  
  167.             this.components = new System.ComponentModel.Container();  
  168.             this.label1 = new System.WinForms.Label();  
  169.             this.cmdReflect = new System.WinForms.Button();  
  170.             this.tvwObjectBrowser = new System.WinForms.TreeView();  
  171.             //@this.TrayHeight = 0;  
  172.             //@this.TrayLargeIcon = false;  
  173.             //@this.TrayAutoArrange = true;  
  174.             label1.Location = new System.Drawing.Point(16, 264);  
  175.             label1.Text = "Click on this button Which Demonstartes some more aspects of reflection such as creating an object at run time based on class name and invoking a method of that object";  
  176.             label1.Size = new System.Drawing.Size(224, 48);  
  177.             label1.ForeColor = System.Drawing.Color.Blue;  
  178.             label1.TabIndex = 2;  
  179.             label1.Anchor = System.WinForms.AnchorStyles.Bottom;  
  180.             cmdReflect.Location = new System.Drawing.Point(256, 272);  
  181.             cmdReflect.ForeColor = System.Drawing.Color.Blue;  
  182.             cmdReflect.Size = new System.Drawing.Size(96, 24);  
  183.             cmdReflect.TabIndex = 1;  
  184.             cmdReflect.Anchor = System.WinForms.AnchorStyles.Bottom;  
  185.             cmdReflect.Text = "More Reflection";  
  186.             cmdReflect.Click += new System.EventHandler(this.cmdReflect_Click);  
  187.             tvwObjectBrowser.Location = new System.Drawing.Point(16, 16);  
  188.             tvwObjectBrowser.Size = new System.Drawing.Size(336, 240);  
  189.             tvwObjectBrowser.TabIndex = 0;  
  190.             tvwObjectBrowser.Anchor = System.WinForms.AnchorStyles.All;  
  191.             this.Text = "Reflection";  
  192.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);  
  193.             this.ClientSize = new System.Drawing.Size(368, 317);  
  194.             this.Controls.Add(this.label1);  
  195.             this.Controls.Add(this.cmdReflect);  
  196.             this.Controls.Add(this.tvwObjectBrowser);  
  197.         }  
  198.         protected void cmdReflect_Click(object sender, System.EventArgs e) {  
  199.             Type t = Type.GetType("MyReflection.MyClass1");  
  200.             object obj1;  
  201.             obj1 = Activator.CreateInstance(t);  
  202.             obj1.GetType().InvokeMember("M1", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj1, new object[] {});  
  203.             Type t2 = Type.GetType("MyReflection.MyClass2");  
  204.             object obj2;  
  205.             obj2 = Activator.CreateInstance(t2);  
  206.             obj2.GetType().InvokeMember("M2", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj2, new object[] {});  
  207.         }  
  208.         /// <summary>  
  209.         /// The main entry point for the application.  
  210.         /// </summary>  
  211.         public static void Main(string[] args) {  
  212.             Application.Run(new TestReflection());  
  213.         }  
  214.     }  
  215. }