Do you ever try to find out information about an object at run time? Though all of us working with object all the day but I think few of us try this. It is very useful when you want to interrogate types in the system as well as to create code on the fly.
.NET Framework provides a feature called Reflection which enables us to get information about object, fields, properties, or events contains at runtime.
Wikipedia says that "In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior".
System.Reflection namespace contains all the Reflection related classes. These classes are used to get information from any of the class under .NET framework.The Type class is the root of all reflection operations.
We can get Type objects in a number of ways:
Assembly assembly = Assembly.GetExecutingAssembly();
// Get all the types in the Assembly
Type[] assemblyType = assembly.GetType();
Module[] modules = assembly.GetModules();
Object obj = new Object();
// Get the type of an object
Type objectType = obj.GetType();
or
Type objectType = typeof(obj);
The following member methods of Type follow the same pattern:
| Type of Object Returned |
Methods |
| ConstructorInfo |
GetConostructor(), GetConostructors() |
| EventInfo |
GetEvent(), GetEvents() |
| FieldInfo |
GetField(),GetFields() |
| InterfaceInfo |
GetInterface(),GetInterfaces() |
| MemberInfo |
GetMember(),GetMembers() |
| MethodInfo |
GetMethod();GetMembers() |
| PropertyInfo |
GetProperty(),GetProperty() |
Let see an example where we will create a class, make an object of that class and then we will find out the information about that object.
Here is the Customer Class:
class Customer
{
//Public fields
public int ID;
public string Name;
public Dealer oDealer;
//Public property
public int IDProperty { get; set; }
public string StringProperty { get; set; }
public Dealer ObjectProperty { get; set; }
//Public Method
public void insertCustomer()
{ }
public void deleteCustomer()
{ }
////public events
public event EventHandler MyEvent1;
public event EventHandler MyEvent2;
}
Here is an object of Customer class:
Customer oCustomer =new Customer();
oCustomer.ID = 1002;
oCustomer.Name = "Zahid";
oCustomer.oDealer = oDelaer;
Dealer oDelaer = new Dealer();
oDelaer.ID = 1001;
oDelaer.Name = "Microsoft";
Now we can find out the information of Customer by using Type Class. I write a class Reflection.cs. Using this class we can get information about Fields, Properties, Methods, Events etc of any class.
Here is the Reflection.cs:
class Reflection
{
public void getObjectInformation(Object obj)
{
//Get the Type
System.Type oType = obj.GetType();
string objectName = oType.Name;
//info in the string of information about the object obj
string info = "Class Name:" + objectName + Environment.NewLine;
//Get all the Fields information of the object
System.Reflection.FieldInfo[] oFields = oType.GetFields();
foreach (System.Reflection.FieldInfo oField in oFields)
{
info +=oField.Name + " ("+oField.FieldType + "):" + oField.GetValue(obj) + Environment.NewLine;
}
//Get all the Property name of the object
System.Reflection.PropertyInfo[] oPropertys = oType.GetProperties();
foreach (System.Reflection.MemberInfo oProperty in oPropertys)
{
info +="Property Name:"+ oProperty.Name + Environment.NewLine;
}
//Get all the Events of the object
System.Reflection.EventInfo[] oEvents = oType.GetEvents();
foreach (System.Reflection.EventInfo oEvent in oEvents)
{
info += "Event Name:" + oEvent.Name + Environment.NewLine;
}
//Get all the Methods of the object
System.Reflection.MethodInfo[] oMethods = oType.GetMethods();
foreach (System.Reflection.MethodInfo oMethod in oMethods)
{
info += "Method Name:" + oMethod.Name + Environment.NewLine;
}
MessageBox.Show(info);
}
}
Isn’t it very easy but believe it Reflection is very powerful by using it you can dynamic generate code. I will show you how we can generate dynamic code using Reflection in next article.
I hope this article will help them who want to get information about objects at runtime. If anything didn't make sense, or you've got other questions, just leave them in the comments.
Reference:
-
-
-
Robinson, Nagel, Watson, Glaynn, Skinner, Evjen ,Professional C#,4th Edithon
-
Microsft .NET Framework 2.0, Training kit.