Monday, 30 January 2017

REFLECTIONS IN C#

REFLECTIONS IN C#


"is the ability of inspecting assemblies' metadata at runtime. it is used to find all types in an  assembly and /or dynamically invoke methods in an assembly."


Uses of reflection:

1. When you drag and drop a button on a win forms or asp.net application. The properties window uses reflection to show all the properties of the Button class. So, reflection is extensivley used by IDE or a UI designers.


2. Late binding can be achieved by using reflection. You can use reflection to dynamically create an instance of a type, about which we don't have any information at compile time. So, reflection enables you to use code that is not available at compile time.

3. Consider an example where we have two alternate implementations of an interface. You want to allow the user to pick one or the other using a config file. With reflection, you can simply read the name of the class whose implementation you want to use from the config file, and instantiate an instance of that class. This is another example for late binding using reflection.

--------------------------------------------------------------------------------------------------------------------------
using System;
using System.Reflection;

namespace ConsoleApplication4
{
    public class MainClass
    {
       
        private static void Main()
        {    
            Type T = Type.GetType("ConsoleApplication4.Customer");
            Console.WriteLine("Full Name = {0}", T.FullName);
            Console.ReadLine();
            Console.WriteLine("Just the Name = {0}", T.Name);
            Console.WriteLine("Just the Namespace = {0}", T.Namespace);

            Console.ReadLine();

            Console.WriteLine("Properties in Customer");
            PropertyInfo[] properties = T.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                Console.WriteLine(property.PropertyType.Name + " " + property.Name);
                Console.ReadLine();
            }

            Console.ReadLine();

            Console.WriteLine("methods in Customer class");
            MethodInfo[] methods = T.GetMethods();
            foreach (MemberInfo method in methods)
            {
                Console.WriteLine(method.ReflectedType.Name + " " + method.Name);
                Console.ReadLine();
            }


            Console.ReadLine();

            Console.WriteLine("Constructor in Customer class");
            ConstructorInfo[] constructors = T.GetConstructors();
            foreach (ConstructorInfo constructor in constructors)
            {
                Console.WriteLine(constructor.Name);
                Console.ReadLine();
            }
        }
    }
}
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Customer(int ID, string Name)
        {
            this.Id = ID;
            this.Name = Name;
        }
        public Customer()
        {
            this.Id = -1;
            this.Name = string.Empty;

        }
        public void PrintID()
        {
            Console.WriteLine("ID= {0}", Id);
            Console.ReadLine();
        }
        public void PrintName()
        {
            Console.WriteLine("Name ={0}", Name);
            Console.ReadLine();
        }
    }



==============================================























No comments:

Post a Comment

Note: only a member of this blog may post a comment.