Esempio n. 1
0
        /// <summary>
        /// Execute the methods on the specified instance that are addorned with ConsoleAction
        /// attributes that have CommandLineSwitch(es) defined that match keys in the
        /// specified ParsedArguments using the specified ILogger to report any switches not
        /// found.  An ExpectFailedException will be thrown if more than one method is found
        /// with a matching CommandLineSwitch defined in ConsoleAction attributes
        /// </summary>
        /// <param name="arguments"></param>
        /// <param name="type"></param>
        /// <param name="warnForNotFoundSwitches"></param>
        /// <param name="instance"></param>
        /// <param name="logger"></param>
        /// <returns>true if command line switches were executed otherwise false</returns>
        public static bool ExecuteSwitches(ParsedArguments arguments, Type type, bool warnForNotFoundSwitches = true, object instance = null, ILogger logger = null)
        {
            bool executed = false;

            foreach (string key in arguments.Keys)
            {
                ConsoleMethod methodToInvoke = GetConsoleMethod(arguments, type, key, instance);

                if (methodToInvoke != null)
                {
                    if (IsolateMethodCalls)
                    {
                        methodToInvoke.InvokeInSeparateAppDomain();
                    }
                    else
                    {
                        methodToInvoke.InvokeInCurrentAppDomain();
                    }
                    executed = true;
                    logger?.AddEntry("Executed {0}: {1}", key, methodToInvoke.Information);
                }
                else
                {
                    if (logger != null && warnForNotFoundSwitches)
                    {
                        logger.AddEntry("Specified command line switch was not found {0}", LogEventType.Warning, key);
                    }
                }
            }
            return(executed);
        }
Esempio n. 2
0
 protected static void ShowActions <TConsoleMethod>(List <TConsoleMethod> actions) where TConsoleMethod : ConsoleMethod
 {
     for (int i = 1; i <= actions.Count; i++)
     {
         ConsoleMethod consoleMethod = actions[i - 1];
         string        menuOption    = consoleMethod.Information;
         Console.WriteLine("{0}. {1}", i, menuOption);
     }
 }
Esempio n. 3
0
        protected internal static int InvokeSelection(List <ConsoleMethod> actions, string header, string footer, int selectedNumber)
        {
            selectedNumber -= 1;
            ConsoleMethod action = actions[selectedNumber];
            MethodInfo    method = action.Method;
            MethodInfo    invoke = typeof(ConsoleMethod).GetMethod("Invoke");

            object[] parameters = GetParameters(method);
            action.Parameters = parameters;

            if (!string.IsNullOrEmpty(header))
            {
                Out(header, ConsoleColor.White);
            }

            try
            {
                if (!method.IsStatic)
                {
                    ConstructorInfo ctor = method.DeclaringType.GetConstructor(Type.EmptyTypes);
                    if (ctor == null)
                    {
                        ExceptionHelper.Throw <InvalidOperationException>("Specified non-static method is declared on a type that has no parameterless constructor. {0}.{1}", method.DeclaringType.Name, method.Name);
                    }

                    action.Provider = ctor.Invoke(null);
                }

                if (IsolateMethodCalls)
                {
                    InvokeInSeparateAppDomain(invoke, action, parameters);
                }
                else
                {
                    InvokeInCurrentAppDomain(invoke, action, parameters);
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw;
                }
            }
            if (!string.IsNullOrEmpty(footer))
            {
                Out(footer, ConsoleColor.White);
            }

            return(selectedNumber);
        }
Esempio n. 4
0
 public static void InvokeInSeparateAppDomain(this ConsoleMethod consoleInvokeableMethod)
 {
     CommandLineInterface.InvokeInSeparateAppDomain(consoleInvokeableMethod.Method, consoleInvokeableMethod.Provider, consoleInvokeableMethod.Parameters);
 }
Esempio n. 5
0
        protected static void ShowMenu(Assembly assemblyToAnalyze, ConsoleMenu[] otherMenus, string headerText)
        {
            List <ConsoleMethod> actions = ConsoleMethod.FromAssembly <ConsoleActionAttribute>(assemblyToAnalyze);// GetConsoleInvokeableMethods<ConsoleAction>(assemblyToAnalyze);

            ShowMenu(otherMenus, headerText, actions);
        }