一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ASP.NET高级技术教程

时间:2008-12-18 编辑:简简单单 来源:一聚教程网

好了下面我们来看看在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.111com.net

添加一个自定义类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页看上去像这样:










PropertyValue:
MethodParameter:


Result:
Result:

我只是签署了在服务器智力,不能更高兴,我的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.111com.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());
}

}

热门栏目