GetFields返回空数组

Ste*_*lan 1 c# reflection

谁能看到我下面做错了什么?该类型具有服务方法试图访问的公共属性,为什么它不被反射拾取?

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
    }
}
Run Code Online (Sandbox Code Playgroud)

干杯

斯图尔特

Seb*_*ian 9

SomeProperty - 顾名思义 - 是一个属性.使用GetPropertyGetProperties,而不是!这导致PropertyInfo而不是FieldInfo.