Monday, June 9, 2008

.NET Reflection

Working with Reflection in VB.NET

Reflection is a very nice feature introduced in the Microsoft .NET Framework. When you compile your program code for the .NET Framework to execute it is compiled into the MSIL (Microsoft Intermediate Language) codes and this code is placed in files called assemblies. Unlike DLL’s, the previous containers for compiled codes, assemblies are self explaining. This means they can describe their content unlike COM DLL’s which need a type library and need to store their information in the system’s registry.

Now reflection is the capability to get information from assemblies about the contained types, their members, their accessibility, attributes and so more. You can programmatically access the properties of any assembly or the currently executing assembly or any type using reflection.

The Reflection related classes are contained in a namespace called System.Reflection and the main class that is used is Type. To load an external assembly you need to get the reference to that assembly using Assembly class’s Load or LoadFrom method. Then to get all the types present, you can call the GetType or GetTypes method.
In the sample application attaching below has a test class with some public and private members and used reflection on this to get information about this class. Get the reference to the Type object of this class using the GetType method which returns a reference to the Type of the class. Then enumerate on the Methods present in this class and displayed them in a ListView control.

You can change and experiment with the code to see how it works. The forcoming content reflects the information about the Methods of this type you can also get information about the Type itself, weather it is public, is this class sealed and so. Try to go through the .NET Framework SDK and you’ll see a lot of useful members of the Type class.

Introduction:
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 there is a class Library, in this class library there are two classes and some methods in these classes. After making this, build this class Library. On building the class Library a dll will genarate.

Reflection Class Library

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Namespace reflectionclass
Public Class xx
Public Function sum(ByVal a As Integer, ByVal b As Integer) As Integer
Return a + b
End Function
Public Function [sub](ByVal a As Integer, ByVal b As Integer) As Integer
Return a - b
End Function
End Class
Public Class yy
Public Function mul(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function
End Class
End Namespace
After building the calssLibrary we have to make the application. Here in application we are using two ListBox, two buttons, and 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 methods we have to use in our application. The application is:

Reflection Application:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Reflection
Namespace reflectionclient
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private asm As System.Reflection.Assembly
Private t As Type()
Private ty As Type
Private m As MethodInfo()
Private mm As MethodInfo
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
'Here wec have to load the assembly of classlibrary by givig the full path of our classLibrary.dll
asm = System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\Administrator\My Documents\Rahul\Reflection\reflectionclass\reflectionclass\bin\Debug\reflectionclass.dll")
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
t = asm.GetTypes()
For i As Integer = 0 To t.Length - 1
listBox1.Items.Add(t(i).FullName)
Next i
End Sub
Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
listBox2.Items.Clear()
ty = asm.GetType(listBox1.SelectedItem.ToString())
m = ty.GetMethods()
For j As Integer = 0 To m.Length - 1
listBox2.Items.Add(m(j).Name)
Next j
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
mm = ty.GetMethod(listBox2.SelectedItem.ToString())
Dim o As Object
o = Activator.CreateInstance(ty)
Dim oo As Object() = {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()
End Sub
End Class
End Namespace



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.

Labels: ,

1 Comments:

At April 11, 2011 at 8:25 PM , Blogger keerthireddy said...

Can you let me know how to do the same for an website application since there is no dll created once you build the website

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home