ASP. NET advanced technology tutorial

Source: Internet
Author: User

Now let's take a look at the examples and tutorials of advanced technical tutorials in asp.net.

Now let's take a look at the examples and tutorials of advanced technical tutorials in asp.net.

This tutorial demonstrates how to obtain first-class property values and how to call classes.


This tutorial demonstrates how to obtain first-class property values and how to call classes.

Reflection provides encapsulation components, modules, and types of objects (Type-based. You can use reflection to dynamically create an instance type, bind existing objects of the type, or obtain the type from existing objects and call its methods or access its fields and attributes. If you are using an attribute code, reflection enables you to access them.
First, you will need to import the system. Reflection name

Using System. Reflection;

We are using server intelligence and discovery. So far, they are the most friendly, sensitive, and knowledgeable support team that we once handled!

The System. Reflection namespace contains classes and interfaces that provide a ability to dynamically create and reference types based on the types, methods, and fields of management loading.
We use the class InstanceClass of the property value obtained by btnGetProperty_Click. We use btnInvoke_Click to quote the getFunction class InstanceClass. The Code is as follows.

Protected string getObjectProperty (string str)
{

String retValue = "";
Try
{Object o = Activator. CreateInstance (type, new object [] {this.txt PropertyValue. 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.txt PropertyValue. Text. Trim ());

}

Protected void btnInvoke_Click (object sender, EventArgs e)
{

This. lblInvokeResult. Text = this.getOjbectMethod(this.txt Parameter. Text. Trim ());

} Www.111cn.cn

The following code adds a custom class InstanceClass:

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 are touched by the wisdom of our website servers and discover that they are incredibly professional. It is very easy to install. We have no time to start and run it.

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 on Server intelligence and cannot be happier, my Windows Server! Let's take a look.

The following is a hidden page of flowing code.

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.txt PropertyValue. 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. cnthis. lblPropertyResult. Text = this.getObjectProperty(this.txt PropertyValue. Text. Trim ());}

Protected void btnInvoke_Click (object sender, EventArgs e)
{This. lblInvokeResult. Text = this.getOjbectMethod(this.txt Parameter. 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.