有谁知道以下是否可以将对象的 List<> 传递给函数并指定该函数应在其使用的每个对象中使用哪个属性?
我有一个类在整个类中使用对象的特定属性,但我不想创建同一个类的多个副本来使用该对象的不同属性。我考虑过使用 Linq,但我没有看到一种方法来指定在操作类的其他函数中使用哪个属性。
我在想必须有一种更优雅的方式来做到这一点,而不是创建同一个类来处理每个属性。我考虑过使用反射来告诉函数使用哪个属性,但这很快就会变得丑陋
示例伪代码:
class Store
{
int amount;
int id;
int serial;
}
class AggregationMethods
{
bool Has3Values( List<Store> places /* some other param to specify which property to use*/)
{
// do something with Store.amount or Store.id
}
// other functions to work with Store.amount or Store.id or Store.serial
}