Reflection in C#.Net

You should have Knowledge of assemblies (dll or exe) before continuing this Topic.
  • Reflection in dot net is used to inspects assemblies(dll or exe) metadata at runtime.
  • By using reflection we can get all the details of the types(Classes, Interfaces, Stuctures) declared in an assembly and also we can dynamically invokes methods from these assemblies's types.
  • As you know dot net framework is a collection of various assemblies, We can access metadata of these assemblies in our code at runtime.
One of real life example of using reflection is the Dot Net Integrated Development Environment (IDE),  Where while doing windows or web application, when you drag a button or any other control (These controls are nothing but the Classes defined in assemblies(dll) in the dot net framework) on to your Form/Page, you can see various Properties/Events of that control on right side(by default). Those properties/Events gets displayed using reflection.

Lets see example of using reflection below.

+
Code
using System;
using System.Reflection;
 
namespace Reflection_examples
{
    class Program
    {
        static void Main(string[] args)
        {
            string TypeName = "Reflection_examples.classForReflectionEx";
            Type T = Type.GetType(TypeName);
 
            //Get proprties from class classForReflectionEx
            PropertyInfo[] propInfo = T.GetProperties();
            Console.WriteLine("Properties present in {0}", T.Name);
 
            foreach (PropertyInfo pInfo in propInfo)
            {
                Console.WriteLine("\n\t{0}.{1}", pInfo.DeclaringType.Name, pInfo.Name);
            }
 
            //Get all Methods from class classForReflectionEx
            MethodInfo[] methodInfo = T.GetMethods();
            Console.WriteLine("\nMethods present in {0}", T.Name);
 
            foreach (MethodInfo mInfo in methodInfo)
            {
                Console.WriteLine("\n\t {0} {1}.{2}", mInfo.ReturnType.ToString(),mInfo.DeclaringType.Name, mInfo.Name);
            }
 
            Console.ReadLine();
        }
    }
 
 
    public class classForReflectionEx
    {
        public int id { getset; }
        public string name { getset; }
 
        public void function1()
        {
            Console.WriteLine("Welcome to Function1");
        }
 
        public void function2()
        {
            Console.WriteLine("Welcome to Function2");
        }
    }
}

for using reflection you have to include System.Reflection namespace in your application.
In the above example, We have created a class named classForReflectionEx. Let us consider this class is present in some other assembly. Now see the code in the main method of Program class, where we used a Type class and passed the Type name(In above case we need information of class classForReflectionEx) whose information we want to getType method of  the Type class.

Now by using PropertiesInfo class, which is present in System.Reflection namespace, we get all the properties that are present in the type (in above case class classForReflectionEx). By using DeclaringType.Name we get the name of type where we declared that property.
Similarly, for getting method details, we use MethodInfo class.

Below is the output of above Sample program.

Reflection example in C#.Net

If you check the output above, you can see below methods are also there in output other than the two methods function1 and function2 that we declared in our class classForReflectionEx. When our class gets compiled that time internally our properties gets converted to get_id,set_id method and get_name and set_name method, hence these methods are reflecting in our output.

Other than these methods we have four additional methods. These methods are present in dot net base class System.Object and we all know that every type(class) in dot net gets inherited from System.Object class and hense we have these four methods for our declared class.

System.Int32 classForReflectionEx.get_id
System.Void classForReflectionEx.set_id
System.String classForReflectionEx.get_name
System.Void classForReflectionEx.set_name
System.String Object.ToString
System.Boolean Object.Equals
System.Int32 Object.GetHashCode
System.Type Object.GetType

You can take this example and try out to get the other members for the class.

No comments :