private Dictionary <string, CommandInfo> FindCommands()
        {
            var result = new Dictionary <string, CommandInfo>();

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes().Where(TypeIsCommand))
                {
                    var commandInfo = CommandMetaDataHelper.GetCommandInfo(type);
                    if (result.ContainsKey(commandInfo.Name))
                    {
                        if (Output != null)
                        {
                            Output.WriteWarning(string.Format("Command with name [{0}]({1}) already exists.", commandInfo.Name, type.FullName));
                        }
                    }
                    else
                    {
                        result[commandInfo.Name] = commandInfo;
                    }
                }
            }

            return(result);
        }
Example #2
0
        public static object CreateCommand(
            Type commandType,
            string[] args,
            IOutput output,
            IEnumerable <IArgValueSource> additionalArgValueSources = null)
        {
            var commandArgumentPropertyInfos = CommandMetaDataHelper.GetCommandArgumentPropertyInfos(commandType);

            var command = Activator.CreateInstance(commandType);

            CommandMetaDataHelper.SetOutputProperty(command, output);

            var commandContext = new CommandContext(args, additionalArgValueSources);

            var errors = new List <string>();

            foreach (var commandArgumentPropertyInfo in commandArgumentPropertyInfos)
            {
                try
                {
                    var commandArgumentAttribute = CommandMetaDataHelper.GetArgumentInfo(commandArgumentPropertyInfo);
                    var value = commandContext.GetArgValue(commandArgumentAttribute);
                    commandArgumentPropertyInfo.SetValue(command, value, null);
                }
                catch (Exception e)
                {
                    errors.Add(e.Message);
                }
            }

            if (errors.Count > 0)
            {
                throw new Exception(string.Join(Environment.NewLine, errors));
            }

            return(command);
        }