Refactor long switch statement

前端 未结 6 1414
我寻月下人不归
我寻月下人不归 2020-12-06 06:09

I\'m program in c# which you controlling by dictating command so now i have a long switch statement. Something like

switch (command)

{
    case \"Show comma         


        
6条回答
  •  一生所求
    2020-12-06 06:32

    I realize this is an old post, but in these situations I find attributes and a factory very handy.

    The following code uses a custom attribute (Command) to allow you to attribute your methods, providing a string value of how they should respond to you.

    The factory method uses reflection to generate a dictionary of these methods and calls it whenever you call CommandFactory.

    Things could get cleaned up a bit, calling invoke is a little ugly, but it just depends on how you want to execute the code.

    using System.Collections.Generic;
    using System.Linq;
    
    namespace MyApp
    {
        using System.Reflection;
        using MyApp.Commands;
    
        class Program
        {
            static void Main(string[] args)
            {
                var methods = new MyCommands();
                MethodInfo myMethod;
                myMethod = CommandFactory.GetCommandMethod("Show Commands");
                myMethod.Invoke(methods, null);
                myMethod = CommandFactory.GetCommandMethod("Close window");
                myMethod.Invoke(methods, null);
                myMethod = CommandFactory.GetCommandMethod("Switch window");
                myMethod.Invoke(methods, null);
            }
        }
    
        public static class CommandFactory
        {
            private static Dictionary speechMethods = new Dictionary();
            public static MethodInfo GetCommandMethod(string commandText)
            {
                MethodInfo methodInfo;
                var commands = new MyCommands();
                if (speechMethods.Count == 0)
                {
                    var methodNames =
                        typeof(MyCommands).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
                    var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType().Any());
                    foreach (var speechAttributeMethod in speechAttributeMethods)
                    {
                        foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
                        {
                            speechMethods.Add(((CommandAttribute)attribute).Command, speechAttributeMethod);
                        }
                    }
                    methodInfo = speechMethods[commandText];
                }
                else
                {
                    methodInfo = speechMethods[commandText];
                }
                return methodInfo;
            }
        }
    }
    
    namespace MyApp.Commands
    {
        class MyCommands
        {
            [Command("Show All")]
            [Command("Show All Commands")]
            [Command("Show commands")]
            public void ShowAll()
            {
                ProgramCommands.ShowAllCommands();
            }
    
            [Command("Close Window")]
            public void CloseWindow()
            {
                ControlCommands.CloseWindow();
            }
    
            [Command("Switch Window")]
            public void SwitchWindow()
            {
                ControlCommands.SwitchWindow();
            }
        }
    
        [System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
        public class CommandAttribute : System.Attribute
        {
            public string Command
            {
                get;
                set;
            }
    
            public CommandAttribute(string textValue)
            {
                this.Command = textValue;
            }
        }
    }
    

提交回复
热议问题