Mazic stick in .Net ~~~~~ Reflection

Hi all

          Today i am going to explain a very simple but very use full topic available in .net ,”REFLECTION”.

 Scenario, say you have a dll added  to your project , and in that dll reference you are having a class and you want to get all its properties and methods ..etc..

Steps:

(1)Add using System.Reflection

(2) Add the dll or class lib inyour project

(3) Create an object of that class say objClass

(4) Get the Type of that class and call the GetProperties method of Reflection as follows

  Myclasses.Myclass objClass = Myclasses.Myclass();
Type t = objClass.GetType();
PropertyInfo[] prop = t.GetProperties();
Foreach(Propery p in prop)
{
console.WriteLine(p.Name);
}

(5) Get all the Method and its parameters of a class

MethodInfo[] mi = objClass.GetMethods();
foreach (MethodInfo m in mi)
{
Console.WriteLile(“Method Name :{0}”,m.Name);
Console.WriteLine(“Parameter(s)”);
ParameterInfo[] pi = m.GetParameters();
foreach (ParameterInfo p in pi)
{
Console.WriteLine(p.Name);
}
}

 

similiarly , you can get many other usefull information using reflection.

Happy smart programming 🙂

Advertisements

2 thoughts on “Mazic stick in .Net ~~~~~ Reflection

  1. nice example but can u tell me what will be the situation where we need to get info of class, method or properties.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s