当前位置:编程学习 > C#/ASP.NET >>

ASP.NET高级技术教程

好了下面我们来看看在asp.net里面的高级技术教程方面的实例与教程吧.

本教程演示了如何去获得一流的财产价值和调用类的方法与思考。


本教程演示了如何去获得一流的财产价值和调用类的方法与思考。

反思提供了对象(型型)的封装组件,模块和类型。您可以使用反射动态地创建一个实例的类型,绑定的类型现有的物件,或获得类型从现有的对象和调用它的方法或访问其字段和属性。如果您使用的是属性的程式码中,反思使您能够访问它们。
首先,您将需要进口的系统。反思名称

using System.Reflection;

我们使用的是服务器的智慧和发现,到目前为止,他们是最友好,敏感,和有知识的支持团队,我们曾经处理!

在System.Reflection命名空间包含类和接口,提供了一个管理加载鉴于种类,方法和领域,有能力动态地建立和引用类型。
我们使用btnGetProperty_Click获得的财产价值的阶级InstanceClass 。我们使用btnInvoke_Click援引功能getFunction类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());

} www.zzzyk.com

添加一个自定义类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;
}

}

我们感动我们的网站服务器的智慧和发现他们是难以置信的专业。其安装非常容易,我们启动和运行中没有时间。

前端ReflectionCsharp.aspx页看上去像这样:

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

我只是签署了在服务器智力,不能更高兴,我的Windows Server !这里瞧瞧吧,看看吧。

流动的代码隐藏页如下。

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

}

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,