GetFields 返回空数组 [英] GetFields returning empty array

查看:140
本文介绍了GetFields 返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anyone see what I am doing wrong below? The type has the public property that the service method is trying to access so why is it not being picked up by 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
    }
}

干杯

斯图尔特

推荐答案

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

SomeProperty is - as the name says - a property. Use GetProperty and GetProperties instead! That leads to PropertyInfo instead of FieldInfo.

这篇关于GetFields 返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆