|
1 | 1 | using System; |
2 | | -using System.Collections; |
3 | 2 | using System.Collections.Generic; |
4 | 3 | using System.ComponentModel.DataAnnotations; |
5 | 4 | using System.Diagnostics; |
@@ -130,99 +129,37 @@ public virtual ApiParameterDescriptor Clone(object value) |
130 | 129 | /// <returns></returns> |
131 | 130 | protected virtual IEnumerable<IApiParameterAttribute> GetAttributes(Type parameterType, IEnumerable<IApiParameterAttribute> defined) |
132 | 131 | { |
133 | | - var attributes = new ParameterAttributeCollection(defined); |
134 | | - var isHttpContent = parameterType.IsInheritFrom<HttpContent>(); |
135 | | - var isApiParameterable = parameterType.IsInheritFrom<IApiParameterable>() || parameterType.IsInheritFrom<IEnumerable<IApiParameterable>>(); |
136 | | - |
137 | | - if (isApiParameterable == true) |
138 | | - { |
139 | | - attributes.Add(new ParameterableAttribute()); |
140 | | - } |
141 | | - else if (isHttpContent == true) |
142 | | - { |
143 | | - attributes.AddIfNotExists(new HttpContentAttribute()); |
144 | | - } |
145 | | - else if (parameterType == typeof(CancellationToken)) |
146 | | - { |
147 | | - attributes.Add(new CancellationTokenAttribute()); |
148 | | - } |
149 | | - else if (attributes.Count == 0) |
| 132 | + if (parameterType.IsInheritFrom<HttpContent>() == true) |
150 | 133 | { |
151 | | - attributes.Add(new PathQueryAttribute()); |
| 134 | + return RepeatOne<HttpContentAttribute>(); |
152 | 135 | } |
153 | | - return attributes; |
154 | | - } |
155 | 136 |
|
156 | | - /// <summary> |
157 | | - /// 表示参数特性集合 |
158 | | - /// </summary> |
159 | | - private class ParameterAttributeCollection : IEnumerable<IApiParameterAttribute> |
160 | | - { |
161 | | - /// <summary> |
162 | | - /// 特性列表 |
163 | | - /// </summary> |
164 | | - private readonly List<IApiParameterAttribute> attributeList = new List<IApiParameterAttribute>(); |
165 | | - |
166 | | - /// <summary> |
167 | | - /// 获取元素数量 |
168 | | - /// </summary> |
169 | | - public int Count |
| 137 | + if (parameterType.IsInheritFrom<IApiParameterable>() || parameterType.IsInheritFrom<IEnumerable<IApiParameterable>>()) |
170 | 138 | { |
171 | | - get => this.attributeList.Count; |
| 139 | + return RepeatOne<ParameterableAttribute>(); |
172 | 140 | } |
173 | 141 |
|
174 | | - /// <summary> |
175 | | - /// 参数特性集合 |
176 | | - /// </summary> |
177 | | - /// <param name="defined">声明的特性</param> |
178 | | - public ParameterAttributeCollection(IEnumerable<IApiParameterAttribute> defined) |
| 142 | + if (parameterType == typeof(CancellationToken)) |
179 | 143 | { |
180 | | - this.attributeList.AddRange(defined); |
| 144 | + return RepeatOne<CancellationTokenAttribute>(); |
181 | 145 | } |
182 | 146 |
|
183 | | - /// <summary> |
184 | | - /// 添加新特性 |
185 | | - /// </summary> |
186 | | - /// <param name="attribute">新特性</param> |
187 | | - public void Add(IApiParameterAttribute attribute) |
| 147 | + if (defined.Any() == false) |
188 | 148 | { |
189 | | - this.attributeList.Add(attribute); |
| 149 | + return RepeatOne<PathQueryAttribute>(); |
190 | 150 | } |
191 | 151 |
|
192 | | - /// <summary> |
193 | | - /// 添加新特性 |
194 | | - /// </summary> |
195 | | - /// <param name="attribute">新特性</param> |
196 | | - /// <returns></returns> |
197 | | - public bool AddIfNotExists(IApiParameterAttribute attribute) |
198 | | - { |
199 | | - var type = attribute.GetType(); |
200 | | - if (this.attributeList.Any(item => item.GetType() == type) == true) |
201 | | - { |
202 | | - return false; |
203 | | - } |
204 | | - |
205 | | - this.attributeList.Add(attribute); |
206 | | - return true; |
207 | | - } |
208 | | - |
209 | | - /// <summary> |
210 | | - /// 返回迭代器 |
211 | | - /// </summary> |
212 | | - /// <returns></returns> |
213 | | - public IEnumerator<IApiParameterAttribute> GetEnumerator() |
214 | | - { |
215 | | - return this.attributeList.GetEnumerator(); |
216 | | - } |
| 152 | + return defined; |
| 153 | + } |
217 | 154 |
|
218 | | - /// <summary> |
219 | | - /// 返回迭代器 |
220 | | - /// </summary> |
221 | | - /// <returns></returns> |
222 | | - IEnumerator IEnumerable.GetEnumerator() |
223 | | - { |
224 | | - return this.GetEnumerator(); |
225 | | - } |
| 155 | + /// <summary> |
| 156 | + /// 返回单次的迭代器 |
| 157 | + /// </summary> |
| 158 | + /// <typeparam name="T"></typeparam> |
| 159 | + /// <returns></returns> |
| 160 | + private static IEnumerable<T> RepeatOne<T>() where T : new() |
| 161 | + { |
| 162 | + return Enumerable.Repeat(new T(), 1); |
226 | 163 | } |
227 | 164 | } |
228 | 165 | } |
0 commit comments