Page.aspx:
<a href="#" <%= ToggleUiVisibility(new List<bool>() { true, true, false }) %> >
link
</a>
C#:
public string ToggleUiVisibility(List<bool> conditions)
{
return conditions.Any(x=>!x) ? "style=\"display:none;\"" : string.Empty;
}
我想知道是否可以做(psuedo code)
之类的事情ToggleUiVisibility(true, true, argsN);
public string ToggleUiVisibility(args)
{
// make sure each argument is a bool and trigger the same
// functionality as above
}
答案 0 :(得分:5)
使用params关键字:
public string ToggleUiVisibility(params bool[] values)
{
// make sure each argument is a bool and trigger the same
// functionality as above
}
然后你可以调用你的方法:
ToggleUiVisibility(true, false, ...);
答案 1 :(得分:2)
您可以使用params
:
public string ToggleUiVisibility(params bool[] conditions)
{
return conditions.Any(x=>!x) ? "style=\"display:none;\"" : string.Empty;
}
但是你的调用代码必须单独拼出每个参数,或者将它们包装在一个数组中:
ToggleUiVisibility(true, true, false); // works
ToggleUiVisibility(argsN); // works
ToggleUiVisibility(true, true, argsN); // does not work - you'll need to "merge" the individual values and the array
ToggleUiVisibility((new bool[] {true, false}).Concat(argsN).ToArray()); // works