62,266
社区成员
发帖
与我相关
我的任务
分享 Page page = this.Page;
Type type = page.GetType();
foreach (PropertyInfo pi in type.GetProperties())
{
object value = pi.GetValue(page, null);
} 验证发生前无法调用 Page.IsValid。应在 CausesValidation=True 且已启动回发的控件的事件处理程序中查询它,或在调用 Page.Validate 后查询。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 验证发生前无法调用 Page.IsValid。应在 CausesValidation=True 且已启动回发的控件的事件处理程序中查询它,或在调用 Page.Validate 后查询。
源错误:
行 77: foreach (PropertyInfo pi in type.GetProperties())
行 78: {
行 79: object value = pi.GetValue(page, null);
行 80: }
using System;
using System.Reflection;
public class Program
{
static void Main(string[] args)
{
TestRefrance01();
Console.ReadKey();
}
private static void TestRefrance01()
{
PageBase page = new Page();
Type type = page.GetType();
//PropertyInfo[] ps = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
//Console.WriteLine(ps.Length);
foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
{
object value = pi.GetValue(page, null);
Console.WriteLine(value);//显示结果
}
}
public class PageBase
{
public string page_base { get; set; }
public PageBase()
{
page_base = "PageBase";
}
}
public class Page : PageBase
{
public string page { get; set; }
public Page()
{
page = "Page child";
}
}
}
Page page = this.Page;
Type type = page.GetType();
foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance| BindingFlags.DeclaredOnly| BindingFlags.Public))
{
object value = pi.GetValue(page, null);
}