Esempio n. 1
0
        /// <summary>
        /// Tries to parse the provided string, extracting a value of the type
        /// described by this interface.
        /// </summary>
        /// <param name="context">Context for parsing.</param>
        /// <param name="stringToParse">The string to parse.</param>
        /// <returns>True on success; false otherwise.</returns>
        protected override object Parse(ArgumentParseContext context, string stringToParse)
        {
            if (!_commandArgType.TryParse(context, stringToParse, out object selection))
            {
                throw new ArgumentOutOfRangeException(nameof(stringToParse));
            }

            var groupOptions = new CommandGroupOptions
            {
                ServiceConfigurer = context.ServiceConfigurer
            };

            var constructorArgTypes     = new[] { typeof(CommandGroupOptions), typeof(object), typeof(object) };
            var commandGroupConstructor = Type.GetTypeInfo().GetConstructor(constructorArgTypes);

            if (commandGroupConstructor == null)
            {
                throw new InternalInvariantBrokenException($"Constructor not found in {Type.FullName}: ({string.Join(", ", constructorArgTypes.Select(ty => ty.FullName))})");
            }

            try
            {
                return(commandGroupConstructor.Invoke(new[] { groupOptions, selection, context.ContainingObject }));
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Esempio n. 2
0
        private Type ConstructCommandTypeFactory(Type inputType, out Func <ICommand> factory)
        {
            Type loopType;

            // See if it implements ICommand; if so, use it as is.
            if (typeof(ICommand).GetTypeInfo().IsAssignableFrom(inputType.GetTypeInfo()))
            {
                loopType = inputType;
                factory  = () => ConstructCommandType(inputType);
            }

            // See if it is an enum; if so, use it as the inner type for a command group.
            else if (inputType.GetTypeInfo().IsEnum)
            {
                loopType = typeof(CommandGroup <>).MakeGenericType(new[] { inputType });

                // Now make sure there's an appropriate constructor.
                var constructor = loopType.GetTypeInfo().GetConstructor(new[] { typeof(CommandGroupOptions) });
                if (constructor == null)
                {
                    throw new InternalInvariantBrokenException($"Type missing parameterless constructor, not usable for loop: {loopType.FullName}");
                }

                factory = () =>
                {
                    var groupOptions = new CommandGroupOptions
                    {
                        ServiceConfigurer = _options.ParserOptions.ServiceConfigurer
                    };

                    return((ICommand)constructor.Invoke(new object[] { groupOptions }));
                };
            }

            // Otherwise, we can't do anything with it.
            else
            {
                throw new NotSupportedException($"Type not supported as command for loop: {inputType.FullName}");
            }

            return(loopType);
        }
 public CommandGroupOptionsAttribute(CommandGroupOptions options)
 {
     this.CommandGroupOptions = options;
 }
 public CommandGroupOptionsAttribute(CommandGroupOptions options)
 {
     this.CommandGroupOptions = options;
 }