Skip to content

Commit f61f267

Browse files
committed
增加DataCollection.TryGetValue<T>()重载方法;
增加ApiRequestContext.TryGetArgument<T>()重载方法;
1 parent 46efd07 commit f61f267

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

WebApiClientCore/ApiRequestContext.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Net.Http;
34

45
namespace WebApiClientCore
@@ -65,6 +66,39 @@ public bool TryGetArgument(string parameterName, out object? value)
6566
return this.TryGetArgument(parameterName, StringComparer.Ordinal, out value);
6667
}
6768

69+
/// <summary>
70+
/// 尝试根据参数名获取参数值
71+
/// </summary>
72+
/// <typeparam name="TValue"></typeparam>
73+
/// <param name="parameterName">参数名</param>
74+
/// <param name="value">值</param>
75+
/// <returns></returns>
76+
public bool TryGetArgument<TValue>(string parameterName, [MaybeNull] out TValue value)
77+
{
78+
return this.TryGetArgument(parameterName, StringComparer.Ordinal, out value);
79+
}
80+
81+
/// <summary>
82+
/// 尝试根据参数名获取参数值
83+
/// </summary>
84+
/// <typeparam name="TValue"></typeparam>
85+
/// <param name="parameterName">参数名</param>
86+
/// <param name="comparer">比较器</param>
87+
/// <param name="value">值</param>
88+
/// <returns></returns>
89+
public bool TryGetArgument<TValue>(string parameterName, StringComparer comparer, [MaybeNull] out TValue value)
90+
{
91+
if (this.TryGetArgument(parameterName, comparer, out var objValue) && objValue is TValue tValue)
92+
{
93+
value = tValue;
94+
return true;
95+
}
96+
97+
value = default;
98+
return false;
99+
}
100+
101+
68102
/// <summary>
69103
/// 尝试根据参数名获取参数值
70104
/// </summary>

WebApiClientCore/DataCollection.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56

67
namespace WebApiClientCore
@@ -51,20 +52,36 @@ public bool ContainsKey(object key)
5152
}
5253

5354
/// <summary>
54-
/// 读取指定的键并强制转换为目标类型
55+
/// 读取指定的键并尝试转换为目标类型
56+
/// 失败则返回目标类型的default值
5557
/// </summary>
56-
/// <typeparam name="T"></typeparam>
58+
/// <typeparam name="TValue"></typeparam>
5759
/// <param name="key">键</param>
5860
/// <returns></returns>
59-
public T Get<T>(object key)
61+
[return: MaybeNull]
62+
public TValue Get<TValue>(object key)
6063
{
61-
#nullable disable
62-
if (this.TryGetValue(key, out var value))
64+
this.TryGetValue<TValue>(key, out var value);
65+
return value;
66+
}
67+
68+
/// <summary>
69+
/// 尝试获取值
70+
/// </summary>
71+
/// <typeparam name="TValue"></typeparam>
72+
/// <param name="key">键</param>
73+
/// <param name="value">值</param>
74+
/// <returns></returns>
75+
public bool TryGetValue<TValue>(object key, [MaybeNull] out TValue value)
76+
{
77+
if (this.TryGetValue(key, out var objValue) && objValue is TValue tValue)
6378
{
64-
return value == null ? default : (T)value;
79+
value = tValue;
80+
return true;
6581
}
66-
return default;
67-
#nullable enable
82+
83+
value = default;
84+
return false;
6885
}
6986

7087
/// <summary>

WebApiClientCore/System.Net.Http/HttpResponseMessageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static async Task SaveAsAsync(this HttpResponseMessage httpResponse, Stre
115115
{
116116
recvSize += length;
117117
await destination.WriteAsync(buffer, 0, length, cancellationToken).ConfigureAwait(false);
118-
await destination.FlushAsync(cancellationToken);
118+
await destination.FlushAsync(cancellationToken).ConfigureAwait(false);
119119
}
120120

121121
progressChanged.Invoke(new HttpProgress(fileSize, recvSize, isCompleted));

0 commit comments

Comments
 (0)