PowerArgs.CommandLineAction.Create C# (CSharp) Method

Create() static private method

static private Create ( MethodInfo actionMethod, List knownAliases ) : CommandLineAction
actionMethod System.Reflection.MethodInfo
knownAliases List
return CommandLineAction
        internal static CommandLineAction Create(MethodInfo actionMethod, List<string> knownAliases)
        {
            var ret = PropertyInitializer.CreateInstance<CommandLineAction>();
            ret.ActionMethod = actionMethod;

            ret.Source = actionMethod;
            ret.Aliases.Add(actionMethod.Name);

            ret.Metadata.AddRange(actionMethod.Attrs<IArgMetadata>().AssertAreAllInstanceOf<ICommandLineActionMetadata>());

            ret.IgnoreCase = true;

            if (actionMethod.DeclaringType.HasAttr<ArgIgnoreCase>() && actionMethod.DeclaringType.Attr<ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            if (actionMethod.HasAttr<ArgIgnoreCase>() && actionMethod.Attr<ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            if (actionMethod.GetParameters().Length == 1 && ArgRevivers.CanRevive(actionMethod.GetParameters()[0].ParameterType) == false)
            {
                ret.Arguments.AddRange(actionMethod.GetParameters()[0].ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p, knownAliases)));
            }
            else if (actionMethod.GetParameters().Length > 0 && actionMethod.GetParameters().Where(p => ArgRevivers.CanRevive(p.ParameterType) == false).Count() == 0)
            {
                ret.Arguments.AddRange(actionMethod.GetParameters().Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p)));
                foreach (var arg in (ret.Arguments).Where(a => a.Position >= 0))
                {
                    arg.Position++; // Since position 0 is reserved for the action specifier
                }
            }
            else if (actionMethod.GetParameters().Length > 0)
            {
                throw new InvalidArgDefinitionException("Your action method contains a parameter that cannot be revived on its own.  That is only valid if the non-revivable parameter is the only parameter.  In that case, the properties of that parameter type will be used.");
            }

            return ret;
        }

Same methods

CommandLineAction::Create ( PropertyInfo actionProperty, List knownAliases ) : CommandLineAction

Usage Example

Beispiel #1
0
        private List <CommandLineAction> FindCommandLineActions(Type t)
        {
            var knownAliases = new List <string>();

            foreach (var argument in Arguments)
            {
                knownAliases.AddRange(argument.Aliases);
            }

            BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public;

            var actions = (from p in t.GetProperties(flags)
                           where  CommandLineAction.IsActionImplementation(p)
                           select CommandLineAction.Create(p, knownAliases)).ToList();

            if (t.HasAttr <ArgActionType>())
            {
                t = t.Attr <ArgActionType>().ActionType;
            }

            foreach (var action in t.GetMethods(flags).Where(m => CommandLineAction.IsActionImplementation(m)).Select(m => CommandLineAction.Create(m, knownAliases.ToList())))
            {
                var matchingPropertyBasedAction = actions.Where(a => a.Aliases.First() == action.Aliases.First()).SingleOrDefault();
                if (matchingPropertyBasedAction != null)
                {
                    continue;
                }
                actions.Add(action);
            }

            return(actions);
        }
All Usage Examples Of PowerArgs.CommandLineAction::Create