Asp. NET Advanced Technology Tutorials

Source: Internet
Author: User
Tags reflection trim

All right, let's take a look at the examples and tutorials in the advanced technology tutorials in asp.net.

This tutorial demonstrates how to get top-class property values and invoke classes in methods and thinking.


This tutorial demonstrates how to get top-class property values and invoke classes in methods and thinking.

Reflection provides an object (type) for encapsulating components, modules, and types. You can use reflection to dynamically create an instance of the type, an existing object of the binding type, or to obtain a type from an existing object and invoke its methods or access its fields and properties. If you are using a property's code, reflection enables you to access them.
First, you will need to import the system. Reflection Name

Using System.Reflection;

We are using the wisdom and discovery of the server, so far they are the friendliest, sensitive, and knowledgeable support team we have ever dealt with!

The System.Reflection namespace contains classes and interfaces that provide a management load given the kind, method and domain that are capable of dynamically establishing and referencing types.
We use Btngetproperty_click to obtain the property value of the class Instanceclass. We use Btninvoke_click to invoke functional GetFunction class Instanceclass. The code is 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 ());

} www.111cn.net

Add a custom class Instanceclass here for example 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 "Your input value is:" +_returnvalue}
set {_returnvalue = value;}
}
Public Instanceclass ()
{

}
Public Instanceclass (String str)
{
This._returnvalue = str;
}
public string getfunction (String str)
{
Return "Your input value is:" + str;
}

}

We moved our web server to the wisdom and found that they were incredibly professional. Its installation is very easy, we start and run in no time.

The front end reflectioncsharp.aspx page looks 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 in the server intelligence and couldn't be happier with my Windows Server! Here, take a look at it.

The flowing 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)
{
Www.111cn.netthis.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 ());
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.