首页 新闻 赞助 找找看

apsx页面无法通过反射获取私有方法

0
[已解决问题] 解决于 2017-09-15 10:53

我想要通过反射获取页面的私有方法,用的如下代码

     public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var method = this.GetType().GetMethod("GetAge", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            Response.Write(method.Name);
        }
        private string GetAge()
        {
            return "10";
        }

        public string GetName()
        {
            return "aa";
        }

    }

但返回的总是null,请问该如何修改或者有没有其他途径获取到私有方法

岩小黑的主页 岩小黑 | 菜鸟二级 | 园豆:202
提问于:2017-09-14 10:44
< >
分享
最佳答案
0

看输出,理解一下。

   public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebForm1 test = new WebForm1();
            var method = test.GetType().GetMethod("GetAge", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            Response.Write(this.GetType().Name + "<br/>");
            Response.Write(this.GetType().FullName + "<br/>");
            Response.Write(test.GetType().Name + "<br/>");
            Response.Write(test.GetType().FullName + "<br/>");
            Response.Write(method.Name);
        }
        private string GetAge()
        {
            return "10";
        }

        public string GetName()
        {
            return "aa";
        }

    }
奖励园豆:5
骑着蜗牛望太阳 | 菜鸟二级 |园豆:242 | 2017-09-15 10:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册