簡體   English   中英

GetFields返回空數組

[英]GetFields returning empty array

任何人都可以在下面看到我在做什么錯嗎? 該類型具有服務方法嘗試訪問的公共屬性,因此為什么不通過反射來獲取它呢?

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
    }
}

干杯

斯圖爾特

顧名思義,SomeProperty是一個屬性。 請改用GetPropertyGetProperties 這導致了PropertyInfo而不是FieldInfo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM