Thursday, October 6, 2011

C#.net - Reflection in C#.net

Reflection is a very powerful feature that any programming language would like to provide, because it allows us to get some information about objects in runtime. It can be used in the applications normally but this is provided for doing some advanced programming. This might be for runtime code generation (It goes through creating, compilation and execution of source code in runtime). Also it can get the names of the methods that are inside the class and constructors of that object.


Let see how to use reflection in C#.net


Step 1
Create a Console application and give the solution name as SolReflection.


Step 2
Create a sample Class in the solution,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SolReflection
{
    public class Employee
    {

        #region Constructor

        public Employee()
        {

        }

        public Employee(String FirstName, String LastName)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
        }
        #endregion

        #region Property

        public String FirstName
        {
            get;
            set;
        }

        public String LastName
        {
            get;
            set;
        }

        #endregion

        #region Methods

        public void GetData(String FirstName, String LastName)
        {
            try
            {
                this.FirstName = FirstName;
                this.LastName = LastName;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void PrintData()
        {
            try
            {
                System.Console.WriteLine(FirstName);
                System.Console.WriteLine(LastName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }
}

Step 3
Write a below code in solution,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; 

namespace SolReflection
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                // Get Type of given Class
                Type TypeObj=typeof(Employee); // Or you can use like this

               // Type TypeObj = Type.GetType("SolReflection.Employee");

                // Get Construction Names
                ConstructorInfo[] ConstructionInfoObj = TypeObj.GetConstructors();

                System.Console.WriteLine("Constructor");   
                foreach (ConstructorInfo ConstructorData in ConstructionInfoObj)
                {
                    System.Console.WriteLine(ConstructorData.ToString());   
                }
                  

                // Get Property Names

                PropertyInfo[] PropertyInfoObj = TypeObj.GetProperties();

                System.Console.WriteLine();

                System.Console.WriteLine("Property"); 

                foreach (PropertyInfo PropertyData in PropertyInfoObj)
                {
                    System.Console.WriteLine(PropertyData.ToString());   
                }

                
                // Get Methods Name

                System.Console.WriteLine();

                System.Console.WriteLine("Method");   

                MethodInfo[] MethodInfoObj = TypeObj.GetMethods();

                foreach (MethodInfo MethodData in MethodInfoObj)
                {
                    System.Console.WriteLine(MethodData.ToString());   
                }

            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);   
            }

        }
    }

   
}

The program should use the namespace System.Reflection. To get type of the object, the typeof operator can be used. There is one more method GetType(). This also can be used for retrieving the type information of a class. The Operator typeof allow us to get class name of our object and GetType() method uses to get data about objects type.

Run the Project.


Download
Download Source Code


No comments:

Post a Comment