【发布时间】:2020-07-24 20:26:36
【问题描述】:
假设你有这个类:
// this is c# btw
class Rewards {
public int property1 {get;set;}
public int property2 {get;set;}
public int property3 {get;set;}
}
并且您在某处创建了该类的 Instance 并更新了它的一些值
Rewards reward = new Rewards();
reward.property1 = 10000000000;
现在,您要做的是获取其值符合特定条件的属性的 名称(例如:值大于 0 的属性 -> 在这种情况下 Property1 将被返回/推送到数组中
你会怎么做?
我的尝试:
var allRewards = typeof(Rewards).GetProperties().ToList(); // this gets all the properties, but I'm not sure how the filter them with a Where query
Debug.Log(typeof(Rewards).GetProperty(allRewards[1].Name).GetValue(rewards))); // printing one of the values as a test - which works
所以它应该做的是遍历每个属性,运行特定条件,如果通过测试,则将该属性存储在列表中
我明白了,我可以使用 for 循环遍历列表,但我想要一个带有 Where 查询的解决方案
【问题讨论】:
标签: c# linq class reflection properties