DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

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

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.

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

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.


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

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.

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.

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());
}
}



Looking for the VB.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!