2013-03-24 97 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