Kshitij’s Blog .NET/ CSLA.NET / ASP.NET / VB.NET / C# / SQL Server / MySql ..
  • Jun
    16

    Using Reflection

    Filed under: .NET, Technical, VB.NET;


    So what basically is reflection:


    Using a class library methods at runtime without adding its reference in your project is done through Reflection.  In this article I will show you how to use reflection using a simple example.
    Let’s first create a Class Library.

    ScreenHunter_01 Jun. 13 12.06

    let’s code our ReflectionTest Class

    
    Namespace reflectionclass
    Public Class ClsReflection
    
    #Region "Methods"
    Public Function add(ByVal x As Integer, ByVal y As Integer) As Integer
    Return x + y
    End Function
    
    Public Function [sub](ByVal x As Integer, ByVal y As Integer) As Integer
    Return x - y
    End Function
    #End Region
    
    End Class
    End Namespace
    

    let’s make a test project now. Add a windows form application to the solution.

    ScreenHunter_02 Jun. 13 12.20

    Now let’s design our Form.

    ScreenHunter_02 Jun. 16 12.48

    so after putting two command buttons two listview controls and four textbox controls our form should look like this.

    Let’s code our form now

    Imports System.Reflection
    
    Public Class Form1
    Dim objAssembly As Assembly
    'Declare an array of Types to Hold All Classes of Assembly
    Dim arrTypes As Type()
    Dim objType As Type
    'Delcare an array to hold all methods of Assembly
    Dim arrMethods As MethodInfo()
    Dim objMethod As MethodInfo
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
    OpenFileDialog1.ShowDialog()
    txtAssembly.Text = OpenFileDialog1.FileName
    objAssembly = System.Reflection.Assembly.LoadFrom(txtAssembly.Text)
    For Each cls In objAssembly.GetTypes
    '
    If cls.FullName.IndexOf("ClsReflection") > 0 Then
    lstClasses.Items.Add(cls.FullName)
    End If
    Next
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub
    
    Private Sub lstClasses_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstClasses.SelectedIndexChanged
    Try
    lstMethods.Items.Clear()
    objType = objAssembly.GetType(Me.lstClasses.SelectedItem.ToString)
    arrMethods = objType.GetMethods
    For Each method In objType.GetMethods
    lstMethods.Items.Add(method.Name)
    Next
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub
    
    Private Sub cmdCallMethod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCallMethod.Click
    Try
    objMethod = objType.GetMethod(lstMethods.SelectedItem.ToString())
    Dim obj As Object
    obj = Activator.CreateInstance(objType)
    'Let's Create Array of Parameter
    Dim objParameters As Object() = {Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)}
    'If there is no parameter in method then on the place of oo pass the null
    txtResult.Text = objMethod.Invoke(obj, objParameters).ToString()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub
    End Class
    

    ScreenHunter_03 Jun. 16 13.09

    Select Add and put any two numbers in the first and second number textbox and click on execute method and you should get the result.

    ScreenHunter_04 Jun. 16 13.12

    Now select Sub and enter any two number in first number and second number textbox and press execute method and you should get the desired result.

    This article show you how you can call method dynamically at runtime without referencing the assembly in your project. It is a very basic example but I think it will help you to understand how reflection works in .Net.

    If you have any point you are not getting in this article please let me know i will try to explain.

    Happy Coding!

    Kshitij

    2 people like this post.
    4 Comments

4 Responses to “Using Reflection”

  1. Hey, nice post, really well written. You should blog more about this.

  2. Nice Post. Thanks

    Marta

  3. Good Article. Very Informative. Please write more on this.

  4. Sure Aleah.

    Thanks,

Leave a Reply