C# GetFields返回空数组

C# GetFields返回空数组,c#,reflection,C#,Reflection,有人能看到下面我做错了什么吗?该类型具有服务方法试图访问的公共属性,那么为什么反射不能获取它呢 Public class SomeClass { private YetAnotherClass yetAnotherClass; public SomeClass(SomeOtherClass otherclass) { this.yetAnotherClass = otherclass.SomeProperty; } public YetAnothe

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

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


Stewart

正如名字所说,某些财产是一种财产。改用GetProperty和GetProperties!这将导致PropertyInfo而不是FieldInfo。

SomeProperty正如其名称所示,是一个属性。改用GetProperty和GetProperties!这将导致PropertyInfo而不是FieldInfo。

如果答案对您有帮助,您能接受吗?如果答案对你有帮助,你能接受吗?谢谢你。我浪费了一个小时想弄明白。非常感谢。我浪费了一个小时想弄明白这件事。