Пример #1
0
        /// <summary>
        /// 请求Api的参数描述
        /// </summary>
        /// <param name="parameter">参数信息</param>
        /// <param name="defaultAtribute">缺省特性时使用的默认特性</param>
        /// <exception cref="ArgumentNullException"></exception>
        public DefaultApiParameterDescriptor(ParameterInfo parameter, IApiParameterAttribute defaultAtribute)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            var parameterAttributes = parameter.GetCustomAttributes().ToArray();

            var parameterType        = parameter.ParameterType;
            var parameterAlias       = parameterAttributes.OfType <AliasAsAttribute>().FirstOrDefault();
            var parameterName        = parameterAlias == null ? parameter.Name : parameterAlias.Name;
            var validationAttributes = parameterAttributes.OfType <ValidationAttribute>().ToReadOnlyList();

            this.Member               = parameter;
            this.Name                 = parameterName ?? string.Empty;
            this.Index                = parameter.Position;
            this.ParameterType        = parameterType;
            this.ValidationAttributes = validationAttributes;

            var attributes = this.GetAttributes(parameter, parameterAttributes).ToArray();

            if (attributes.Length == 0)
            {
                this.Attributes = new[] { defaultAtribute }.ToReadOnlyList();
            }
            else
            {
                this.Attributes = attributes.ToReadOnlyList();
            }
        }
            /// <summary>
            /// 添加新特性
            /// </summary>
            /// <param name="attribute"></param>
            /// <returns></returns>
            public bool AddIfNotExists(IApiParameterAttribute attribute)
            {
                var type = attribute.GetType();

                if (this.attribueList.Any(item => item.GetType() == type) == true)
                {
                    return(false);
                }

                this.attribueList.Add(attribute);
                return(true);
            }
 /// <summary>
 /// 添加新特性
 /// </summary>
 /// <param name="attribute"></param>
 public void Add(IApiParameterAttribute attribute)
 {
     this.attribueList.Add(attribute);
 }