public static void RefViewStateProperty(object obj)
{
BindingFlags flags=BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; PropertyInfo[] propertyInfos=obj.GetType().GetProperties(flags);
for(int i=0;i<propertyInfos.Length ;i++)
{
Attribute useViewStateAttribute=Attribute.GetCustomAttribute(propertyInfos[i],typeof(UseViewStateAttribute ),true);
if(useViewStateAttribute!=null && propertyInfos[i].CanRead)
{
object result=propertyInfos[i].GetValue(obj,null);
}
}
}
以上useViewStateAttribute是个自定义的属性类,
我发现在properInfos数组中,obj对象的私有属性都没有出现,是不是私有属性无法被反射?

解决方案 »

  1.   

    按照msdn中的解释并不是不行,具体如下:
    Res
    A property is considered public to reflection if it has at least one accessor that is public. That is, you can call type.GetProperty("propertyname", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) to get it.Otherwise, the property is private and you must use type.GetProperty("propertyname", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) to get it.The following BindingFlags filter flags can be used to define which nested types to include in the search: You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return. 
    Specify BindingFlags.Public to include public properties in the search. 
    Specify BindingFlags.NonPublic to include non-public properties (that is, private and protected members) in the search. 
    Specify BindingFlags.FlattenHierarchy to include static properties up the hierarchy. 
    The following BindingFlags modifier flags can be used to change how the search works: BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited. 
    See System.Reflection.BindingFlags for more information.If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).
      

  2.   

    Otherwise, the property is private and you must use type.GetProperty("propertyname", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) to get it
    我查MSDN时就见到了,但是我写如下代码:
    BindingFlags flags=BindingFlags.Instance | BindingFlags.NonPublic;
    PropertyInfo[] propertyInfos=obj.GetType().GetProperties(flags);
    得到的propertyInfos是个空数组.
      

  3.   

    为什么不看全呢
    If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).
      

  4.   

    可以得到
    http://study.pay500.com/4/s44905.htm
    但是只能访问本类的
    基类的好像访问不到
      

  5.   

    If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).
    ---我传入的对象的类型是public的,而且对象和调用方法都在一个assembly中,所以,上面的内容我就没考虑.
      

  6.   

    lovefootball(蟑螂)提供的例子我在WinForm中试了下,是可行的.
    很奇怪,但是在ASP.Net中的Page类中就是行不通.
    我上例中传入的是个Form,目的是想检查Form中所有的属性,有用到ViewState的(我自定了个Attribute来作标识),我就提前读一次属性值.
    BindingFlags flags=BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    MemberInfo[] memberInfos=obj.GetType().GetMembers(flags);
    我发现读出来的不论是FieldInfo,还是PropertyInfo,还是MethodInfo都是只有public和protected的,private的一个都没有.
      

  7.   

    说传了,我上例中传入的是个Page或是用户控件.
      

  8.   

    哦,自己的Assembly内部的可以访问。
      

  9.   

    这两天较忙,没时间上CSDN.
    我有试过,代码如下:
    public class Test
    {
    public Test()
    {
    } private string  x=string.Empty ;
    private string PP
    {
    get
    {
    return x;
    }
    }
    }public class Helper
    {
    public static void RefViewStateProperty(object obj)
    {
    BindingFlags flags=BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;            PropertyInfo[] propertyInfos=obj.GetType().GetProperties(flags);
    for(int i=0;i<propertyInfos.Length ;i++)
    {
    Attribute useViewStateAttribute=Attribute.GetCustomAttribute(propertyInfos[i],typeof(UseViewStateAttribute ),true);
    if(useViewStateAttribute!=null && propertyInfos[i].CanRead)
    {
    object result=propertyInfos[i].GetValue(obj,null);
    }
    }
    }
    }public class TestPage:System.Web.UI.Page
    {
    ......
    private string CurStatus
    {
    get
    {
    if(this.ViewState["CurStatus"]==null)
    { this.ViewState["CurStatus"]="Test";
    }
    return this.ViewState["CurStatus"].ToString();
    }
    } private void Page_Load(object sender, System.EventArgs e)
    { if(!this.IsPostBack )
    {
    Helper.RefViewStateProperty(this);
    Test x=new Test();
    Helper.RefViewStateProperty(x);
    }
    }
    }
    在Helper.RefViewStateProperty(this)中取到的propertyInfos数组中总是没有TestPage的CurStatus属性.
    但如果是Helper.RefViewStateProperty(x)中取到的propertyInfos数组中就可以取到Test的PP属性.
    测试说明对普通类和Page类,NET的反射处理是存在差别的,但我还没想明白这中间有什么差别.