How to Use Reflection in C#
Introduction:
Reflection is the process by which we get the reflection of our program. In simple words we get the meta data with the help of Reflection. Meta data contains the format of the data which is used in your program or the data types like int, float, double, string, class e.t.c.
Examples:
To explain this let us consider the following examples:
In order to use Reflection in your program you have to include the following library in your program:
1 2 3 4 5 6 7 |
Using System.Reflection; int i= 42; //In the above mentioned line I is a variable of type int which is assigned the value 42// Type type= i.GetType(); |
//In the above mentioned line Type denotes the type of data which is used in your program. Now we have made the instance of Type as type.
1 |
i.GetType(); |
In this line GetType() is a built in function which is included in the library
1 |
Using System.Reflection; |
This function GetType() takes the format of the data types which are used in your program. i.GetType(); n shows that GetType() function will take the format of data related to i and store it in type which is the instance of Type.//
1 |
Console.WriteLine(type); |
//This line will display the data type of i.
Output:
1 |
System.Int32 |
//The output is showing that the variable I has int type.//
Another Example:
1 |
Employee employee= new Employee("john", "3434-234344-3", 20, 6); |
//Now in this example Employee is the name of the class and employee is its instance. Now the line = new Employee(“john”, “3434-234344-3”, 20, 6); shows that it will save the record new Employee(“john”, “3434-234344-3”, 20, 6); in the instance employee of Employee class.//
1 |
Type type= employee.GetType(); |
//Now this line Typetype= employee.GetType(); will save the type of employee in type.//
1 |
Console.WriteLine(type); |
//After the execution of this line the following result will be displayed:
Output:
1 |
ClassActivities.Employee |
//The above mentioned line is showing that employee has the data type of class known as Employee which is in class ClassActivities.
Another Example:
To demonstrate this further let us consider another example:
First make a class of Employee having the following code:
1 2 3 4 5 6 7 8 9 10 |
public Class Employee { public int ID { get; set; } //This function is public which means that it is accessible even from outside the class in the program. int ID means that the type of the function is int(integer) whereas ID is the name of the function. { get; set; } shows that data will be set in this function by set; and it will be extracted from the function by get;. Both the get; & set; functions are built in C#. // public string Name { get; set; } public int Pay { get; set; } // Employee class details } |
Now in the main we will do the following tasks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System.Reflection; public void Main(string args[]) { Employee employee=new Employee(); Type type= employee.GetType(); //In this line we are getting the type of employee which is basically the instance of Employee class.// Console.WriteLine(type); //This line will display the data type of employee.// MemberInfo[] membInfo= type.GetMembers(); //This array MemberInfo[] will hold the data types of each member or data set of Employee class. membInfo is the instance of this class. = type.GetMembers(); will get the data types of each data set in the Employee class and save it in membInfo the instance of MemberInfo[] array. GetMembers(); is a built in function in C# which stores each single record related to a class.// foreach(MemberInfo m in membInfo) //In this loop each membInfo(data set) will be stored in MemberInfo m and the loop will iterate according to the number of membInfo available.// { Console.Write(m.Name+" : "); // m is the instance of MemberInfom member name Write(m.Name+" : "); will print the name of the Employee.// Console.Write(m.ToString()+" : "); //description Console.Write(m.MemberType); //It will display the type of the Name variable. Console.WriteLine(); } } |