/// <summary> /// 生成ApiParameterDescriptor /// </summary> /// <param name="parameter">参数信息</param> /// <returns></returns> private static ApiParameterDescriptor GetParameterDescriptor(ParameterInfo parameter) { var parameterType = parameter.ParameterType; var parameterAlias = parameter.GetCustomAttribute(typeof(AliasAsAttribute)) as AliasAsAttribute; var parameterName = parameterAlias == null ? parameter.Name : parameterAlias.Name; var isHttpContent = parameterType.IsInheritFrom <HttpContent>(); var isApiParameterable = parameterType.IsInheritFrom <IApiParameterable>() || parameterType.IsInheritFrom <IEnumerable <IApiParameterable> >(); var defined = parameter.GetAttributes <IApiParameterAttribute>(true); var attributes = new ParameterAttributeCollection(defined); if (isApiParameterable == true) { attributes.Add(new ParameterableAttribute()); } else if (isHttpContent == true) { attributes.AddIfNotExists(new HttpContentAttribute()); } else if (attributes.Count == 0) { attributes.Add(new PathQueryAttribute()); } return(new ApiParameterDescriptor { Value = null, Member = parameter, Name = parameterName, Index = parameter.Position, Attributes = attributes.ToArray(), ParameterType = parameterType }); }
/// <summary> /// 返回参数特性 /// </summary> /// <param name="parameterType">参数类型</param> /// <param name="defined">参数上声明的特性</param> /// <returns></returns> public virtual IApiParameterAttribute[] GetAttributes(Type parameterType, IEnumerable <IApiParameterAttribute> defined) { var attributes = new ParameterAttributeCollection(defined); var isHttpContent = parameterType.IsInheritFrom <HttpContent>(); var isApiParameterable = parameterType.IsInheritFrom <IApiParameterable>() || parameterType.IsInheritFrom <IEnumerable <IApiParameterable> >(); if (isApiParameterable == true) { attributes.Add(new ParameterableAttribute()); } else if (isHttpContent == true) { attributes.AddIfNotExists(new HttpContentAttribute()); } else if (parameterType == typeof(CancellationToken)) { attributes.Add(new CancellationTokenAttribute()); } else if (attributes.Count == 0) { attributes.Add(new PathQueryAttribute()); } return(attributes.ToArray()); }