>

How to reflection using ASP.NET 2.0 and C#.NET

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

using System.Reflection;

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!

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 string getObjectProperty(string str)
{

string retValue = “”;
try
{

object o = Activator.CreateInstance(type, new object[] { this.txtPropertyValue.Text.Trim()});
PropertyInfo pi = type.GetProperty(“ReturnValue”, typeof(string));
retValue = pi.GetValue(o, null).ToString();

}
catch (Exception ex)
{

Console.WriteLine(ex.Message);

}
return retValue;

}

protected string getOjbectMethod(string str)
{

object retValue = null;
try
{

object o = Activator.CreateInstance(type);
MethodInfo mi = type.GetMethod(“getFunction”, BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string)},null);
retValue = mi.Invoke(o, new object[] { str });

}
catch (Exception ex)
{

Console.WriteLine(ex.Message);

}
return retValue.ToString();

}

protected void btnGetProperty_Click(object sender, EventArgs e)
{

this.lblPropertyResult.Text = this.getObjectProperty(this.txtPropertyValue.Text.Trim());

}

protected void btnInvoke_Click(object sender, EventArgs e)
{

this.lblInvokeResult.Text = this.getOjbectMethod(this.txtParameter.Text.Trim());

}

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

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class InstanceClass
{

private string _returnValue = “”;
public string ReturnValue
{

get { return “You input value is:”+_returnValue; }
set { _returnValue = value; }

}
public InstanceClass()
{

}
public InstanceClass(string str)
{

this._returnValue = str;

}
public string getFunction(string str)
{

return “You input value is:” + str;

}

}

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

I just signed up at Server Intellect and couldn’t be more pleased with my Windows Server! Check it out and see for yourself.

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Reflection;

public partial class _Default : System.Web.UI.Page
{

private Type type = null;
protected void Page_Load(object sender, EventArgs e)
{

type = typeof(InstanceClass);

}

protected string getObjectProperty(string str)
{

string retValue = “”;
try
{

object o = Activator.CreateInstance(type, new object[] { this.txtPropertyValue.Text.Trim()});
PropertyInfo pi = type.GetProperty(“ReturnValue”, typeof(string));
retValue = pi.GetValue(o, null).ToString();

}
catch (Exception ex)
{

Console.WriteLine(ex.Message);

}
return retValue;

}

protected string getOjbectMethod(string str)
{

object retValue = null;
try
{

object o = Activator.CreateInstance(type);
MethodInfo mi = type.GetMethod(“getFunction”, BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string)},null);
retValue = mi.Invoke(o, new object[] { str });

}
catch (Exception ex)
{

Console.WriteLine(ex.Message);

}
return retValue.ToString();

}

protected void btnGetProperty_Click(object sender, EventArgs e)
{

this.lblPropertyResult.Text = this.getObjectProperty(this.txtPropertyValue.Text.Trim());

}

protected void btnInvoke_Click(object sender, EventArgs e)
{

this.lblInvokeResult.Text = this.getOjbectMethod(this.txtParameter.Text.Trim());

}

}

Advertisements