Every .Net compiler produces metadata about the types defined in the modules they produce. This metadata is packaged along with the module and can be called this metadata by a mechanism called Reflection.
In the .NET world, reflection is the process of runtime type discovery. Using reflection services, we can able to programmatically obtain the same metadata information which is displayed by using ildasm.exe. For example, through reflection, we can obtain a list of all types contained within a given assembly, including the methods, fields, properties, and events defined by a given type. You can also dynamically discover the set of interfaces supported by a given type, the parameters of a method and return type of method.
The System.Reflection namespace used to access the metadata of a class or of an assembly.
Here we use the reflection to access the metadata of a class in .Net. For example we have Employee class and IEmp interface. Employee class has methods, fields, properties and it is implementing the IEmp interface as shown below.
namespace ReflectionExp
{
class Employee : IEmp
{
public int empId = 1;
public string empName = "John";
public int GetEmpId()
{
return 100;
}
public string GetEmpName()
{
return "ABC";
}
public void AddEmp(int Id)
{
int id = Id;
}
public int EmployeeId{ get; set; }
#region IEmp Members
public double GetSal(int Id)
{
return 10000;
}
#endregion
}
}
namespace ReflectionExp
{
interface IEmp
{
double GetSal(int Id);
}
}
Now access the metadata of Employee class by using different methods. To find out the method details defined in Employee class write below method in client.
static void GetMethods(Type t)
{
MethodInfo[] mi = t.GetMethods();
foreach (MethodInfo m in mi)
Console.WriteLine("Method Name: " + m.Name + " ; Return Type: " + m.ReturnType);
}
We are using MethodInfo predefined class to get the methods defined in the class.
To access properties defined in the class use below method.
static void GetProperties(Type t)
{
PropertyInfo[] PIs = t.GetProperties();
foreach (PropertyInfo pi in PIs)
Console.WriteLine("Property Name: " + pi.Name + " ; Property Type: " + pi.PropertyType);
}
We are using the PropertyInfo predefined class to know the properties defined in the class.
To access fields defined in the class use below method.
static void GetFields(Type t)
{
FieldInfo[] FIs = t.GetFields();
foreach (FieldInfo fi in FIs)
Console.WriteLine("Field Name: " + fi.Name + " ; Field Type: " + fi.FieldType);
}
We are using the FieldInfo predfined class to know the variables defined in the class.
To know what are the interfaces particular class implemented use below method.
static void GetInterfaces(Type t)
{
Type[] IFaces = t.GetInterfaces();
foreach (Type iface in IFaces)
Console.WriteLine("Implemented Interface Name: " + iface.Name);
}
The code for client to access metadata of Employee class as shown below.
namespace ReflectionExp
{
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee();
Type t = obj.GetType();
Console.WriteLine("/*Methods defined in Employee Class*/\n");
GetMethods(t);
Console.WriteLine("\n/*Properties defined in Employee Class*/\n");
GetProperties(t);
Console.WriteLine("\n/*Fields defined in Employee Class*/\n");
GetFields(t);
Console.WriteLine("\n/*Interfaces implemented by Employee Class*/\n");
GetInterfaces(t);
Console.ReadLine();
}
static void GetMethods(Type t)
{
MethodInfo[] mi = t.GetMethods();
foreach (MethodInfo m in mi)
Console.WriteLine("Method Name: " + m.Name + " ; Return Type: " + m.ReturnType);
}
static void GetProperties(Type t)
{
PropertyInfo[] PIs = t.GetProperties();
foreach (PropertyInfo pi in PIs)
Console.WriteLine("Property Name: " + pi.Name + " ; Property Type: " + pi.PropertyType);
}
static void GetFields(Type t)
{
FieldInfo[] FIs = t.GetFields();
foreach (FieldInfo fi in FIs)
Console.WriteLine("Field Name: " + fi.Name + " ; Field Type: " + fi.FieldType);
}
static void GetInterfaces(Type t)
{
Type[] IFaces = t.GetInterfaces();
foreach (Type iface in IFaces)
Console.WriteLine("Implemented Interface Name: " + iface.Name);
}
}
}
The output displays the metadata of Employee class as shown below.
Even we can create types dynamically at runtime by using System.Reflection.Emit namespace.