コード例 #1
0
        private static void WriteControllerMethodNames(ICliController controller, string message)
        {
            Console.WriteLine(message);
            List <MethodInfo> InvokeableMethods = controller.GetType().GetMethods().Where(m => Attribute.GetCustomAttributes(m).Any(a => a is CliCommand)).ToList();

            foreach (MethodInfo methodInfo in InvokeableMethods)
            {
                Console.WriteLine(methodInfo.GetMethodInvokeHelpText());
            }
        }
コード例 #2
0
        private static bool Execute(ICliController controller, string argumentsCommand, List <CommandLineArgument> argumentsArguments)
        {
            try
            {
                MethodInfo methodToInvoke = controller.GetType().GetMethods().FirstOrDefault(m =>
                                                                                             m.GetParameters().Length.Equals(argumentsArguments.Count) && Attribute.GetCustomAttributes(m)
                                                                                             .Any(a => a is CliCommand &&
                                                                                                  (a as CliCommand).Name.Equals(argumentsCommand)));

                if (methodToInvoke == null)
                {
                    WriteControllerMethodNames(controller, "the provided method name does not exist on the given controller; available options are");
                    return(false);
                }

                List <ParameterInfo> methodParams = methodToInvoke.GetParameters().ToList();
                List <object>        invokeArgs   = new List <object>();

                if (methodParams.Count != argumentsArguments.Count)
                {
                    return(false);
                }

                for (int i = 0; i < methodParams.Count; i++)
                {
                    Type t = methodParams[i].ParameterType;
                    invokeArgs.Add(TryParse(t, argumentsArguments[i].Values[0]));
                }

                methodToInvoke.Invoke(controller, invokeArgs.ToArray());
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e);
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }

            return(true);
        }