Reflection in C# 2010 with .NET 4.0
Reflection is a very important technique which is used to extract whole details of a program at run time.
In .Net 4.0 Framework, when a program is reflected it is done by extracting its metadata from its assembly(which has extension .dll) and then by using this metadata its assemblies, modules and type information can be retrieved.
We can say that,
"Reflection is the process of finding out the internal elements, such as metadata, assemblies, modules,
and types, of an application without accessing the source code."
In .Net 4.0 Framework, we can implement reflection by using classes like Assembly, Type, MethodInfo, PropertyInfo, ConstructorInfo classes which resides in System.Reflection namespace.
Here I have given a small example of reflection.
Form:
The name of all the controls are given in them itself.. (The above figure is the design mode for the form)
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 System.Collections;
namespace Reflection
{
public partial class TestingReflection : Form
{
public TestingReflection()
{
InitializeComponent();
}
//making objects of Assembly and Type class to store assembly and type details
Assembly assem;
Type[] type;
private void btnTypes_Click(object sender, EventArgs e)
{
//getting info. about all the types available in assembly
type = assem.GetTypes();
//displaying info. in the list lstTypes by using for loop
for (int ifor = 0; ifor < type.Length; ifor++)
{
lstTypes.Items.Add(type[ifor]);
}
}
private void lstTypes_SelectedIndexChanged(object sender, EventArgs e)
{
//taking array of class MethodInfo, PropertyInfo and ConstructorInfo to store info
MethodInfo []method;
PropertyInfo[] property;
ConstructorInfo[] contr;
//this integer variable is for the for loop
int ifor;
//clearing all the three lists
lstContr.Items.Clear();
lstMethods.Items.Clear();
lstProperties.Items.Clear();
//getting all the methods available in the Type (e.g., class, interface, etc.)
method = type[lstTypes.SelectedIndex].GetMethods();
//displaying info. in the list lstMethods by using for loop
for (ifor = 0; ifor < method.Length; ifor++)
{
lstMethods.Items.Add(method[ifor]);
}
//getting all the properties available in the Type (e.g., class, interface, etc.)
property = type[lstTypes.SelectedIndex].GetProperties();
//displaying info. in the list lstProperty by using for loop
for (ifor = 0; ifor < property.Length; ifor++)
{
lstProperties.Items.Add(property[ifor]);
}
//getting all the constructors available in the Type (e.g., class, interface, etc.)
contr = type[lstTypes.SelectedIndex].GetConstructors();
//displaying info. in the list lstConstructor by using for loop
for (ifor = 0; ifor < contr.Length; ifor++)
{
lstContr.Items.Add(contr[ifor]);
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// on the click of btnBrowse button showing the Open file dialog box
openFileDialog.ShowDialog();
// assigning the .dll file name to the Assembly object
assem = Assembly.LoadFile(openFileDialog.FileName);
// setting the full name of the assembly file in the textbox txtFullName
string fullName = assem.FullName;
txtFullName.Text = fullName;
}
}
}
Snap Shots
Run the application..
Select your assembly file in Open Dialog box.
From the View Type list select the Type of which information you want to get..
Reflection is a very important technique which is used to extract whole details of a program at run time.
In .Net 4.0 Framework, when a program is reflected it is done by extracting its metadata from its assembly(which has extension .dll) and then by using this metadata its assemblies, modules and type information can be retrieved.
We can say that,
"Reflection is the process of finding out the internal elements, such as metadata, assemblies, modules,
and types, of an application without accessing the source code."
In .Net 4.0 Framework, we can implement reflection by using classes like Assembly, Type, MethodInfo, PropertyInfo, ConstructorInfo classes which resides in System.Reflection namespace.
Here I have given a small example of reflection.
Form:
The name of all the controls are given in them itself.. (The above figure is the design mode for the form)
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 System.Collections;
namespace Reflection
{
public partial class TestingReflection : Form
{
public TestingReflection()
{
InitializeComponent();
}
//making objects of Assembly and Type class to store assembly and type details
Assembly assem;
Type[] type;
private void btnTypes_Click(object sender, EventArgs e)
{
//getting info. about all the types available in assembly
type = assem.GetTypes();
//displaying info. in the list lstTypes by using for loop
for (int ifor = 0; ifor < type.Length; ifor++)
{
lstTypes.Items.Add(type[ifor]);
}
}
private void lstTypes_SelectedIndexChanged(object sender, EventArgs e)
{
//taking array of class MethodInfo, PropertyInfo and ConstructorInfo to store info
MethodInfo []method;
PropertyInfo[] property;
ConstructorInfo[] contr;
//this integer variable is for the for loop
int ifor;
//clearing all the three lists
lstContr.Items.Clear();
lstMethods.Items.Clear();
lstProperties.Items.Clear();
//getting all the methods available in the Type (e.g., class, interface, etc.)
method = type[lstTypes.SelectedIndex].GetMethods();
//displaying info. in the list lstMethods by using for loop
for (ifor = 0; ifor < method.Length; ifor++)
{
lstMethods.Items.Add(method[ifor]);
}
//getting all the properties available in the Type (e.g., class, interface, etc.)
property = type[lstTypes.SelectedIndex].GetProperties();
//displaying info. in the list lstProperty by using for loop
for (ifor = 0; ifor < property.Length; ifor++)
{
lstProperties.Items.Add(property[ifor]);
}
//getting all the constructors available in the Type (e.g., class, interface, etc.)
contr = type[lstTypes.SelectedIndex].GetConstructors();
//displaying info. in the list lstConstructor by using for loop
for (ifor = 0; ifor < contr.Length; ifor++)
{
lstContr.Items.Add(contr[ifor]);
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// on the click of btnBrowse button showing the Open file dialog box
openFileDialog.ShowDialog();
// assigning the .dll file name to the Assembly object
assem = Assembly.LoadFile(openFileDialog.FileName);
// setting the full name of the assembly file in the textbox txtFullName
string fullName = assem.FullName;
txtFullName.Text = fullName;
}
}
}
Snap Shots
Run the application..
Select your assembly file in Open Dialog box.
From the View Type list select the Type of which information you want to get..
No comments:
Post a Comment