参考代码

时间:2017-06-01 13:37:19

标签: c# .net

我想知道在c#中是否可以这样做。

让我们说我有一个使用这些方法的课程:

public class Ladder
{
    private int currentStep = 0;

    Ladder Up()
    {
        currentStep++;
        return this;
    }

    Ladder Down()
    {
        currentStep--;
        return this;
    }
}

我可以这样使用它:

Ladder ladder = new Ladder();

ladder.Up().Up().Up().Down().Down();

现在,我想添加一个条件方法,可以像这样使用:

ladder.IF(somecondition, Up(), Down());

如果somecondition == true则表示执行Up(),否则执行Down()

有可能吗?我正在考虑使用匿名方法,但无法弄清楚如何引用"这个"实例所以它会知道这些函数的引用是什么。

非常感谢任何帮助!

6 个答案:

答案 0 :(得分:5)

这可能接近你所要求的内容,虽然我不会说它会让代码更清晰地面向未来的维护者:

Ladder If(bool condition,
    Func<Ladder, Ladder> @true, 
    Func<Ladder, Ladder> @false)
{
    return condition ? @true(this) : @false(this);
}

用法:

ladder
    .Up()
    .Down()
    .If(true, l => l.Up(), l => l.Down())
    .Up()
    .Down();

答案 1 :(得分:3)

您不需要条件方法。使用?:运算符:

somecondition ? ladder.Up() : ladder.Down();

答案 2 :(得分:2)

您可以添加另一个方法Go来封装您的需求,您有两个选项 - 可能同时实现两者:

public Ladder Go(bool condition)
{
    if(condition)
      Up();
    else
      Down();
    return this;
}

public Ladder Go(Func<bool> condition)
{
    return this.Go(condition());
}

这允许您指定布尔值,内联条件或传递函数

ladder.Go(true);
ladder.Go(someValue > 0);
ladder.Go( someMethodWhichTakesNoParamsAndReturnsBool )

答案 3 :(得分:2)

这是编写此方法的方法:

public Ladder If(bool someCondition, Func<Ladder> trueFunc, Func<Ladder> falseFunc)
    {
        return someCondition ? trueFunc() : falseFunc();
    }

调用它的方式是

ladder.If(condition, ladder.Up, ladder.Down);

答案 4 :(得分:0)

这是我能得到的最接近你想要的东西 - 有一个名为If的扩展方法,它采取行动在梯子上执行。这些操作在梯形图类中定义为静态方法。然后使用静态导入功能导入Ladder类。

// This import is really important - it lets you refer to Up, 
// rather than Ladder.Up.
using static Ladder;

void Main()
{
    Ladder ladder = new Ladder();

    // Basic usage
    ladder.If(true, Up, Down);
    Console.WriteLine(ladder.ToString());

    // And demonstrating chaining.
    ladder.If(false, Up, Down).If(false, Up, Down);
    Console.WriteLine(ladder.ToString());
}

public class Ladder
{
    private int currentStep = 0;

    public Ladder Up()
    {
        currentStep++;
        return this;
    }

    public Ladder Down()
    {
        currentStep--;
        return this;
    }

    // Static methods for Up and Down a ladder.
    public static void Up(Ladder ladder)
    {
        ladder.Up();
    }

    public static void Down(Ladder ladder)
    {
        ladder.Down();
    }

    public override string ToString()
    {
        return "I'm on step " + currentStep;
    }
}


public static class Extensions
{
    // Extension method to conditionally take an action on the ladder.
    public static Ladder If(this Ladder ladder, bool condition, Action<Ladder> trueAction, Action<Ladder> falseAction)
    {
        if (condition)
        {
            trueAction(ladder);
        }
        else
        {
            falseAction(ladder);
        }

        return ladder;
    }
}

答案 5 :(得分:0)

您可以在Ladder类中添加另一个方法(使用:using System.Reflection;

public Ladder IF(bool condition, string trueFunc, string falseFunc)
{
    MethodInfo trueMethod = this.GetType().GetMethod(trueFunc);
    MethodInfo falseMethod = this.GetType().GetMethod(falseFunc);
    return condition ? (Ladder)trueMethod.Invoke(this, null) : (Ladder)falseMethod.Invoke(this, null);
}

并使用它:

ladder.IF(true, "Up", "Down"); // goes up
ladder.IF(false, "Up", "Down"); // goes down
ladder.IF(true, "Down", "Up"); // goes down
ladder.IF(false, "Down", "Up"); // goes up