Loading...

Reflections in C# Programming, Step by Step Tutorial

Reflection in C# Programming:

Reflection in Computer Programming is an ability to see metadata (Data about Data) for the purpose to find assemblies, modules and type information on run time. Reflection is used to see the data of any class or dll like what are the methods or function in it , what are their data type and what parameters are passed in that methods.

We will learn here how to use Reflections in C# to see the data of a dll file. The steps are given below:

  • Make a .dll File and add an interface and a class and also add methods in them with passed parameters to see that and get information about that on run time.
  • Attach that dll to a Win Form Application, If you do not know how to make or attach a dll file, read 

    Use a DLL in c#.

  • Design the Windows Form as per your need , My design look like this:

  • Now Write the following code on the back of it:

Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection; // Using namespace of dll

namespace lab4_eflection
{
    public partial class Form1 : Form
    {
        Type[] t;         // Aray of data typr Type
        MethodInfo[] m;  // Aray of data typr MethodInfo, Its name defines its work
        ParameterInfo[] p;  // Aray of data typr ParameterInfo
        ConstructorInfo[] c;  // Aray of data typr ConstructorInfo
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) // when form is loaded
        {
            Assembly as1 = Assembly.LoadFrom("task2dll.dll");   // loading assembly, task2dll.dll is the name of our dll
            t = as1.GetTypes();   // getting types of assembly in array 't'
            foreach (Type t1 in t)
            {
                combo_type.Items.Add(t1.Name);  // adding names of type in combo box
            }

        }
        private void combo_type_SelectedIndexChanged(object sender, EventArgs e)  // when combo box of type is changed
        {
            int a = combo_type.SelectedIndex;   // reading index number of selected item
            m = t[a].GetMethods();   // getting methods of the index number's type in m array
            txt_names.Text = t[a].Namespace;   // writing namespace name
            txt_base.Text = (t[a].BaseType != null) ? t[a].BaseType.Name : "No Base Type"// if t[a].BaseType != null then code after ? will run , other wise code after : will run
            c = t[a].GetConstructors();  // getting constructors in c array
            combo_methods.Items.Clear();  // clearing combox items
            foreach (MethodInfo m1 in m)
            {
                combo_methods.Items.Add(m1.Name);  // writing name of methods in combo box
            }
        }

        private void combo_methods_SelectedIndexChanged(object sender, EventArgs e)  // when combo box of method is changed
        {
            int a = combo_methods.SelectedIndex;  // you know it :)
            p = m[a].GetParameters();   // getting parameter of selected method/function
            lst_paramtr.Items.Clear(); // clearing list box of parameters
            lst_type.Items.Clear();  // clearing list box of type of parameters
            foreach (ParameterInfo p1 in p)
            {
                lst_paramtr.Items.Add(p1.Name);   // writing on list box
                lst_type.Items.Add(p1.ParameterType);
            }
        }       
    }
}


Output:


Thank You for reading this. Like this if your understood, It helps a lot to keep us motivated

0 comments:

Post a Comment

Google+

 
TOP