Wednesday, May 7, 2008

VB.NET Reflection with Example

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 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 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




.

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



: 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.



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.



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 will 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.

No comments: