现在的位置: 首页 > 综合 > 正文

aspx页面在运行的时候会自动产生当前页面类的一个子类。

2012年01月14日 ⁄ 综合 ⁄ 共 472字 ⁄ 字号 评论关闭
假设有一个WebForm1.aspx,其中定义了类WebForm1。

但是下面的代码运行的结果却有点出乎意料:


private void Page_Load(object sender, System.EventArgs e)

{

        Response.Write(Object.ReferenceEquals(
this.GetType(), typeof(WebForm1)).ToString()); //输出为false.

}


原来aspx页面在运行的时候会动态产生一个派生类:ASP.WebForm1_aspx。运行时的实例对象是从那个派生类创建的。

因此上边的代码改成如下,输出就为true了。



private void Page_Load(object sender, System.EventArgs e)

{

        Response.Write(Object.ReferenceEquals(
this.GetType().BaseType, typeof(WebForm1)).ToString()); //输出为true.

}


抱歉!评论已关闭.