2013-03-24 102 views
0

任何人都可以看到我在下面做错了什么?该类型具有服务方法试图访问的公共属性,为什么它没有被反射拾取?GetFields返回空阵列

Public class SomeClass 
{ 
    private YetAnotherClass yetAnotherClass; 

    public SomeClass(SomeOtherClass otherclass) 
    { 
     this.yetAnotherClass = otherclass.SomeProperty; 
    } 

    public YetAnotherClass SomeProperty 
    { 
     get { return this.yetAnotherClass; } 
    } 
} 

Public class ServiceClass 
{ 
    public void DoSomething(SomeClass someclass) 
    { 
     Type type = someclass.GetType(); 
     FieldInfo[] fieldsinfo = type.GetFields(BindingFlags.Public | BindingFlags.Instance); // returns empty collection 
     FieldInfo fieldinfo = type.GetField("SomeProperty"); // returns null reference exception 
    } 
} 

干杯

斯图尔特

+0

如果能帮到您,请您接受答案吗?谢谢 – Sebastian 2013-04-14 13:19:27

回答

7

SomeProperty是 - 正如它的名字说 - 属性。改用GetPropertyGetProperties!这导致PropertyInfo而不是FieldInfo