我想要通过反射获取页面的私有方法,用的如下代码
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,请问该如何修改或者有没有其他途径获取到私有方法
看输出,理解一下。
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"; } }