asp.net网站项目调用page,或者ashx页面不能用反射

public class TestHandler : System.Web.IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(System.Web.HttpContext context)
        {
            Page page = BuildManager.CreateInstanceFromVirtualPath("~/webform1.aspx", typeof(WebForm1) ) as Page; 
// WebForm1是你要加载页面对应的后台类
            page.ProcessRequest(context);

        }
    }
public partial class fanshe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IHttpHandler page =BuildManager.CreateInstanceFromVirtualPath("/ascx/test.ashx", typeof (IHttpHandler)) as IHttpHandler;
        page.ProcessRequest(HttpContext.Current);
    }
}

 

页面对应的类是动态编译的,所以不会出现在 Assembly 中,也无法通过反射取得。
除了用上面的方法加载页面,也可以在 @Page 指令中指定 ClassName,然后可以直接 new 这个page(当然前提是页面要已经访问过了precompiled)

posted @ 2017-01-12 17:31  uxinxin  阅读(748)  评论(0编辑  收藏  举报