Skip to content

Commit 3f2e499

Browse files
committed
强制应用特定类型对应的参数特性
1 parent e540741 commit 3f2e499

File tree

1 file changed

+18
-81
lines changed

1 file changed

+18
-81
lines changed

WebApiClient/Contexts/ApiParameterDescriptor.cs

Lines changed: 18 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.ComponentModel.DataAnnotations;
54
using System.Diagnostics;
@@ -130,99 +129,37 @@ public virtual ApiParameterDescriptor Clone(object value)
130129
/// <returns></returns>
131130
protected virtual IEnumerable<IApiParameterAttribute> GetAttributes(Type parameterType, IEnumerable<IApiParameterAttribute> defined)
132131
{
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)
150133
{
151-
attributes.Add(new PathQueryAttribute());
134+
return RepeatOne<HttpContentAttribute>();
152135
}
153-
return attributes;
154-
}
155136

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>>())
170138
{
171-
get => this.attributeList.Count;
139+
return RepeatOne<ParameterableAttribute>();
172140
}
173141

174-
/// <summary>
175-
/// 参数特性集合
176-
/// </summary>
177-
/// <param name="defined">声明的特性</param>
178-
public ParameterAttributeCollection(IEnumerable<IApiParameterAttribute> defined)
142+
if (parameterType == typeof(CancellationToken))
179143
{
180-
this.attributeList.AddRange(defined);
144+
return RepeatOne<CancellationTokenAttribute>();
181145
}
182146

183-
/// <summary>
184-
/// 添加新特性
185-
/// </summary>
186-
/// <param name="attribute">新特性</param>
187-
public void Add(IApiParameterAttribute attribute)
147+
if (defined.Any() == false)
188148
{
189-
this.attributeList.Add(attribute);
149+
return RepeatOne<PathQueryAttribute>();
190150
}
191151

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+
}
217154

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);
226163
}
227164
}
228165
}

0 commit comments

Comments
 (0)