Friday, August 8, 2014

A very simple example on System.Reflection in Asp.net

Today I will show you a very simple example on Reflection.

Follows the steps:

Step 1. Create a project with asp.net empty web application and name it "ReflectApp".

Step 2. Create a web form in it and paste following code and run it.

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, 
                            ByVal e As System.EventArgs) Handles Me.Load
        Dim currentAppName = System.Reflection.Assembly.Load("ReflectApp")

        For Each type As Type In currentAppName.GetTypes
            If type.Name = "Customer" Then
                Dim o As Object = Activator.CreateInstance(type)

                Dim mInfo As System.Reflection.MethodInfo = type.GetMethod("getName")

                If mInfo IsNot Nothing Then
                    Dim result As String = mInfo.Invoke(o, Nothing)

                    Response.Write(result)
                End If

                Exit For
            End If

        Next 
    End Sub
    Public Class Customer
        Private Property _Name As String

        Sub New()
            _Name = "John Miller"
        End Sub

        Public Function getName() As String
            Return _Name
        End Function

    End Class

End Class


Result will be: John Miller

You can debug and see how it works.

No comments:

Post a Comment

React-select is very slow on larger list - Found solution - using react-window

 I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...