private static void BuildParameter(ParameterBuilder builder, System.Reflection.ParameterInfo paramInfo, int position, int count, CommandService service, IServiceProvider services)
        {
            var attributes = paramInfo.GetCustomAttributes();
            var paramType  = paramInfo.ParameterType;

            builder.Name = paramInfo.Name;

            builder.IsOptional   = paramInfo.IsOptional;
            builder.DefaultValue = paramInfo.HasDefaultValue ? paramInfo.DefaultValue : null;

            foreach (var attribute in attributes)
            {
                switch (attribute)
                {
                case SummaryAttribute summary:
                    builder.Summary = summary.Text;
                    break;

                case OverrideTypeReaderAttribute typeReader:
                    builder.TypeReader = GetTypeReader(service, paramType, typeReader.TypeReader, services);
                    break;

                case ParamArrayAttribute _:
                    builder.IsMultiple = true;
                    paramType          = paramType.GetElementType();
                    break;

                case ParameterPreconditionAttribute precon:
                    builder.AddPrecondition(precon);
                    break;

                case NameAttribute name:
                    builder.Name = name.Text;
                    break;

                case RemainderAttribute _:
                    if (position != count - 1)
                    {
                        throw new InvalidOperationException($"Remainder parameters must be the last parameter in a command. Parameter: {paramInfo.Name} in {paramInfo.Member.DeclaringType.Name}.{paramInfo.Member.Name}");
                    }

                    builder.IsRemainder = true;
                    break;

                default:
                    builder.AddAttributes(attribute);
                    break;
                }
            }

            builder.ParameterType = paramType;

            if (builder.TypeReader == null)
            {
                builder.TypeReader = service.GetDefaultTypeReader(paramType)
                                     ?? service.GetTypeReaders(paramType)?.FirstOrDefault().Value;
            }
        }
Example #2
0
            void CreateArgument(ParameterBuilder builder)
            {
                var preconditions = attributes.Where(a => a is ParameterPreconditionAttribute);

                foreach (var precondition in preconditions)
                {
                    builder.AddPrecondition(precondition as ParameterPreconditionAttribute);
                }
                builder.AddAttributes(attributes.Where(a => !(a is ParameterPreconditionAttribute)).ToArray());

                builder.IsOptional   = parameter.IsOptional;
                builder.DefaultValue = parameter.HasDefaultValue ? parameter.DefaultValue : null;

                builder.WithSummary(this._metadataProvider.GetArgumentValue(a => a.Summary));

                var overrideTypeReader = this._metadataProvider.GetArgumentValue(a => a.TypeReader);

                if (overrideTypeReader != null)
                {
                    builder.TypeReader = this.GetTypeReader(builder.ParameterType, overrideTypeReader);
                }

                var remainder = this._metadataProvider.GetArgumentValue(a => a.Remainder);

                if (remainder || position.current == position.total - 1)
                {
                    if (position.current != position.total - 1)
                    {
                        throw new InvalidOperationException(
                                  $"Remainder parameters must be the last parameter in a command. Paramerter: {parameter.Name} in {parameter.Member?.DeclaringType?.FullName ?? "Unknown"}.{parameter.Member.Name}");
                    }
                    builder.IsRemainder = true;
                }

                if (builder.TypeReader == null)
                {
                    var        readers = this._commandService.TypeReaders.FirstOrDefault(t => t.Key == builder.ParameterType);
                    TypeReader reader;

                    if (readers != null)
                    {
                        reader = readers.FirstOrDefault();
                    }
                    else
                    {
                        reader = (TypeReader)typeof(CommandService).GetMethod("GetDefaultTypeReader")?.Invoke(this._commandService, new Object[] { builder.ParameterType }) ?? null;
                    }

                    builder.TypeReader = reader;
                }
            }