コード例 #1
0
        public static ParameterBuilder CreateParameterBuilder(CommandBuilder command, ParameterInfo parameterInfo, bool last)
        {
            var builder    = new ParameterBuilder(parameterInfo.ParameterType, command);
            var attributes = parameterInfo.GetCustomAttributes(false);

            for (var i = 0; i < attributes.Length; i++)
            {
                switch (attributes[i])
                {
                case NameAttribute nameAttribute:
                    builder.WithName(nameAttribute.Value);
                    break;

                case DescriptionAttribute descriptionAttribute:
                    builder.WithDescription(descriptionAttribute.Value);
                    break;

                case RemarksAttribute remarksAttribute:
                    builder.WithRemarks(remarksAttribute.Value);
                    break;

                case ParamArrayAttribute _:
                    if (!last)
                    {
                        throw new ParameterBuildingException(builder, $"A params array parameter must be the last parameter in a command. Parameter: {parameterInfo.Name} in {parameterInfo.Member.Name} in {parameterInfo.Member.DeclaringType}.");
                    }

                    builder.WithIsMultiple(true);
                    builder.Type = parameterInfo.ParameterType.GetElementType();
                    break;

                case RemainderAttribute _:
                    if (!last)
                    {
                        throw new ParameterBuildingException(builder, $"A remainder parameter must be the last parameter in a command. Parameter: {parameterInfo.Name} in {parameterInfo.Member.Name} in {parameterInfo.Member.DeclaringType}.");
                    }

                    builder.WithIsRemainder(true);
                    break;

                case OverrideTypeParserAttribute overrideTypeParserAttribute:
                    builder.WithCustomTypeParserType(overrideTypeParserAttribute.Value);

                    if (overrideTypeParserAttribute.GetType() != typeof(OverrideTypeParserAttribute))
                    {
                        builder.AddAttribute(overrideTypeParserAttribute);
                    }
                    break;

                case ParameterCheckAttribute parameterCheckAttribute:
                    builder.AddCheck(parameterCheckAttribute);
                    break;

                case Attribute attribute:
                    builder.AddAttribute(attribute);
                    break;
                }
            }

            if (parameterInfo.HasDefaultValue)
            {
                builder.WithIsOptional(true)
                .WithDefaultValue(parameterInfo.DefaultValue);
            }

            else if (builder.IsMultiple)
            {
                builder.WithIsOptional(true)
                .WithDefaultValue(Array.CreateInstance(builder.Type, 0));
            }

            if (builder.Name == null)
            {
                builder.WithName(parameterInfo.Name);
            }

            return(builder);
        }
コード例 #2
0
ファイル: ReflectionUtils.cs プロジェクト: Still34/Qmmands
        public static ParameterBuilder BuildParameter(ParameterInfo parameterInfo, bool last)
        {
            var builder    = new ParameterBuilder();
            var attributes = parameterInfo.GetCustomAttributes(false);

            for (var i = 0; i < attributes.Length; i++)
            {
                switch (attributes[i])
                {
                case NameAttribute nameAttribute:
                    builder.WithName(nameAttribute.Name);
                    break;

                case DescriptionAttribute descriptionAttribute:
                    builder.WithDescription(descriptionAttribute.Description);
                    break;

                case RemarksAttribute remarksAttribute:
                    builder.WithRemarks(remarksAttribute.Remarks);
                    break;

                case ParamArrayAttribute _:
                    if (!last)
                    {
                        throw new InvalidOperationException($"A params array parameter must be the last parameter in a command. Parameter: {parameterInfo.Name} in {parameterInfo.Member.Name} in {parameterInfo.Member.DeclaringType.Name}.");
                    }

                    builder.WithIsMultiple(true)
                    .WithType(parameterInfo.ParameterType.GetElementType());
                    break;

                case RemainderAttribute _:
                    if (!last)
                    {
                        throw new InvalidOperationException($"A remainder parameter must be the last parameter in a command. Parameter: {parameterInfo.Name} in {parameterInfo.Member.Name} in {parameterInfo.Member.DeclaringType.Name}.");
                    }

                    builder.WithIsRemainder(true);
                    break;

                case OverwriteTypeParserAttribute overwriteTypeParserAttribute:
                    builder.CustomTypeParserType = overwriteTypeParserAttribute.CustomTypeParserType;
                    break;

                case ParameterCheckBaseAttribute parameterCheckAttribute:
                    builder.AddCheck(parameterCheckAttribute);
                    break;

                case Attribute attribute:
                    builder.AddAttribute(attribute);
                    break;
                }
            }

            if (parameterInfo.HasDefaultValue)
            {
                builder.WithIsOptional(true)
                .WithDefaultValue(parameterInfo.DefaultValue);
            }

            if (!builder.IsMultiple)
            {
                builder.WithType(parameterInfo.ParameterType);
            }

            if (builder.Name is null)
            {
                builder.WithName(parameterInfo.Name);
            }

            return(builder);
        }