这应该很容易回答,但我什至不知道如何正确提问,所以我提前为我的 n00b-ness 道歉。我一直在努力解释它以进行没有运气的搜索...
基本上我有一个方法,它接受几个参数作为“开关”(通过调用方法设置为 0 或 1)和可选字符串,并使用它们来“构建”其行动计划。它是这样的:
public static void Foo(int a, int b, int c, optionalString aa, optionalString, bb, optionalString cc)
{
if (a == 1)
{ Object1 o1 = Thing.Property1[aa]; }
if (b == 1)
{ Object2 o2 = Thing.Property2[bb]; }
if (c == 1)
{ Object3 o3 = Thing.Property3[cc]; }
Bar(optionalo1, optionalo2, optionalo3); // Edit: I explained this part a little wrong, see below.
}
编辑澄清:我不能将空值传递给,Bar()
因为它只需要使用实际设置的属性来调用。例如,调用 Foo() 时设置 a、b 和 c,如下所示:
Foo(1, 0, 1, string1, string3) //In this instance I only want the first and third properties set. The strings contain the values I want them set to.
{
if (a == 1)
{ set this property based on string1 }
if (b == 1)
{ this one would not be set because b was 0 }
if (c == 1)
{ set this property based on string3 }
Bar(property1, property3);
// In this instance, Bar() must be called with only those two arguments, it cannot contain any null values.
编辑结束
因此,如果不对每个可能的组合使用大量嵌套if()
语句或方法Bar()
,有没有办法在所有这些都被评估后调用它?从技术上讲,变量尚未分配,因此Bar()
无效。或者,有没有更好的方法来完成这样的事情?
这适用于与 SharePoint 服务器对象模型交互的控制台应用程序(如果有任何区别)。非常感谢您的宝贵时间!