DotNet Tutorials

Server Intellect

 How to reflection using ASP.NET 2.0 and VB.NET

This tutorial demonstrates how to go get class's property value and invoke class's method with Reflection. This tutorial demonstrates how to go get class's property value and invoke class's method with Reflection.

Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.
First, you will need to import the System. Reflection namespace

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Imports System.Reflection;

The System.Reflection namespace contains classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types.
We use btnGetProperty_Click to get the property value of the class InstanceClass. And we use btnInvoke_Click to invoke the function getFunction of the class InstanceClass. The code as follows.

Protected Function getObjectProperty(ByVal str As String) As String
Dim retValue As String = ""
Try
Dim o As Object = Activator.CreateInstance(type, New Object() {Me.txtPropertyValue.Text.Trim()})
Dim pi As PropertyInfo = type.GetProperty("ReturnValue", GetType(String))
retValue = pi.GetValue(o, Nothing).ToString()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return retValue
End Function

Protected Function getOjbectMethod(ByVal str As String) As String
Dim retValue As Object = Nothing
Try
Dim o As Object = Activator.CreateInstance(type)
Dim mi As MethodInfo = type.GetMethod("getFunction", BindingFlags.Public Or BindingFlags.Instance, Nothing, New Type() {GetType(String)}, Nothing)
retValue = mi.Invoke(o, New Object() {str})
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return retValue.ToString()
End Function

Protected Sub btnGetProperty_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.lblPropertyResult.Text = Me.getObjectProperty(Me.txtPropertyValue.Text.Trim())
End Sub

Protected Sub btnInvoke_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.lblInvokeResult.Text = Me.getOjbectMethod(Me.txtParameter.Text.Trim())
End Sub

Add one custom class InstanceClass in this example.The code as follows:

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.


Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class InstanceClass
Private _returnValue As String = ""

Public Property ReturnValue() As String
Get
Return "You input value is:" + _returnValue
End Get
Set(ByVal value As String)
_returnValue = value
End Set
End Property

Public Sub New()

End Sub

Public Sub New(ByVal str As String)
Me._returnValue = str
End Sub

Public Function getFunction(ByVal str As String) As String
Return "You input value is:" + str
End Function
End Class

The front end ReflectionCsharp.aspx page looks something like this:

<table style="width: 708px">
<tr>
<td style="width: 68px">PropertyValue:</td>
<td style="width: 100px">
<asp:TextBox ID="txtPropertyValue" runat="server"></asp:TextBox></td>
<td style="width: 53px">MethodParameter:</td>
<td style="width: 100px">
<asp:TextBox ID="txtParameter" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnGetProperty" runat="server" OnClick="btnGetProperty_Click" Text="GetPropertyValue" /></td>
<td colspan="2">
<asp:Button ID="btnInvoke" runat="server" OnClick="btnInvoke_Click" Text="InvokeMethod" /></td>
</tr>
<tr>
<td style="width: 68px; height: 21px">Result:</td>
<td style="width: 100px; height: 21px; text-align: left;">
<asp:Label ID="lblPropertyResult" runat="server" ForeColor="Red" Width="258px"></asp:Label></td>
<td style="width: 53px; height: 21px">Result:</td>
<td style="width: 100px; height: 21px;text-align: left;">
<asp:Label ID="lblInvokeResult" runat="server" ForeColor="Red" Width="229px"></asp:Label></td>
</tr>
</table>

The flow for the code behind page is as follows.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports System.Reflection

Class _Default
Inherits System.Web.UI.Page '
Private type As Type = Nothing

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
type = GetType(InstanceClass)
End Sub

Protected Function getObjectProperty(ByVal str As String) As String
Dim retValue As String = ""
Try
Dim o As Object = Activator.CreateInstance(type, New Object() {Me.txtPropertyValue.Text.Trim()})
Dim pi As PropertyInfo = type.GetProperty("ReturnValue", GetType(String))
retValue = pi.GetValue(o, Nothing).ToString()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return retValue
End Function

Protected Function getOjbectMethod(ByVal str As String) As String
Dim retValue As Object = Nothing
Try
Dim o As Object = Activator.CreateInstance(type)
Dim mi As MethodInfo = type.GetMethod("getFunction", BindingFlags.Public Or BindingFlags.Instance, Nothing, New Type() {GetType(String)}, Nothing)
retValue = mi.Invoke(o, New Object() {str})
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return retValue.ToString()
End Function

Protected Sub btnGetProperty_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.lblPropertyResult.Text = Me.getObjectProperty(Me.txtPropertyValue.Text.Trim())
End Sub

Protected Sub btnInvoke_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.lblInvokeResult.Text = Me.getOjbectMethod(Me.txtParameter.Text.Trim())
End Sub
End Class



Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect