Exemplo n.º 1
0
        public VerbTaskResult ExecuteTask(string verb, string[] args)
        {
            if (!this.Verbs.Contains(verb))
            {
                Console.Write($"'{verb}' is not a valid task. ");
                this.Verbs["list"].Execute(null, null);
                return(1);
            }

            var task           = this.Verbs[verb];
            var namedArgs      = this.Parser.ParseNamedArguments(args);
            var posArgs        = this.Parser.ParsePositionalArguments(args);
            var typedArguments = Instantiate.CreateInstance(task.ArgumentType);

            foreach (KeyValuePair <string, string> namedArg in namedArgs)
            {
                foreach ((var prop, var converted) in from prop in task.ArgumentType.GetProperties()
                         let attr = prop.GetCustomAttribute <NamedArgumentAttribute>()
                                    where attr != null
                                    where attr.LongName.Equals(namedArg.Key, StringComparison.OrdinalIgnoreCase) ||
                                    attr.ShortName.Equals(namedArg.Key, StringComparison.OrdinalIgnoreCase)
                                    let converter = TypeDescriptor.GetConverter(prop.PropertyType)
                                                    where converter != null
                                                    let converted = converter.ConvertFromInvariantString(namedArg.Value)
                                                                    select(prop, converted)
                         )
                {
                    prop.SetValue(typedArguments, converted);
                }
            }

            foreach (KeyValuePair <int, string> posArg in posArgs)
            {
                foreach ((var prop, var converted) in from prop in task.ArgumentType.GetProperties()
                         let attr = prop.GetCustomAttribute <PositionalArgumentAttribute>()
                                    where attr != null
                                    where attr.Position == posArg.Key
                                    let converter = TypeDescriptor.GetConverter(prop.PropertyType)
                                                    where converter != null
                                                    let converted = converter.ConvertFromInvariantString(posArg.Value)
                                                                    select(prop, converted)
                         )
                {
                    prop.SetValue(typedArguments, converted);
                }
            }

            return(task.Execute(typedArguments, args));
        }
Exemplo n.º 2
0
 public static object CreateInstance(Type createType)
 {
     return(Instantiate.CreateInstance(createType, Type.EmptyTypes));
 }