welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, June 14, 2012

Reflection concept in C# with example

Reflection is the special feature in .net. By using we can easily get the information about object in the runtime.

You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

In order to use reflection we must import the namespace Sytem.Reflection.
Two operators can be used in reflection i.e.

1. Typeof-Used to get from class name of the object. (No need to create object to get the type)
2. Gettype() – Used to get the data about the object.

Initially create one sample class file

public class test
        private int counter;
        public static string name="Dotnet";
        public static string address="";
        ///int counter;
        public test()
         counter = 1;
        public test(int c)
            counter= c;
        public int add()
            return counter + 4;
        public int SUB()
            return counter - 4;
By two ways of  operator we call the class file


  Type objtype = typeof(test);


  test testObject = new test();
        Type objectType = testObject.GetType();

Now the objtype contains all the information about TEST class, objtype contains some properties like IsAbstract, IsClass, IsEnum so this type return boolean value ,so we can easily check the type.
bool th = objectType.IsAbstract;
        bool th1 = objectType.IsClass;
        bool th2 = objectType.IsGenericType;
        bool th3 = objectType.IsEnum;

By the below code, we get all the methods,constructor and fied inforamtion from the TEST class.
ConstructorInfo[] info = objectType.GetConstructors();
        MethodInfo[] methods = objectType.GetMethods();
        FieldInfo[] fld = objectType.GetFields();

        // get all the constructors
        foreach (ConstructorInfo cf in info)
              // get all the methods
        foreach (MethodInfo mf in methods)

        // get all the Fields
        foreach (FieldInfo  ff in fld)

//To set or get the value from the static fileds
        foreach (var field in fld)
            string name = field.Name;
            object obj = field.GetValue(null);

Take the class files and create the dll file for the class, the file has been compiled as assembly. In that case we use the below code to get the information using reflection assembly.

        //Simple Refelction example*******************************************
        //Released by vijay (*******************************
        System.Reflection.Assembly st = System.Reflection.Assembly.Load("test.dll");
        Type otype = st.GetType();

        MethodInfo [] methodsdet = otype.GetMethods();
        // get all the Methodinfo from dll files
        foreach (MethodInfo mf in methodsdet)


Constructors:Void .ctor()Void .ctor(Int32)
Methods:Int32 add()Int32 SUB()System.String ToString()Boolean Equals(System.Object)Int32 GetHashCode()System.Type GetType()
Fields:System.String nameSystem.String address
Top of Form
Bottom of Form

I hope I explain the reflection by giving with the best example…
If you need the sample file for the reflection mail me.i will send the sample source code.
Post your comments here…


Post a Comment