@@ -48,13 +48,13 @@ private static ApiActionDescriptor GetActionDescriptor(MethodInfo method)
4848 {
4949 var actionAttributes = method
5050 . FindDeclaringAttributes < IApiActionAttribute > ( true )
51- . Distinct ( new AttributeComparer < IApiActionAttribute > ( ) )
51+ . Distinct ( new MultiplableComparer < IApiActionAttribute > ( ) )
5252 . OrderBy ( item => item . OrderIndex )
5353 . ToArray ( ) ;
5454
5555 var filterAttributes = method
5656 . FindDeclaringAttributes < IApiActionFilterAttribute > ( true )
57- . Distinct ( new AttributeComparer < IApiActionFilterAttribute > ( ) )
57+ . Distinct ( new MultiplableComparer < IApiActionFilterAttribute > ( ) )
5858 . OrderBy ( item => item . OrderIndex )
5959 . ToArray ( ) ;
6060
@@ -81,34 +81,32 @@ private static ApiParameterDescriptor GetParameterDescriptor(ParameterInfo param
8181
8282 var descriptor = new ApiParameterDescriptor
8383 {
84+ Attributes = null ,
8485 Value = null ,
8586 Name = parameterName ,
8687 Index = parameter . Position ,
8788 ParameterType = parameterType ,
8889 IsApiParameterable = parameterType . IsInheritFrom < IApiParameterable > ( ) || parameterType . IsInheritFrom < IEnumerable < IApiParameterable > > ( ) ,
8990 IsHttpContent = parameterType . IsInheritFrom < HttpContent > ( ) ,
90- IsSimpleType = parameterType . IsSimple ( ) ,
91- IsEnumerable = parameterType . IsInheritFrom < IEnumerable > ( ) ,
92- IsDictionaryOfObject = parameterType . IsInheritFrom < IDictionary < string , object > > ( ) ,
93- IsDictionaryOfString = parameterType . IsInheritFrom < IDictionary < string , string > > ( ) ,
94- Attributes = parameter . GetAttributes < IApiParameterAttribute > ( true ) . ToArray ( )
9591 } ;
9692
97- if ( descriptor . Attributes . Length == 0 )
93+ var defined = parameter . GetAttributes < IApiParameterAttribute > ( true ) ;
94+ var attributes = new ParameterAttributeCollection ( defined ) ;
95+
96+ if ( descriptor . IsApiParameterable == true )
9897 {
99- if ( descriptor . IsApiParameterable == true )
100- {
101- descriptor . Attributes = new [ ] { new ParameterableAttribute ( ) } ;
102- }
103- else if ( descriptor . IsHttpContent == true )
104- {
105- descriptor . Attributes = new [ ] { new HttpContentAttribute ( ) } ;
106- }
107- else
108- {
109- descriptor . Attributes = new [ ] { new PathQueryAttribute ( ) } ;
110- }
98+ attributes . Add ( new ParameterableAttribute ( ) ) ;
11199 }
100+ else if ( descriptor . IsHttpContent == true )
101+ {
102+ attributes . AddIfNotExists ( new HttpContentAttribute ( ) ) ;
103+ }
104+ else if ( attributes . Count == 0 )
105+ {
106+ attributes . Add ( new PathQueryAttribute ( ) ) ;
107+ }
108+
109+ descriptor . Attributes = attributes . ToArray ( ) ;
112110 return descriptor ;
113111 }
114112
@@ -138,17 +136,83 @@ private static ApiReturnDescriptor GetReturnDescriptor(MethodInfo method)
138136 }
139137
140138 /// <summary>
141- /// 特性比较器
139+ /// 表示参数特性集合
140+ /// </summary>
141+ private class ParameterAttributeCollection
142+ {
143+ /// <summary>
144+ /// 特性列表
145+ /// </summary>
146+ private readonly List < IApiParameterAttribute > attribueList = new List < IApiParameterAttribute > ( ) ;
147+
148+ /// <summary>
149+ /// 获取元素数量
150+ /// </summary>
151+ public int Count
152+ {
153+ get
154+ {
155+ return this . attribueList . Count ;
156+ }
157+ }
158+
159+ /// <summary>
160+ /// 参数特性集合
161+ /// </summary>
162+ /// <param name="defined">声明的特性</param>
163+ public ParameterAttributeCollection ( IEnumerable < IApiParameterAttribute > defined )
164+ {
165+ this . attribueList . AddRange ( defined ) ;
166+ }
167+
168+ /// <summary>
169+ /// 添加新特性
170+ /// </summary>
171+ /// <param name="attribute"></param>
172+ public void Add ( IApiParameterAttribute attribute )
173+ {
174+ this . attribueList . Add ( attribute ) ;
175+ }
176+
177+ /// <summary>
178+ /// 添加新特性
179+ /// </summary>
180+ /// <param name="attribute"></param>
181+ /// <returns></returns>
182+ public bool AddIfNotExists ( IApiParameterAttribute attribute )
183+ {
184+ var type = attribute . GetType ( ) ;
185+ if ( this . attribueList . Any ( item => item . GetType ( ) == type ) == true )
186+ {
187+ return false ;
188+ }
189+
190+ this . attribueList . Add ( attribute ) ;
191+ return true ;
192+ }
193+
194+ /// <summary>
195+ /// 转换为数组
196+ /// </summary>
197+ /// <returns></returns>
198+ public IApiParameterAttribute [ ] ToArray ( )
199+ {
200+ return this . attribueList . ToArray ( ) ;
201+ }
202+ }
203+
204+ /// <summary>
205+ /// 是否允许重复的特性比较器
142206 /// </summary>
143- private class AttributeComparer < T > : IEqualityComparer < T > where T : IAttributeMultiplable
207+ private class MultiplableComparer < TAttributeMultiplable > : IEqualityComparer < TAttributeMultiplable > where TAttributeMultiplable : IAttributeMultiplable
144208 {
145209 /// <summary>
146210 /// 是否相等
147211 /// </summary>
148212 /// <param name="x"></param>
149213 /// <param name="y"></param>
150214 /// <returns></returns>
151- public bool Equals ( T x , T y )
215+ public bool Equals ( TAttributeMultiplable x , TAttributeMultiplable y )
152216 {
153217 // 如果其中一个不允许重复,返回true将y过滤
154218 return x . AllowMultiple == false || y . AllowMultiple == false ;
@@ -159,7 +223,7 @@ public bool Equals(T x, T y)
159223 /// </summary>
160224 /// <param name="obj"></param>
161225 /// <returns></returns>
162- public int GetHashCode ( T obj )
226+ public int GetHashCode ( TAttributeMultiplable obj )
163227 {
164228 return obj . GetType ( ) . GetHashCode ( ) ;
165229 }
0 commit comments