Reflection in .NET


Description

Refelction is the mechanism of discovering class information solely at run time.Wondering where it would be useful? Imagine,you are in visual studio IDE (Integrated devolopment environment) and as you type "object." you would see all the methods,properties and events associated with that object.An other example would be an Object browser.The code attached here,loads all assemblies and displays each class,its contructors, methods, properties and events in tree view.The form also hase a command botton (labeled More Reflection) in whose click event I create an object of MyClass1 class based on the name of the class and invoke its methods during run time.Here is an explanation of the code with information on various classes and methods used.

I have called the name space MyReflection.Then I have declared MyClass1 and MyClass2 which are used in the click event of More reflection Button.I will talk about these classes when I come to the click event of this 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 ,arOfTypes to store all the s from an 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 name space.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 Run time application.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.(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();
//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:

PropertyInfo[] pInfo = t.GetProperties() ;
//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:

MethodInfo[] mInfo=t.GetMethods();  

Extracting Event Information:

EventInfo[] eInfo=t.GetEvents();

Once I get all this info,I am presenting it in a tree view 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 creat 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;
//
//MyClass1 and MyClass2 are used to demonstrate the some other aspects of reflection
//such as creating an instace of object at run time based on class name and invoking a method of that object
//see cmdMoreReflection_Click() for these aspects
class MyClass1
{
public MyClass1()
{
//Console.WriteLine("In Constructor of MyClass1");
MessageBox.Show("In Constructor of MyClass1");
}
public void M1()
{
//Console.WriteLine("In M1");
MessageBox.Show("In M1");
}
}
class MyClass2
{
public MyClass2()
{
//Console.WriteLine("In Constructor of MyClass2");
MessageBox.Show("In Constructor of MyClass2");
}
public void M2()
{
//Console.WriteLine("In M2");
MessageBox.Show("In M2");
}
}
//
//
/// <summary>
/// Summary description for TestReflection.
/// </summary>
public class TestReflection : System.WinForms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.Label label1;
private System.WinForms.Button cmdReflect;
private System.WinForms.TreeView tvwObjectBrowser;
public TestReflection()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
//common prefix for the path of assemblies.
//If this is not same on your computers you need to change this
String strPrefixForPath = "D:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2204\\";
//Array for holding path names for all assemblies
//If you have a new assembly Increase the size of this array
//and add the path to the array
string[] arAssemblyPath =
{
strPrefixForPath + "System.Winforms.dll",
strPrefixForPath + "System.dll"
/*strPrefixForPath + "mscorlib.dll",
strPrefixForPath + "Microsoft.ComServices.dll",
strPrefixForPath + "Microsoft.Win32.Interop.dll",
strPrefixForPath + "System.ComponentModel.Design.dll",
strPrefixForPath + "System.configuration.dll",
strPrefixForPath + "System.configuration.Design.dll",
strPrefixForPath + "System.configuration.Install.dll",
strPrefixForPath + "System.Data.dll",
strPrefixForPath + "System.Data.Design.dll",
strPrefixForPath + "System.Diagnostics.Design.dll",
strPrefixForPath + "System.Diagnostics.dll",
strPrefixForPath + "System.DirectoryServices.dll",
strPrefixForPath + "System.dll",
strPrefixForPath + "System.Drawing.dll",
strPrefixForPath + "System.Drawing.Design.dll",
strPrefixForPath + "System.Drawing.Printing.Design.dll",
strPrefixForPath + "System.IO.dll",
strPrefixForPath + "System.Management.dll",
strPrefixForPath + "System.Messaging.dll",
strPrefixForPath + "System.Net.dll",
strPrefixForPath + "System.Runtime.Remoting.dll",
strPrefixForPath + "System.Runtime.Serialization.Formatters.Soap.dll",
strPrefixForPath + "System.Security.dll",
strPrefixForPath + "System.ServiceProcess.dll",
strPrefixForPath + "System.Text.RegularExpressions.dll",
strPrefixForPath + "System.Timers.dll",
strPrefixForPath + "System.Web.dll",
strPrefixForPath + "System.Web.UI.Design.dll",
strPrefixForPath + "System.XMl.dll",
strPrefixForPath + "System.Xml.Serialization.dll" */
};
//strPrefixForPath + "System.WebServices.Design.dll",
Type[] arOfTypes;//Array of Types.This array would hold all the types from each assembly
Assembly objAssembly;//An assembly object so that each assembly could be assinged to it
//Load each assembly
foreach(string str in arAssemblyPath)
{
try //In case we dont find the essembly,capture the exception
{
objAssembly=System.Reflection.Assembly.LoadFrom(str);
arOfTypes=objAssembly.GetTypes();
//For Each type in arOfTypes get Properties,Methods and Events
foreach(Type t in arOfTypes )
{
//Add the class as one of the roots of the treeview
TreeNode tn;
tn=tvwObjectBrowser.Nodes.Add(t.FullName);
//Get Constructors
ConstructorInfo[] ctrInfo=t.GetConstructors();
TreeNode tn2;
tn2=tn.Nodes.Add("Constructors");
foreach(ConstructorInfo c in ctrInfo)
{
tn2.Nodes.Add(c.ToString());
}
//Get Properties
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());
}
//Get Methods
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());
}
//Get Events
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;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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 ();
//@this.TrayHeight = 0;
//@this.TrayLargeIcon = false;
//@this.TrayAutoArrange = true;
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[]{});
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
Application.Run(
new TestReflection());
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all