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.
- 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.
- 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
- ConstructorInfo[] ctrInfo=t.GetConstructors();
-
Extracting Properties Information
- PropertyInfo[] pInfo = t.GetProperties() ;
-
Extracting Method Information
- MethodInfo[] mInfo=t.GetMethods();
Extracting Event Information
- 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.
- 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.
- obj1=Activator.CreateInstance(t);
- 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
- namespace MyReflection {
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.WinForms;
- using System.Reflection;
-
-
-
-
- class MyClass1 {
- public MyClass1() {
-
- MessageBox.Show("In Constructor of MyClass1");
- }
- public void M1() {
-
- MessageBox.Show("In M1");
- }
- }
- class MyClass2 {
- public MyClass2() {
-
- MessageBox.Show("In Constructor of MyClass2");
- }
- public void M2() {
-
- MessageBox.Show("In M2");
- }
- }
-
-
-
-
-
- public class TestReflection: System.WinForms.Form {
-
-
-
- private System.ComponentModel.Container components;
- private System.WinForms.Label label1;
- private System.WinForms.Button cmdReflect;
- private System.WinForms.TreeView tvwObjectBrowser;
- public TestReflection() {
-
-
-
- InitializeComponent();
-
-
-
-
-
- String strPrefixForPath = "D:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2204\\";
-
-
-
- string[] arAssemblyPath = {
- strPrefixForPath + "System.Winforms.dll",
- strPrefixForPath + "System.dll"
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- };
-
- Type[] arOfTypes;
- Assembly objAssembly;
-
- foreach(string str in arAssemblyPath) {
- try
- {
- objAssembly = System.Reflection.Assembly.LoadFrom(str);
- arOfTypes = objAssembly.GetTypes();
-
- foreach(Type t in arOfTypes) {
-
- TreeNode tn;
- tn = tvwObjectBrowser.Nodes.Add(t.FullName);
-
- ConstructorInfo[] ctrInfo = t.GetConstructors();
- TreeNode tn2;
- tn2 = tn.Nodes.Add("Constructors");
- foreach(ConstructorInfo c in ctrInfo) {
- tn2.Nodes.Add(c.ToString());
- }
-
- try {
- PropertyInfo[] pInfo = t.GetProperties();
- tn2 = tn.Nodes.Add("Properties");
- foreach(PropertyInfo p in pInfo) {
- tn2.Nodes.Add(p.ToString());
- }
- } catch (Exception e) {
- tn2 = tn.Nodes.Add("Properties");
- tn2.Nodes.Add(e.ToString());
- }
-
- try {
- MethodInfo[] mInfo = t.GetMethods();
- tn2 = tn.Nodes.Add("Methods");
- foreach(MethodInfo m in mInfo) {
- tn2.Nodes.Add(m.ToString());
- }
- } catch (Exception e) {
- tn2 = tn.Nodes.Add("Methods");
- tn2.Nodes.Add(e.ToString());
- }
-
- try {
- EventInfo[] eInfo = t.GetEvents();
- tn2 = tn.Nodes.Add("Events");
- foreach(EventInfo e in eInfo) {
- tn2.Nodes.Add(e.ToString());
- }
- } catch (Exception e) {
- tn2 = tn.Nodes.Add("Events");
- tn2.Nodes.Add(e.ToString());
- }
- }
- } catch (Exception e) {
- MessageBox.Show(e.ToString());
- }
- }
- arOfTypes = null;
- objAssembly = null;
- }
-
-
-
- public override void Dispose() {
- base.Dispose();
- components.Dispose();
- }
-
-
-
-
- private void InitializeComponent() {
- this.components = new System.ComponentModel.Container();
- this.label1 = new System.WinForms.Label();
- this.cmdReflect = new System.WinForms.Button();
- this.tvwObjectBrowser = new System.WinForms.TreeView();
-
-
-
- label1.Location = new System.Drawing.Point(16, 264);
- 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";
- label1.Size = new System.Drawing.Size(224, 48);
- label1.ForeColor = System.Drawing.Color.Blue;
- label1.TabIndex = 2;
- label1.Anchor = System.WinForms.AnchorStyles.Bottom;
- cmdReflect.Location = new System.Drawing.Point(256, 272);
- cmdReflect.ForeColor = System.Drawing.Color.Blue;
- cmdReflect.Size = new System.Drawing.Size(96, 24);
- cmdReflect.TabIndex = 1;
- cmdReflect.Anchor = System.WinForms.AnchorStyles.Bottom;
- cmdReflect.Text = "More Reflection";
- cmdReflect.Click += new System.EventHandler(this.cmdReflect_Click);
- tvwObjectBrowser.Location = new System.Drawing.Point(16, 16);
- tvwObjectBrowser.Size = new System.Drawing.Size(336, 240);
- tvwObjectBrowser.TabIndex = 0;
- tvwObjectBrowser.Anchor = System.WinForms.AnchorStyles.All;
- this.Text = "Reflection";
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(368, 317);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.cmdReflect);
- this.Controls.Add(this.tvwObjectBrowser);
- }
- protected void cmdReflect_Click(object sender, System.EventArgs e) {
- Type t = Type.GetType("MyReflection.MyClass1");
- object obj1;
- obj1 = Activator.CreateInstance(t);
- obj1.GetType().InvokeMember("M1", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj1, new object[] {});
- Type t2 = Type.GetType("MyReflection.MyClass2");
- object obj2;
- obj2 = Activator.CreateInstance(t2);
- obj2.GetType().InvokeMember("M2", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj2, new object[] {});
- }
-
-
-
- public static void Main(string[] args) {
- Application.Run(new TestReflection());
- }
- }
- }