Reflection in C#

By
Reflection is a concept through which we can load an assembly(.exe )File at run time and can get it  Meta Data. Reflection is used to view attribute information at run time.Reflection allows late binding to method and properties.
To implement this concept required classes that reside within "System.Reflection" Namespace.
There are two method to Get Meta Data from assembly(.exe)file in .Net.
  1. Using Visual Studio Command Prompt.
  2. Custom Meta Data viewer(Using Programming).
     1. ) Using Visual Studio Command Prompt:
 There are some steps to Get meta data Using Visual Studio Command Prompt.
Step :1  First open your Visual Studio Command Prompt-->Type Command ildasm -->Press Enter.

After press Enter button:-

Step :2 Now go File-->open-->Select the assembly(.exe)file from application  as shown below:
see it:
  
Step :3 Now press open then you will see as following output:-
see it:

     2. ) Custom Meta Data viewer(using programming)
In this i will implement two concept to find the Meta Data of any Assembly(.exe)file.
  • Get Meta Data of any assembly(.exe)file.
  • Get Meta Data of a specific class.
     1. Get Meta Data of any assembly(.exe)file:-
There are some steps to implement this concept.which is given below:-
Step :1 First open your visual studio-->File-->New-->project-->select Windows Forms Application-->OK-->Drag and Drop Two comboBox and Button controls as shown below.

Step :2 Now Double click on Get Meta Data Button-->Write the following codes as given below:

using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Reflection_concept
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
  Assembly asm = Assembly.LoadFile(@"D:\Fileinfo\Fileinfo class\bin\Debug/Fileinfo class.exe");
            Type[] typeinfo = asm.GetTypes();
            foreach (Type typ in typeinfo)
            {
                comboBox1.Items.Add(typ.Name);
                comboBox2.Items.Add(typ.FullName);
            }
        }
    }
}
Note:- You can modfy the path according to your file path drive.
Step :3 Now Run the program(press F5)-->Press the Get Meta Data Button.
output:



     2. Get Meta Data of a specific class
There are some steps to get the Meta Data of a specific Class assembly(.exe)file.Now first i am going to create a Student class.
Step :1 Open your visual studio-->File-->New-->Project-->select Console Application-->OK-->Write the program as given below:

using System;
namespace studentclass
{
    class Program
    {
        static void Main(string[] args)
        {
            Student st = new Student();
            st.show();
            st.display("boy");
            Console.ReadLine();
        }
        class Student
        {
             string strname;
            public void show()
            {
                Console.WriteLine("hello"+" " + strname);
            }
            public void display(string s)
            {
                Console.WriteLine("hello"+" " + s);
            }
        }
    }
}


Step :2 Now Run the program(press F5)-->your assembly(.exe) file will be created inside Student class.
see it:



Step :3 Now again open your visual studio-->File-->New-->project-->Select Windows Forms Application-->OK-->Drag and Drop ListBox and button controls as shown below:
Step :4 Now Double click on Get Meta Data Button-->Write the following codes as given below:

using System;
using System.Windows.Forms;
using System.Reflection;
namespace reflectionprogram
{
    public partial class Form1 : Form
    {
        public 
            Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
Assembly asm = Assembly.LoadFile(@"D:\studentclass\studentclass\bin\Debug/studentclass.exe");
            Type[] typeinfo = asm.GetTypes();
            foreach (Type typ in typeinfo)
            {
                if (typ.Name == "Student")
                {
                    MethodInfo[] minfo = typ.GetMethods();
                    foreach (MethodInfo m in minfo)
                    {
                        if (m.Name == "display")
                        {
                            ConstructorInfo[] cinfo = typ.GetConstructors();
                            object obj = cinfo[0].Invoke(null);
                            Object[] values = { "student" };
                            m.Invoke(obj, values);
                        }
                        if (m.Name == "show")
                        {
                            ConstructorInfo[] cinfo = typ.GetConstructors();
                            object obj = cinfo[0].Invoke(null);
                            FieldInfo[] finfo = typ.GetFields();
                            foreach (FieldInfo f in finfo)
                            {
                                if (f.Name == "strname")
                                {
                                    f.SetValue(obj, "People");
                                }
                            }
                            m.Invoke(obj, null);
                        }
                        if (m.Name == "display")
                        {
                            ParameterInfo[] pinfo = m.GetParameters();
                            foreach (ParameterInfo p in pinfo)
                            {
                         listBox1.Items.Add("parameter Name is" + " " + p.Name);
                         listBox1.Items.Add("Return type is" + " " + m.ReturnType);
                            }
                            MemberInfo[] meinfo = typ.GetMembers();
                            foreach (MemberInfo mi in meinfo)
                            {
                                listBox1.Items.Add(mi.Name);
                                listBox1.Items.Add(mi.MemberType);
                            }
                        }
                    }
                }
            }
        }
    }
}
Note:- When you want to use this code to  any other  application,remember two things.
  1. Change assembly(.exe)file path.
  2. Change Class Name and its Method Name.
Step :5 Now Run the program(press F5) and press the Get Meta Data Button
output:

Description:- In above example i have used some classes to get the Meta Data from assembly(.exe)file.
  1. Type
  2. MethodInfo
  3. ConstructorInfo
  4. FileInfo
  5. ParameterInfo
  6. MemberInfo
I have used only some classes there are other classes also,if it is required you can use in your Application.

If you want to Run this whole application without any changes then Download these whole Application from bottom and copy two Files to your D:/ Drive which aer given below:
  • FileInfo
  • studentclass
After that open Reflection Concept Application to Your Visual Studio 2010 or other and Run it.You will not face any problem.
To Get the Latest  Free Updates Subscribe
Click below for download whole application
                 Download

2 comments:

  1. how to create setup file??

    ReplyDelete
    Replies
    1. Hi DIVI !
      Read Below Link...
      http://www.msdotnet.co.in/2013/07/how-to-create-setup-fileexe-with.html

      Delete

Powered by Blogger.