2

请问,我该如何重构这段代码以避免切换情况?我需要改用字典吗?有没有一种方法可以在不添加新案例的情况下更改命令并自动生成...我的意思是这个解决方案不会取决于案例的数量。我希望我的问题很清楚。

    static void Main(string[] args)
    {
        ConsoleHelp ch1 = new ConsoleHelp();
        Plus pl = new Plus();
        Minus mn = new Minus();
        Divide dv = new Divide();
        Multiply mlt = new Multiply();
        Sinus sin = new Sinus();
        Tangent tan = new Tangent();
        Square sq = new Square();
        Degree dg = new Degree();
        Percent pr = new Percent();

        while (true)
        {
            Console.Write("command> ");
            string com = Console.ReadLine();
            if (com != null)
            {
                switch (com.ToLower())
                {
                    case "?":
                        ch1.Helpper();
                        break;
                    case "plus":
                        pl.Sum();
                        break;
                    case "minus":
                        mn.Minusvalue();
                        break;
                    case "divide":
                        dv.Dividevalue();
                        break;
                    case "multiply":
                        mlt.Multvalue();
                        break;
                    case "sinus":
                        sin.Sinusvalue();
                        break;
                    case "tangent":
                        tan.Tangentvalue();
                        break;
                    case "square":
                        sq.Squarevalue();
                        break;
                    case "degree":
                        dg.Degvalue();
                        break;
                    case "percent":
                        pr.Pervalue();
                        break;
                    case "c":
                        Environment.Exit(0);
                        break;

                    default:
                        Console.WriteLine("The command is not supported. Enter one of the valid commands.");
                        break;
                }
            }
        }
        Console.ReadLine();
    }
4

4 回答 4

1

尽管switch在您的示例中使用 a 没有任何问题,但您可以使用 aDictionary<string, action>将您的命令映射到适当的操作:

static void Main(string[] args)
{
    ConsoleHelp ch1 = new ConsoleHelp();
    Plus pl = new Plus();
    Minus mn = new Minus();
    Divide dv = new Divide();
    Multiply mlt = new Multiply();
    Sinus sin = new Sinus();
    Tangent tan = new Tangent();
    Square sq = new Square();
    Degree dg = new Degree();
    Percent pr = new Percent();

    var commands = new Dictionary<string, Action>(StringComparer.InvariantCultureIgnoreCase)
        {
            { "?", ch1.Helper },
            { "plus", p1.Sum },
            { "minus", mn.MinusValue },
            { "divide", dv.DivideValue },
            { "multiply", mlt.Multvalue },
            { "sinus", sin.Sinusvalue }, 
            { "tangent", tan.Tangentvalue }, 
            { "square", sq.Squarevalue }, 
            { "degree", dg.Degvalue }, 
            { "percent", pr.Pervalue}, 
            { "c", () => Environment.Exit(0) }
        };

    while (true)
    {
        Console.Write("command> ");
        string com = Console.ReadLine();
        if (!String.IsNullOrEmpty(com))
        {
            Action action;
            if(commands.TryGetValue(com))
                action();
            else
                Console.WriteLine("The command is not supported. Enter one of the valid commands.");
        }
    }
}
于 2013-08-12T17:26:00.003 回答
1

可以执行以下操作之一:

Dictionary<String, Action> myActions = new Dictionary<String, Action>() {
    {"One", delegate { ActionA(); }},
    {"Two", delegate { ActionB(); }},
    {"Three", delegate { ActionC(); }},
    {"Four", delegate { ActionD(); }}

};

myActions["StringInput"]();

定义字典时,请确保您可以访问外部类,以便它们可以被关闭。

于 2013-08-12T17:21:31.040 回答
1

看起来问题是“如何找到许多命名的处理程序以进行操作”。

一些可能的选择:

  • 处理程序(委托或接口)的命令名称字典
  • 使用自定义属性(方法或整个类,取决于您的需要)标记处理程序并使用反射来找到它们。IE[CustomCommand(Name="percent")]
  • 使用具有命名实例(如 Unity)的依赖注入容器来添加许多“命令处理程序”接口的实现。
  • 只需使用“命令处理程序”接口作为标记并找到所有通过反射实现它的类。
于 2013-08-12T17:21:35.727 回答
0

字典可以帮助你

var dic = new Dictionary<string, Action>();

dic.Add("divide", () => dv.Dividevalue());

var com = "div";

dic[com]();

当然,您必须检查该命令是否已注册,也许是

Action action = null;

if (dic.TryGetValue(com, ou action))
{
    action();
}
于 2013-08-12T17:21:51.730 回答