REFLECTION
REFLECTION IS AN API IN WHICH YOU CAN ADD YOUR
ASSEMBLIES, YOUR DLL FILES DYNAMICALLY, INSTEAD OF DOING ADD A REFRENCE
REFLECTION LOAD THE ASSEMBLY AT RUN TIME TO
USE THE FEATURES AND FUNCTIONALITY OF THE ASSEMBLIES, A REFLECTION PROVIDE YOU
A IMAGE OF YOUR ASSEMBLY AT A RUN TIME TO ACCESS IT’S FEATURES
THERE ARE MANY TOOLS WHICH ARE MADE
BY USING THIS REFLECTION TECHNIQUES
LIKE ILDASM
REFLECTION HELP TO READ THE METADATA OF AN
ASSEMBLY AT RUNTIME
TO USE THIS REFLECTION TECHNIQUES ADD THIS
NAMESPAC E
USING SYSTEM.REFLECTION
THERE ARE MANY CLASSES IN REFLECTION WHICH
WILL PROVIDE YOU AFTER ADDING THIS NAMESPACE
ASSEMBLY ASM=ASSEMBLY.LOAD FROM( )
IS USED TO LOAD YOUR
ASSEMBLY
ASM.GETTYPES()
IT IS USED TO GET ALL TYPES
METHOD INFO()
IS AN CLASS WHICH IS USED TO GET
THE INFORMATION RELATED TO METHODS
PARAMETERINFO( ) IS AN
CLASS TO GET INFO ABOUT PARAMETERS
ACTIVATOR
ACTIVATOR IS AN CLASS IN WHICH THERE IS AN
METHOD CREATE INSTANCE WHICH DO A WORK OF CREATING THE INSTANCE OF ANY OBJECT
OBJECT 0=ACTIVATOR.CREATEINSTANCE(T);
FIRST CREATE A DLL FILES
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public int sum(int a, int b)
{
return
a + b;
}
public int sub(int a, int b)
{
return
a - b;
}
}
class class2
{
public int div(int a, int b)
{
return
a / b;
}
}
}
OPEN YOUR APPLICATION WINDOW FORM
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; // ADD
THIS NAMESPACE
namespace reflection
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
Assembly
asm;
Type[]
tar;
Type
t;
private
void button2_Click(object
sender, EventArgs e)
{
// TO UPLOAD YOUR ANY DLL
FILE WITH THE HELP OF OPENFILEDIALOGBOX
openFileDialog1.ShowDialog();
textBox1.Text =
openFileDialog1.FileName.ToString();
}
private
void button1_Click(object
sender, EventArgs e)
{
// TO GET NAME OF ALL
CLASSES IN YOUR DLL FILE
asm = Assembly.LoadFrom(textBox1.Text);
label1.Text = asm.FullName;
tar = asm.GetTypes();
listBox1.Items.Clear();
foreach
(Type t in
tar)
{
listBox1.Items.Add(t.FullName);
}
}
MethodInfo[]
mar;
private
void listBox1_SelectedIndexChanged(object sender, EventArgs
e)
{
// TO GET ALL METHODS NAME
OF A CLASS SELECTED BY YOU IN LISTBOX1
AND SHOW THEM IN TEXTBOXES
t = asm.GetType(listBox1.SelectedItem.ToString());
mar = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
listBox2.Items.Clear();
foreach
(MethodInfo m in
mar)
{
listBox2.Items.Add(m.Name);
}
}
ParameterInfo[]
pi;
MethodInfo
m;
private
void listBox2_SelectedIndexChanged(object sender, EventArgs
e)
{
// TO GET WHOLE INFO ABOUT
PARAMETERS OF A METHOD SELECTED BY YOU FROM LIST BOX 2
m =
t.GetMethod(listBox2.SelectedItem.ToString());
pi = m.GetParameters();
label2.Text = "no. of parameters =" + pi.Length + Environment.NewLine;
label3.Text = "";
label4.Text = "";
foreach
(ParameterInfo p in
pi)
{
label3.Text += "name of parameter= " + p.Name +Environment.NewLine;
label4.Text += "type of parameter =" + p.ParameterType
+ Environment.NewLine;
}
label5.Text = "
returun type" + m.ReturnType;
}
private
void button3_Click(object
sender, EventArgs e)
{
// TO INVOKE A METHOD WHICH
ONE YOU HAVE SELECTED FROM LISTBOX2
object
o = Activator.CreateInstance(t);
if(pi.Length>0)
{
MessageBox.Show(m.Invoke(o,new object[]{100,50}).ToString());
}
else
{
m.Invoke(o,null);
}
}
}
}
ASSIGNEMENTS
WRITE DOWN A PROGRAM TO GET ALL METHODS OF A
CLASS OF A DLL AT RUN TIME . AND INVOKE THEM AND CREATE TEXTBOXES AT RUN TIME
ACCORDING TO THE PARAMETERS AT RUN TIME
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;
namespace reflection
{
public partial class Form2 : Form
{
public
Form2()
{
InitializeComponent();
}
Assembly
asm;
Type[]
tar;
Type
t;
private
void button1_Click(object
sender, EventArgs e)
{
// TO GET THE FULL NAME OF
YOUR DLL WITH THE HELP OF OPENFILEDIALOGBOX
DialogResult
ar = openFileDialog1.ShowDialog();
if
(ar == DialogResult.OK)
{
textBox1.Text =
openFileDialog1.FileName.ToString();
}
}
private
void button2_Click(object
sender, EventArgs e)
{
// TO GET ALL CLASS NAMES OF
A DLL AT RUN TIME AND THEM IN TO A LISTB0X
asm = Assembly.LoadFrom(textBox1.Text);
//label1.Text
= asm.FullName;
tar = asm.GetTypes();
listBox1.Items.Clear();
foreach
(Type t in
tar)
{
listBox1.Items.Add(t.FullName);
}
}
MethodInfo[]
mar;
private
void listBox1_SelectedIndexChanged(object sender, EventArgs
e)
{
// TO GET ALL METHODS NAME
OF A CLASS BY SELECTING FROM A LIST BOX
t =
asm.GetType(listBox1.SelectedItem.ToString());
mar = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
listBox2.Items.Clear();
foreach
(MethodInfo m in
mar)
{
listBox2.Items.Add(m.Name);
}
}
// THIS IS TO CREATE A
INVOKE METHOD AND TEXT BOXES AT RUN TIME OF A METHOD OF A CLASS OF A DLL AT RUN
TIME
MethodInfo
m;
ParameterInfo[]
pf;
Button
btn;
List<TextBox> li = new
List<TextBox>();
int x,
y;
private
void listBox2_SelectedIndexChanged(object sender, EventArgs
e)
{
btn = new
Button();
y = 40;
m =
t.GetMethod(listBox2.SelectedItem.ToString());
pf = m.GetParameters();
for
(int i = 0; i < li.Count; i++)
{
this.Controls.Remove(li[i]);
}
for
(int i = 0; i < pf.Length; i++)
{
TextBox
tx = new TextBox();
tx.Location = new Point(480, y);
this.Controls.Add(tx);
y = y + 30;
li.Add(tx);
}
btn.Location = new Point(430, y);
btn.Text = "invoke";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
void
btn_Click(object sender, EventArgs e)
{
object
o = Activator.CreateInstance(t);
if
(pf.Length > 0)
{
object[]
values = new object[pf.Length];
for
(int i = 0; i < values.Length; i++)
{
values[i] = Convert.ChangeType(li[i].Text,
pf[i].ParameterType);
}
MessageBox.Show(m.Invoke(o,
values).ToString());
}
else
{
m.Invoke(o, null);
}
}
private
void Form2_Load(object
sender, EventArgs e)
{
}
}
}
No comments:
Post a Comment