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);
        }