Description
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();
- //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 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.
- 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
- 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());
- }
- }
- }

Hung minhPosted Feb 22, 2020, 5:14 AM
Thanks for sharing, It's so clear and detail
Vishal PrajapatiPosted Jun 5, 2019, 4:37 AM
Good article....thanks for sharing with us.
kailash biswalPosted Dec 2, 2010, 11:06 PM
Really a nice article , if possible provide some ariticle related to ;azy object loading Thanks, kailash biswal
Naresh M PPosted Aug 27, 2010, 9:55 AM
You would need to implement ICar in the Car class, else it would not find any interfaces to print.
PriyankaPosted Feb 25, 2010, 7:59 AM
Good Article. Very Helpful. Need to know about what are PE files reffered in the article. Thanks, Priyanka
saif rizviPosted Feb 23, 2010, 6:49 AM
this is good information about reflection. i got lot of thinks from it.....
Bryian TanPosted Feb 6, 2010, 11:52 AM
Nice article. Where to download the source code? Thanks, Bryian Tan