User Id :    Password :      New Member   Forgot Password  
 
Get the Way of Reflection
Description What is Reflection? How we use Reflection? What are the advantages of using Reflection? Get the way of saving memory by using Reflection.   No. of Views     876
  Rating     0
Author Rahul Saxena   Posted On     09 May 2012
Tags C#,Windows Programming    

Sample Code   Download Code

Without adding reference using classLibrary methods at runtime is done through Reflection. In Refelection we have to use an  Activator class. An Activator class is a class which creates the instance of class method at runtime. The generic terms of Reflection are:

Assembly: Which hold the dll of classLibrary.
Type: Hold the class of classLibrary.
MethodInfo: Hold the method of class.
Parameterinfo: Keep the parameter information of Method.

Here I made a class Library, in this class library I made two classes and some methods in these classes. After making this, build this class Library. On building the class Libray a dll will genarate.

Reflection Class Library

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace reflectionclass
{
    public class xx
    {
        public int sum(int a, int b)
        {
            return a + b;
        }

        public int sub(int a, int b)
        {
            return a - b;
        }

    }

    public class yy
    {
        public int mul(int a, int b)
        {
            return a * b;
        }
    }
}

After building the calssLibrary we have to make the application. Here in application we are using two ListBox, two buttons, three textBox. Firstly add the reference of System.Reflection in the application. On clicking of class Button all classes of classLibray will come in listbox1. Here we select the class of which methos we have to use in our application. The application is:

Reflection Application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;


namespace reflectionclient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Assembly asm;
        Type[] t;
        Type ty;
        MethodInfo[] m;
        MethodInfo mm;

        private void Form1_Load(object sender, EventArgs e)
        {  
            //Here wec have to load the assembly of classlibrary by givig the full path of our classLibrary.dll
            asm = Assembly.LoadFrom(@"C:\Documents and Settings\Administrator\My Documents\Rahul\Reflection\reflectionclass\reflectionclass\bin\Debug\reflectionclass.dll");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            t = asm.GetTypes();
            for (int i = 0; i < t.Length; i++)
            {
                listBox1.Items.Add(t[i].FullName);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            ty = asm.GetType(listBox1.SelectedItem.ToString());
            m = ty.GetMethods();
            for (int j = 0; j < m.Length; j++)
            {
                listBox2.Items.Add(m[j].Name);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            mm = ty.GetMethod(listBox2.SelectedItem.ToString());
            object o;
            o = Activator.CreateInstance(ty);
            object[] oo ={ Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text) };      
            //If there is no parameter in method then on the place of oo pass the null
            textBox3.Text = mm.Invoke(o, oo).ToString();
        }
    }
}

 

Figure 1.
When I click on class Button then all classes of that class library will come in list Box, which .dll I load in assembly.

 

Figure 2: When click on class Button.
After clicking on class button we saw here in list box there are  two classes are coming. If we select here any class then  all methods of that class comes in second listbox.

 

Figure 3: When select any class.
Here if I select any method and click on Method button then the result of method according to call will become in TextBox.

 

Figure 4: When select sum method and click on Method Button after entering the value in textBox1 and textBox2.



Summary:
Here we are not adding the reference of class Library of which method we will use in our application.If we add the reference of classLibrary then the memory wiil allocate to all classes and methods either we are using the class methods or not. But with Reflection the memory will allocate only those class library methods which we will call at runtime.

About Author

About Author Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books. Rahul Saxena
No Photo
 
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code