Blue Theme Orange Theme Green Theme Red Theme
 
Click Here for 3 Month Free of ASP.NET Hosting!
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Enterprise Development » Reflection in .NET

Reflection in .NET

Refelction is the mechanism of discovering class information solely at run time. Wondering where it would be useful?

Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 762
Total page views :  161841
Rating :
 4.5/5
This article has been rated :  2 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
TestReflection.zip
 
Become a Sponsor


Related EbooksTop Videos

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


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Hari Shankar
Hari Shankar has been programming for more than five years. He is MCSD C++ and MFC, VB5, SQL Server 6.5, windows architecture1 and windows architecture2 ).
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
TestReflection.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
Become a Sponsor
 Comments
Thanks by Cognito On February 3, 2007
Thank you, it was very useful.
Reply | Email | Delete | Modify | 
Very informative article by rajiv On May 22, 2007
Thanks for giving a very good overview of reflection in dotnet
Reply | Email | Delete | Modify | 
Dynamic Variable Declaration by Hiren On August 1, 2007
Can i declare variable dynamically in .net using reflection if yes please give me 2 lines code as example Regards, Hiren patel hirenp@iservegroup.net
Reply | Email | Delete | Modify | 
Spelling by Dan On October 11, 2007
Oof! You may want to run this one through a spell-checker. Noted "refelction" and "botton". Still, nice article.
Reply | Email | Delete | Modify | 
Good Articles Thanks by rinoy On August 14, 2009
thanks
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved