我想传递一个函数的类属性(或者getter/setter,如果需要的话)引用。传递类属性作为参数
例如,我有一个有很多布尔标志的类的数组。
class Flags
{
public bool a;
public bool b;
public bool c;
public string name;
public Flags(bool a, bool b, bool c, string name)
{
this.a = a;
this.b = b;
this.c = c;
this.name = name;
}
}
我可以写一个返回标志的所有实例为其选定的标志是真正的方法
public Flags[] getAllWhereAisTrue(Flags[] array)
{
List<Flags> resultList = new List<Flags>();
for (int i = 0; i < array.Length; i++)
{
if (array[i].a == true) // for each Flags for which a is true
{ // add it to the results list
resultList.Add(array[i]);
}
}
return resultList.ToArray(); //return the results list as an array
}
我会怎么用,让我一个类的属性作为参数传递,从而为了让我不必为Flags的每个布尔属性写一次这个方法(在这个例子中是三次,一次是a,b和c)?
我试图避免给Flags一个布尔值的数组,以试图让结果代码易于阅读。我正在编写一个供相对缺乏经验的编程人员使用的库。
谢谢
(有道歉,如果这是Passing property as parameter in method愚弄的人,我也不太知道它是同样的问题)
你可以用一个简单的LINQ查询替换你的整个方法。 – SLaks
+1首先检查重复的问题。 – wllmsaccnt
可能重复[如何传递一个类的属性作为方法的参数?](http://stackoverflow.com/questions/1178574/how-can-i-pass-a-property-of-a -class-as-a-parameter-of-method) – nawfal