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 <CommandAttribute>().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);
        }
        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);
        }
예제 #3
0
        static void Main(string[] args)
        {
            #region Application appearance

            Cabinet myCabinet = new Cabinet();
            Console.ForegroundColor = ConsoleColor.Cyan;

            Console.WriteLine(myCabinet.Name + "\n");
            myCabinet.GetDescription();
            Console.ForegroundColor = color;

            Console.WriteLine("The following commands are available:\n ");

            var column1 = new List <string>();
            var column2 = new List <string>();
            FileCabinetCommands.FillLists(column1, column2);

            var maxWidth     = column1.Max(s => s.Length);
            var formatString = string.Format("{{0, -{0}}} |{{1,{1}}}", maxWidth, 4);

            Console.WriteLine(formatString, "Request description: ", "Command:\n ");
            for (int i = 0; i < column1.Count; i++)
            {
                Console.Write(formatString, column1[i], column2[i]);
                Console.WriteLine();
            }

            Console.WriteLine("\n" + @"If you want to exit just type ""exit"".");

            #endregion

            MyCommands.FillDictionary();

            while (alive)
            {
                Console.WriteLine();
                try
                {
                    command = Console.ReadLine();

                    MyCommands.ParseInput(command);
                    if (MyCommands.del != null && attempsToUseDelegate == 1)
                    {
                        attempsToUseDelegate--;
                        MyCommands.del(myCabinet);
                    }
                }
                catch (InvalidNameException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.message);
                    Console.ForegroundColor = color;
                }
                catch (InvalidDayOfBirthException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.message);
                    Console.ForegroundColor = color;
                }
                catch (FileNotFoundException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.message);
                    Console.ForegroundColor = color;
                }
                catch (UserAlreadyExistsException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.message);
                    Console.ForegroundColor = color;
                }
                catch (UserNotExistsException e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.message);
                    Console.ForegroundColor = color;
                }
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            CommandObject <MyCommands> obj = new();       // 创建一个命令行对象

            IArguParser[] parsers = new IArguParser[]
            {
                new PropertyArguParser("-"),
                new FieldArguParser(':'),
                new ArguParser(),
            };

            obj.CommandUnresolved += (_s, _e) =>
            {
                if (_e.CommandName != "FFF")
                {
                    _e.Handled = true;
                }
            };

            if (args.Length > 0)
            {
                CommandParser.SplitCommandLineFromStartupArgs(out CommandSegment[] rst);
                obj.ExecuteCommand(rst);
            }

            Console.WriteLine("Easy command. Copyright 2021 Null.\n");
            while (true)
            {
                string cmdline = MyCommands.NextCommandString();
                if (cmdline == null)
                {
                    return;
                }
                if (string.IsNullOrWhiteSpace(cmdline))
                {
                    continue;
                }
#if false
                var result = obj.ExecuteCommand(parsers, cmdline, true);
                if (result != null)
                {
                    Console.WriteLine(result);
                }
#else
                try
                {
                    var result = obj.ExecuteCommand(parsers, cmdline, true);
                    if (result != null)
                    {
                        Console.WriteLine(result);
                    }
                }
                catch (CommandException)
                {
                    Console.Error.WriteLine("Syntax error: can't execute command.");
                }
                catch (TargetInvocationException)
                {
                    Console.Error.WriteLine("Method error: exception thrown when execute method.");
                }
                catch (Exception)
                {
                    Console.Error.WriteLine("Unexpected exception");
                }
#endif
            }
        }