vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, June 14, 2012

Reflection concept in C# with example


Reflection:
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="www.dotnetcode.in";
        ///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

Typeof:

  Type objtype = typeof(test);

Gettype():

  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
        Response.Write("<br/>");
        Response.Write("Constructors:");
        foreach (ConstructorInfo cf in info)
        {
            Response.Write(cf);
        }
              // get all the methods
        Response.Write("<br/>");
        Response.Write("Methods:");
        foreach (MethodInfo mf in methods)
        {
            Response.Write(mf);
        }

        // get all the Fields
        Response.Write("<br/>");
        Response.Write("Fields:");
        foreach (FieldInfo  ff in fld)
        {
            Response.Write(ff);
        }

//To set or get the value from the static fileds
        foreach (var field in fld)
        {
            string name = field.Name;
            object obj = field.GetValue(null);
            Response.Write("<br/>");
            Response.Write(name=(string)obj);
        }

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 (www.dotnetcode.in)*******************************
        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
        Response.Write("<br/>");
        Response.Write("DLLMethods:");
        foreach (MethodInfo mf in methodsdet)
        {
            Response.Write(mf);
        }


Output:

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
Dotnet
www.dotnetcode.in
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…

0 comments:

Post a Comment