Exemple #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;
            }
        }
Exemple #2
0
        protected override object Parse(ArgumentParseContext context, string stringToParse)
        {
            if (!_commandArgType.TryParse(context, stringToParse, out object selection))
            {
                throw new ArgumentOutOfRangeException(nameof(stringToParse));
            }

            var commandGroupConstructor = Type.GetTypeInfo().GetConstructor(new[] { _commandTypeType });

            if (commandGroupConstructor == null)
            {
                throw new ArgumentOutOfRangeException(nameof(stringToParse));
            }

            var group = commandGroupConstructor.Invoke(new[] { selection });

            if (group == null)
            {
                throw new ArgumentOutOfRangeException(nameof(stringToParse));
            }

            return(group);
        }