"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Monday, January 28, 2013

Reflection in c#

After a series of post regarding caches, we are into reflection now.
Here i'm posting how to invoke a method,Getting events properties,fields from a class using reflection.

For that i created a console application  ( named : ReflectionConsoleApplication ) and invoke using System.Reflection; Namespace and created a class and code goes below.

namespace ReflectionConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Getting the Type of Class
            Type Tpe = typeof(MyClass);
            // Creating an Instance of the class
            object Ins = Activator.CreateInstance(Tpe);

            //Defining Parameter
            object[] param = new object[] { 10, 5 };
            //Method Invoking and getting result back
            int result = (int)Tpe.InvokeMember("AddNUm", BindingFlags.InvokeMethod, null, Ins, param);
            Console.WriteLine(result);
            Console.ReadLine();

            //Getting Field
            System.Reflection.FieldInfo[] FiledInfo = Tpe.GetFields();
            foreach (System.Reflection.FieldInfo Finfo in FiledInfo)
                Console.WriteLine(Finfo.Name);
            Console.ReadLine();

            //Getting Properties
            System.Reflection.PropertyInfo[] PropInfo = Tpe.GetProperties();
            foreach (System.Reflection.PropertyInfo propInfo in PropInfo)
            {
                string P = propInfo.Name.ToString();
                switch (P)
                {
                    case "MyName": Console.WriteLine("MyName");
                        Console.ReadLine();
                        break;
                    case "Age": Console.WriteLine("Age");
                        Console.ReadLine();
                        break;
                    case "MCls": Console.WriteLine("MCls");
                        Console.ReadLine();
                        break;
                    default: Console.WriteLine("Default");
                        Console.ReadLine();
                        break;
                }
            }
            Console.ReadLine();

            //Getting Events
            System.Reflection.EventInfo[] Evnt = Tpe.GetEvents();
            foreach (System.Reflection.EventInfo e in Evnt)
                Console.WriteLine(e.Name);
            Console.ReadLine();
        }
    }

    public class MyClass
    {
        //Method
        public virtual int AddNUm(int N1, int N2)
        {
            int Result = N1 + N2;
            return Result;
        }

        //Fields
        public string _MyName;
        public int _Age;
        public MyClass ClsObj;

        //Properties
        public string MyName { get; set; }
        public int Age { get; set; }
        public MyClass MCls { get; set; }

        //New Events
        public event EventHandler E1;
        public event EventHandler E2;
    }
}


Output


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: