C# 在对语句求值后调用方法

C# 在对语句求值后调用方法,c#,methods,sharepoint-2010,optional-arguments,C#,Methods,Sharepoint 2010,Optional Arguments,这应该是很容易回答,但我甚至不知道如何正确地问它,所以我为我的n00b提前道歉。我一直在努力将它解释为没有运气的搜索 基本上,我有一个方法,它将几个参数作为“开关”(调用方法设置为0或1)和可选字符串,并使用它们“构建”其行动计划。事情是这样的: public static void Foo(int a, int b, int c, optionalString aa, optionalString, bb, optionalString cc) { if (a == 1) {

这应该是很容易回答,但我甚至不知道如何正确地问它,所以我为我的n00b提前道歉。我一直在努力将它解释为没有运气的搜索

基本上,我有一个方法,它将几个参数作为“开关”(调用方法设置为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.
}
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.
编辑以澄清:我无法将空值传递给
Bar()
,因为只需要使用实际设置的属性调用它。例如,调用Foo()时,a、b和c的集合如下所示:

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.
}
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.
编辑结束

因此,如果不对每个可能的
Bar()
组合使用大量嵌套的
if()
语句或方法,有没有一种方法可以在所有这些语句都经过计算后调用它?从技术上讲,变量尚未赋值,因此
Bar()
无效。或者,有没有更好的方法来完成这样的事情


这适用于与SharePoint server对象模型交互的控制台应用程序(如果有任何区别的话)。非常感谢您抽出时间

也许您只想将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);
}

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

使用定义为
dictionary
的字典结构,其中
Key=可以创建的任何唯一值。然后,您在方法中所要做的就是计算密钥并执行相关操作

从你的例子来看:

    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]();
    }
publicstaticvoidfoo(inta、intb、intc、optionalString aa、optionalString、bb、optionalString cc)
{
Dictionary objectMapper=新字典
{
{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]();
}

在我的示例中,唯一的键是3个输入变量。但是,正如您所看到的,覆盖所有可能的情况都是相当乏味的,这就是为什么我不建议完全采用这种方式,而是尝试修改您的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(1, 0, 1, string1, string3)

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

首先,可以是0或1的“开关”不是int,而是
bool
。此外,严格意义上,这些字符串参数不是可选的,为什么不直接使用
stringaa==null
,如果它为null,就不要使用它?你的问题很难理解。看起来,除了示例代码中出现的一些奇怪的-o命名问题外,它完全符合您的要求。发布一些相关的代码,当我看到IFoo、Foo和Bar时,我感觉自己像外星人。您不能用泛型解决这个问题吗?不幸的是,对Bar()的调用不能包含任何空值。请看我的编辑来澄清。啊,这看起来像是一个更干净的方式来完成我的目标,如果其他一切都失败了。谢谢你的选择!不幸的是,
Bar()
方法实际上代表了特定于SharePoint的各种对象索引上的一系列操作,但在本例中,它的调用方式与常规方法相同,不灵活。如果没有使用参数的确切数目和组合来调用它,它就会爆炸。如果不重写一堆SharePoint程序集,我无法更改它的工作方式。@thanby,lambda表达式可以是您想要的任何语句序列。但是,如果您只有一个公共API调用,那么您实际上没有太多选择。如果您喜欢我的方法,那么很高兴认识到它。:)到目前为止,这是最好的答案竞争者!(我还没有代表投票,否则我会)调用
Foo()
指定参数,比如
(string1:firstString,string3:thirdString)
…我做得对,不是吗?就像我说的,我对这门语言还不熟悉
Bar()
不能接受数组,但是我可以使用数组来表示参数组合来构建对它的调用吗?(如果有意义的话)是的,如果你把参数命名为Foo,你会没事的。我不确定您对C#有多熟悉,所以如果我解释了一些您已经知道的内容,我很抱歉,但这样声明Bar并不会将其限制为仅接收数组。它将接受数组和单独传递的值,也就是说,您仍然可以使用参数列表调用它。您可以在此处找到信息:。如果您尝试执行的操作涉及数量可变的参数,并且这些参数中没有一个可以为null,那么我认为您需要使用params。通过阅读msdn文章,看起来params可能是解决我的问题的一种方法。我会试试看什么最有效。谢谢你的建议!