[英]How do I use reflection to get a list of values of an object's fields that match a particular type?
Please note that for conciseness and readability, I've substituted types, fields, and methods that are more simple to work with. 请注意,为了简洁和可读性,我已替换了更易于使用的类型,字段和方法。
I've defined a boolean property, personProperty
for a class, Person
, in which I want the getter, get{}
to call a private method, personMethod(int arg)
on each integer field value that's defined in Person
(in this case _age
, _phoneNumber
). 我已经为类
Person
定义了一个布尔属性personProperty
,在该类中我希望getter get{}
对在Person
中定义的每个整数字段值(在本例中为_age
personMethod(int arg)
调用私有方法personMethod(int arg)
, _phoneNumber
)。 It should ignore all other types like readingList
. 它应该忽略所有其他类型,如
readingList
。
This is so that if I were to add another integer field to Person
(or modify or delete any Person
field names), I would not have to update the definition of personProperty
which, by design choice, depends on all integer fields of the Person
class (ie, it is never the case that the developer will introduce an int
field that he doesn't want personMethod
to run against). 这样,如果我要向
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;
}
}
I need to know what I should substitute for LISTOFPERSONINTS
so that it returns an iterable over the values stored in _age
, _phoneNumber
(and all other future int
, like _futureInt
defined in Person
). 我需要知道我应该代替
LISTOFPERSONINTS
,使其返回了存储在值的迭代_age
, _phoneNumber
(以及其他所有未来int
,像_futureInt
中定义Person
)。
I don't think that using reflection would be better than adjusting your property each time you add a field, but there you go: 我不认为使用反射会比每次添加字段来调整属性都要好,但是您可以:
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);
}
}
Test: 测试:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.