Tuesday, 19 July 2011

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

Imports System.Reflection;

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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

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

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

The flow for the code behind page is as follows.

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!

No comments:

Post a Comment