1

这应该很容易回答,但我什至不知道如何正确提问,所以我提前为我的 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 服务器对象模型交互的控制台应用程序(如果有任何区别)。非常感谢您的宝贵时间!

4

3 回答 3

1

您需要将代码转换为数据。您有一些输入参数,您需要对它们执行一些操作。

使用定义为您可以创建的Dictionary<Key, Action>位置的字典结构。Key = whatever unique value然后,您在方法中所要做的就是计算密钥并执行相关操作。

从你的例子:

    public static void Foo(int a, int b, int c, optionalString aa, optionalString, bb, optionalString cc)
    {
Dictionary<int, Action> objectMapper = new Dictionary<int, Action>
        {
            { 0, () => Bar() },
            { 1, () => Bar(Thing.Property1[aa]) },
            { 2, () => Bar(Thing.Property2[bb]) },
            { 4, () => Bar(Thing.Property3[cc]) },
            { 3, () => Bar(Thing.Property1[aa], Thing.Property2[bb]) },
            { 5, () => Bar(Thing.Property1[aa], Thing.Property3[cc]) },
            { 6, () => Bar(Thing.Property2[bb], Thing.Property3[cc]) },
            { 7, () => Bar(Thing.Property1[aa], Thing.Property2[bb], Thing.Property3[cc]) },
        };    
        objectMapper[a & b & c]();
    }

在我的示例中,唯一键就是ANDing3 个输入变量。但是,如您所见,涵盖所有可能性非常繁琐,这就是为什么我不建议完全这样做的原因,而是尝试重新设计您的 Bar 方法以在输入参数上更加灵活。

于 2013-01-25T22:19:07.533 回答
1

取决于 Bar 的签名,是的。

如果 bar 被声明为类似

public static void Bar(params string[] values) {
    foreach(var v in values) {
        // use value
    }
}

然后 Foo 可以构建一个数组并发送它,例如

var list values = new List<string>();

if(a == 1) {
    list.Add(optionalo1);
    // do whatever else
}

if(b == 1) {
    list.Add(optionalo2);
    // do whatever else
}

Bar(values.ToArray());

编辑:

还要记住,如果 Foo 被声明为

public static void Foo(int a, int b, int c, string aa = null, string bb = null, string cc = null)

并且您像在示例中那样调用它:

Foo(1, 0, 1, string1, string3)

然后 string1 将按照您的意愿在 string1 中结束,但传递的 string3 将在 string2 中结束。您需要将该位置的 null 传递给 Foo ,否则这些值将被混淆。

于 2013-01-25T22:57:13.140 回答
1

也许您只想将 null 作为默认值传递给Bar方法,如下所示:

public static void Foo(int a, int b, int c, 
    optionalString aa, optionalString, bb, optionalString cc)
{
    Object1 o1 = null;
    Object1 o2 = null;
    Object1 o3 = null;
    if (a == 1)
    { o1 = Thing.Property1[aa]; }
    if (b == 1)
    { o2 = Thing.Property2[bb]; }
    if (c == 1)
    { o3 = Thing.Property3[cc]; }

    Bar(o1, o2, o3);
}
于 2013-01-25T21:56:32.250 回答