繁体   English   中英

如何使用反射获取与特定类型匹配的对象字段的值列表?

[英]How do I use reflection to get a list of values of an object's fields that match a particular type?

请注意,为了简洁和可读性,我已替换了更易于使用的类型,字段和方法。

我已经为类Person定义了一个布尔属性personProperty ,在该类中我希望getter get{}对在Person中定义的每个整数字段值(在本例中为_age personMethod(int arg)调用私有方法personMethod(int arg)_phoneNumber )。 它应该忽略所有其他类型,如readingList

这样,如果我要向Person添加另一个整数字段(或修改或删除任何Person字段名称),则不必更新personProperty的定义,根据设计选择,该定义取决于Person类的所有整数字段(即,永远不会让开发人员引入一个他不想让personMethod运行的int字段)。

public Class Person
{
   private int _age;
   public int _phoneNumber;
   // protected int _futureInt;
   Dictionary<string, string> _readingList = new Dictionary<string, string>();

   public bool personProperty
   {
       get
       {
           // ...
           bool personPropertyReturnValue; 
           List<bool> resultList = new List<bool>();
           foreach(int personFieldValue in LISTOFPERSONINTS)
           {
               bool result = personMethod(personFieldValue);
               resultList.Add(result);
           }
           // Do stuff with `resultList` that'll initialize personPropertyReturnValue;
           return personPropertyReturnValue;
       }
   }

    private bool personMethod(int arg)
    {
       bool returnValue = true;
       // Do stuff to initialize `returnValue`
       return returnValue;
    }
}

我需要知道我应该代替LISTOFPERSONINTS ,使其返回了存储在值的迭代_age_phoneNumber (以及其他所有未来int ,像_futureInt中定义Person )。

我不认为使用反射会比每次添加字段来调整属性都要好,但是您可以:

public class Person
{
    private int _age;
    public int _phoneNumber;
    // protected int _futureInt;
    Dictionary<string, string> _readingList = new Dictionary<string, string>();

    public Person(int age){
        _age = age;
    }

    public bool personProperty
    {
        get
        {
            List<bool> resultList = new List<bool>();
            var intFields = this.GetType().GetFields(BindingFlags.Instance | 
                                                     BindingFlags.NonPublic | 
                                                     BindingFlags.Public)
                                          .Where(f => f.FieldType == typeof(int))
                                          .Select(f => f.GetValue(this)).Cast<int>();
            foreach (int personFieldValue in intFields)
            {
                bool result = personMethod(personFieldValue);
                resultList.Add(result);
            }
            // Do stuff with `resultList` that'll initialize personPropertyReturnValue;
            bool personPropertyReturnValue = resultList.All(b => b);
            return personPropertyReturnValue;
        }
    }

    private bool personMethod(int arg)
    {
        return (arg > 0);
    }
}

测试:

var person1 = new Person(0);
Console.WriteLine(person1.personProperty);   // False

var person2 = new Person(1);
Console.WriteLine(person2.personProperty);   // False

var person3 = new Person(1) { _phoneNumber = 1 };
Console.WriteLine(person3.personProperty);   // True

反思:如何从 object 中获取值作为列表<object><div id="text_translate"><p>使用反射,我需要一个接受通用对象列表并打印其值的 function</p><pre> List&lt;Gender&gt; genders = new List&lt;Gender&gt; { new Gender { Id: 1, Name: "Male" }, new Gender { Id: 2, Name: "Female" } } PrintListValues(genders, "Name"); // prints Male, Female</pre><p> PrintListValues 必须接受列表为 object。 并且该列表可以包含任何类型的 object。 只要通用 object 有属性传入,它应该打印列表中每个项目的值。</p><pre> public static void PrintValues(object listObject) { // code here }</pre><p> 不幸的是,这就是项目要求的方式。</p><p> 一直在为这个问题挠头,无法弄清楚。 反思对我来说太难了。 如果有人可以给我提示,那就太好了!</p></div></object>

[英]Reflection: how to get values from object as List<object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Reflection获取特定接口类型的所有字段 如何使用反射来获取方法的字段? 如何使用Reflection设置类型为List <CustomClass>的Property 如何使用反射获取特定类型的通用列表 使用 C# 反射,如果该对象是列表内的对象的属性,则如何获取该对象的属性及其值 如何在列表中获取特定的派生对象 <T> ? 如何使用反射将类型对象转换为C#中的列表。 我可以访问对象中的属性,但不能访问值,有什么建议吗? 反思:如何从 object 中获取值作为列表<object><div id="text_translate"><p>使用反射,我需要一个接受通用对象列表并打印其值的 function</p><pre> List&lt;Gender&gt; genders = new List&lt;Gender&gt; { new Gender { Id: 1, Name: "Male" }, new Gender { Id: 2, Name: "Female" } } PrintListValues(genders, "Name"); // prints Male, Female</pre><p> PrintListValues 必须接受列表为 object。 并且该列表可以包含任何类型的 object。 只要通用 object 有属性传入,它应该打印列表中每个项目的值。</p><pre> public static void PrintValues(object listObject) { // code here }</pre><p> 不幸的是,这就是项目要求的方式。</p><p> 一直在为这个问题挠头,无法弄清楚。 反思对我来说太难了。 如果有人可以给我提示,那就太好了!</p></div></object> 如何通过反射获取委托类型的返回类型? 如何使用反射来获取对象实例的公共属性?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM