Pārlūkot izejas kodu

新增ObjectJsonConverter
修正Dictionary.TryAdd函数二义性

懒得勤快 1 gadu atpakaļ
vecāks
revīzija
281b2cc063

+ 160 - 161
Masuit.Tools.Abstractions/Extensions/BaseType/IDictionaryExtensions.cs

@@ -7,6 +7,9 @@ using System.Threading.Tasks;
 
 namespace Masuit.Tools;
 
+/// <summary>
+/// 字典扩展
+/// </summary>
 public static class IDictionaryExtensions
 {
     /// <summary>
@@ -52,53 +55,69 @@ public static class IDictionaryExtensions
     /// 添加或更新键值对
     /// </summary>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
-    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
+    /// <param name="key">键</param>
+    /// <param name="addValue">添加时的值</param>
+    /// <param name="updateValueFactory">更新时的操作</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        foreach (var item in @this)
+        if ([email protected](key, addValue))
         {
-            that[item.Key] = item.Value;
+            @this[key] = updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
-    public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
+    /// <param name="key">键</param>
+    /// <param name="addValue">添加时的值</param>
+    /// <param name="updateValueFactory">更新时的操作</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        foreach (var item in @this)
+        if ([email protected](key, addValue))
         {
-            that[item.Key] = item.Value;
+            @this[key] = updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
+    /// <typeparam name="TKey"></typeparam>
+    /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
-    public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
+    /// <param name="key">键</param>
+    /// <param name="addValue">添加时的值</param>
+    /// <param name="updateValueFactory">更新时的操作</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        foreach (var item in @this)
+        if ([email protected](key, addValue))
         {
-            that[item.Key] = item.Value;
+            @this[key] = updateValueFactory(key, @this[key]);
         }
-    }
 
+        return @this[key];
+    }
+    
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
+    /// <typeparam name="TKey"></typeparam>
+    /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="key">键</param>
     /// <param name="addValue">添加时的值</param>
-    /// <param name="updateValueFactory">更新时的操作</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
+    /// <param name="updateValue">更新时的值</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
     {
         if ([email protected](key, addValue))
         {
-            @this[key] = updateValueFactory(key, @this[key]);
+            @this[key] = updateValue;
         }
 
         return @this[key];
@@ -110,12 +129,12 @@ public static class IDictionaryExtensions
     /// <param name="this"></param>
     /// <param name="key">键</param>
     /// <param name="addValue">添加时的值</param>
-    /// <param name="updateValueFactory">更新时的操作</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
+    /// <param name="updateValue">更新时的值</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
     {
         if ([email protected](key, addValue))
         {
-            @this[key] = updateValueFactory(key, @this[key]);
+            @this[key] = updateValue;
         }
 
         return @this[key];
@@ -129,12 +148,12 @@ public static class IDictionaryExtensions
     /// <param name="this"></param>
     /// <param name="key">键</param>
     /// <param name="addValue">添加时的值</param>
-    /// <param name="updateValueFactory">更新时的操作</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
+    /// <param name="updateValue">更新时的值</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
     {
         if ([email protected](key, addValue))
         {
-            @this[key] = updateValueFactory(key, @this[key]);
+            @this[key] = updateValue;
         }
 
         return @this[key];
@@ -146,17 +165,14 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValue">添加时的值</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValue))
+        foreach (var item in that)
         {
-            @this[key] = await updateValueFactory(key, @this[key]);
+            AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
         }
-
-        return @this[key];
     }
 
     /// <summary>
@@ -165,17 +181,14 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValue">添加时的值</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValue))
+        foreach (var item in that)
         {
-            @this[key] = await updateValueFactory(key, @this[key]);
+            AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
         }
-
-        return @this[key];
     }
 
     /// <summary>
@@ -184,17 +197,14 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValue">添加时的值</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValue))
+        foreach (var item in that)
         {
-            @this[key] = await updateValueFactory(key, @this[key]);
+            AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
         }
-
-        return @this[key];
     }
 
     /// <summary>
@@ -204,13 +214,13 @@ public static class IDictionaryExtensions
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="key">键</param>
-    /// <param name="addValue">添加时的值</param>
-    /// <param name="updateValue">更新时的值</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
+    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="updateValueFactory">更新时的操作</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValue))
+        if ([email protected](key, addValueFactory(key)))
         {
-            @this[key] = updateValue;
+            @this[key] = updateValueFactory(key, @this[key]);
         }
 
         return @this[key];
@@ -219,15 +229,17 @@ public static class IDictionaryExtensions
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
+    /// <typeparam name="TKey"></typeparam>
+    /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="key">键</param>
-    /// <param name="addValue">添加时的值</param>
-    /// <param name="updateValue">更新时的值</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
+    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="updateValueFactory">更新时的操作</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValue))
+        if ([email protected](key, addValueFactory(key)))
         {
-            @this[key] = updateValue;
+            @this[key] = updateValueFactory(key, @this[key]);
         }
 
         return @this[key];
@@ -240,13 +252,13 @@ public static class IDictionaryExtensions
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="key">键</param>
-    /// <param name="addValue">添加时的值</param>
-    /// <param name="updateValue">更新时的值</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
+    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="updateValueFactory">更新时的操作</param>
+    public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValue))
+        if ([email protected](key, addValueFactory(key)))
         {
-            @this[key] = updateValue;
+            @this[key] = updateValueFactory(key, @this[key]);
         }
 
         return @this[key];
@@ -258,14 +270,17 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
+    /// <param name="key">键</param>
+    /// <param name="addValue">添加时的值</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
+    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        foreach (var item in that)
+        if ([email protected](key, addValue))
         {
-            AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
+            @this[key] = await updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
@@ -274,14 +289,17 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
+    /// <param name="key">键</param>
+    /// <param name="addValue">添加时的值</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
+    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        foreach (var item in that)
+        if ([email protected](key, addValue))
         {
-            AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
+            @this[key] = await updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
@@ -290,14 +308,17 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
+    /// <param name="key">键</param>
+    /// <param name="addValue">添加时的值</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
+    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        foreach (var item in that)
+        if ([email protected](key, addValue))
         {
-            AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
+            @this[key] = await updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
@@ -345,14 +366,17 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
+    /// <param name="key">键</param>
+    /// <param name="addValueFactory">添加时的操作</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
+    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        foreach (var item in @this)
+        if ([email protected](key, await addValueFactory(key)))
         {
-            AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
+            @this[key] = await updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
@@ -361,14 +385,17 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
+    /// <param name="key">键</param>
+    /// <param name="addValueFactory">添加时的操作</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
+    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        foreach (var item in @this)
+        if ([email protected](key, await addValueFactory(key)))
         {
-            AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
+            @this[key] = await updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
@@ -377,53 +404,56 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="that">另一个字典集</param>
+    /// <param name="key">键</param>
+    /// <param name="addValueFactory">添加时的操作</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
+    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        foreach (var item in @this)
+        if ([email protected](key, await addValueFactory(key)))
         {
-            AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
+            @this[key] = await updateValueFactory(key, @this[key]);
         }
+
+        return @this[key];
     }
 
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
-    /// <typeparam name="TKey"></typeparam>
-    /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="that">另一个字典集</param>
-    /// <param name="updateValueFactory">更新时的操作</param>
-    public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
     {
-        return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
+        foreach (var item in @this)
+        {
+            that[item.Key] = item.Value;
+        }
     }
 
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
-    /// <typeparam name="TKey"></typeparam>
-    /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="that">另一个字典集</param>
-    /// <param name="updateValueFactory">更新时的操作</param>
-    public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
     {
-        return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
+        foreach (var item in @this)
+        {
+            that[item.Key] = item.Value;
+        }
     }
 
     /// <summary>
     /// 添加或更新键值对
     /// </summary>
-    /// <typeparam name="TKey"></typeparam>
-    /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="that">另一个字典集</param>
-    /// <param name="updateValueFactory">更新时的操作</param>
-    public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
     {
-        return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
+        foreach (var item in @this)
+        {
+            that[item.Key] = item.Value;
+        }
     }
 
     /// <summary>
@@ -432,17 +462,14 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
+    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValueFactory(key)))
+        foreach (var item in @this)
         {
-            @this[key] = updateValueFactory(key, @this[key]);
+            AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
         }
-
-        return @this[key];
     }
 
     /// <summary>
@@ -451,17 +478,14 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
+    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValueFactory(key)))
+        foreach (var item in @this)
         {
-            @this[key] = updateValueFactory(key, @this[key]);
+            AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
         }
-
-        return @this[key];
     }
 
     /// <summary>
@@ -470,17 +494,14 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
+    public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
     {
-        if ([email protected](key, addValueFactory(key)))
+        foreach (var item in @this)
         {
-            @this[key] = updateValueFactory(key, @this[key]);
+            AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
         }
-
-        return @this[key];
     }
 
     /// <summary>
@@ -489,17 +510,11 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static Task AddOrUpdateToAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        if ([email protected](key, await addValueFactory(key)))
-        {
-            @this[key] = await updateValueFactory(key, @this[key]);
-        }
-
-        return @this[key];
+        return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
     }
 
     /// <summary>
@@ -508,17 +523,11 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static Task AddOrUpdateToAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        if ([email protected](key, await addValueFactory(key)))
-        {
-            @this[key] = await updateValueFactory(key, @this[key]);
-        }
-
-        return @this[key];
+        return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
     }
 
     /// <summary>
@@ -527,17 +536,11 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
-    /// <param name="key">键</param>
-    /// <param name="addValueFactory">添加时的操作</param>
+    /// <param name="that">另一个字典集</param>
     /// <param name="updateValueFactory">更新时的操作</param>
-    public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
+    public static Task AddOrUpdateToAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
     {
-        if ([email protected](key, await addValueFactory(key)))
-        {
-            @this[key] = await updateValueFactory(key, @this[key]);
-        }
-
-        return @this[key];
+        return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
     }
 
     /// <summary>
@@ -565,15 +568,10 @@ public static class IDictionaryExtensions
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="key"></param>
-    /// <param name="addValueFactory"></param>
-    public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
+    /// <param name="addValue"></param>
+    public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
     {
-        if ([email protected](key))
-        {
-            @this[key] = await addValueFactory();
-        }
-
-        return @this[key];
+        return @this.TryAdd(key, addValue) ? addValue : @this[key];
     }
 
     /// <summary>
@@ -583,15 +581,18 @@ public static class IDictionaryExtensions
     /// <typeparam name="TValue"></typeparam>
     /// <param name="this"></param>
     /// <param name="key"></param>
-    /// <param name="addValue"></param>
-    public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
+    /// <param name="addValueFactory"></param>
+    public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
     {
-        return @this.TryAdd(key, addValue) ? addValue : @this[key];
-    }
+        if ([email protected](key))
+        {
+            @this[key] = await addValueFactory();
+        }
 
-#if !NETSTANDARD2_1_OR_GREATER
+        return @this[key];
+    }
 
-    public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
+    private static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
     {
         if (dictionary == null)
             throw new ArgumentNullException(nameof(dictionary));
@@ -601,8 +602,6 @@ public static class IDictionaryExtensions
         return true;
     }
 
-#endif
-
     /// <summary>
     /// 遍历IEnumerable
     /// </summary>

+ 1 - 1
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -3,7 +3,7 @@
         <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6;net7;net8</TargetFrameworks>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>2.6.8.9</Version>
+        <Version>2.6.9</Version>
         <Authors>懒得勤快</Authors>
         <Description>全龄段友好的C#万能工具库,码数吐司库,不管你是菜鸟新手还是骨灰级玩家都能轻松上手,Masuit.Tools基础公共库(适用于.NET4.6.1/.NET Standard2.0及以上项目),包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
             官网教程:https://tools.masuit.org

+ 100 - 36
Masuit.Tools.Abstractions/Systems/MaskConverter2.cs

@@ -7,46 +7,110 @@ namespace Masuit.Tools.Systems.Text.Json;
 
 public class MaskConverter : JsonConverter<string>
 {
-	/// <summary>Reads and converts the JSON to type <typeparamref name="T" />.</summary>
-	/// <param name="reader">The reader.</param>
-	/// <param name="typeToConvert">The type to convert.</param>
-	/// <param name="options">An object that specifies serialization options to use.</param>
-	/// <returns>The converted value.</returns>
-	public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
-	{
-		return reader.GetString();
-	}
-
-	/// <summary>Writes a specified value as JSON.</summary>
-	/// <param name="writer">The writer to write to.</param>
-	/// <param name="value">The value to convert to JSON.</param>
-	/// <param name="options">An object that specifies serialization options to use.</param>
-	public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
-	{
-		writer.WriteStringValue(string.IsNullOrEmpty(value) ? value : value.Mask());
-	}
+    /// <summary>Reads and converts the JSON to type <typeparamref name="T" />.</summary>
+    /// <param name="reader">The reader.</param>
+    /// <param name="typeToConvert">The type to convert.</param>
+    /// <param name="options">An object that specifies serialization options to use.</param>
+    /// <returns>The converted value.</returns>
+    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+    {
+        return reader.GetString();
+    }
+
+    /// <summary>Writes a specified value as JSON.</summary>
+    /// <param name="writer">The writer to write to.</param>
+    /// <param name="value">The value to convert to JSON.</param>
+    /// <param name="options">An object that specifies serialization options to use.</param>
+    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
+    {
+        writer.WriteStringValue(string.IsNullOrEmpty(value) ? value : value.Mask());
+    }
 }
 
 public class MaskEmailConverter : JsonConverter<string>
 {
-	/// <summary>Reads and converts the JSON to type <typeparamref name="T" />.</summary>
-	/// <param name="reader">The reader.</param>
-	/// <param name="typeToConvert">The type to convert.</param>
-	/// <param name="options">An object that specifies serialization options to use.</param>
-	/// <returns>The converted value.</returns>
-	public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
-	{
-		return reader.GetString();
-	}
-
-	/// <summary>Writes a specified value as JSON.</summary>
-	/// <param name="writer">The writer to write to.</param>
-	/// <param name="value">The value to convert to JSON.</param>
-	/// <param name="options">An object that specifies serialization options to use.</param>
-	public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
-	{
-		writer.WriteStringValue(string.IsNullOrEmpty(value) ? value : value.MaskEmail());
-	}
+    /// <summary>Reads and converts the JSON to type <typeparamref name="T" />.</summary>
+    /// <param name="reader">The reader.</param>
+    /// <param name="typeToConvert">The type to convert.</param>
+    /// <param name="options">An object that specifies serialization options to use.</param>
+    /// <returns>The converted value.</returns>
+    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+    {
+        return reader.GetString();
+    }
+
+    /// <summary>Writes a specified value as JSON.</summary>
+    /// <param name="writer">The writer to write to.</param>
+    /// <param name="value">The value to convert to JSON.</param>
+    /// <param name="options">An object that specifies serialization options to use.</param>
+    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
+    {
+        writer.WriteStringValue(string.IsNullOrEmpty(value) ? value : value.MaskEmail());
+    }
+}
+
+public class ObjectJsonConverter : JsonConverter<object>
+{
+    public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+    {
+        if (reader.TokenType == JsonTokenType.String)
+        {
+            if (DateTime.TryParse(reader.GetString(), out DateTime dateTime))
+                return dateTime;
+            return reader.GetString();
+        }
+
+        if (reader.TokenType == JsonTokenType.Number)
+        {
+            if (reader.TryGetInt32(out int intNum))
+                return intNum;
+            if (reader.TryGetDouble(out double doubleNum))
+                return doubleNum;
+            return reader.GetDecimal();
+        }
+        return reader.GetDecimal();
+    }
+
+    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
+    {
+        Type objType = value.GetType();
+        if (value == null)
+            writer.WriteNullValue();
+        if (objType == typeof(string) || objType == typeof(DateTime) || objType == typeof(Guid))
+            writer.WriteStringValue(value.ToString());
+        else if (objType == typeof(int))
+            writer.WriteNumberValue((int)value);
+        else if (objType == typeof(double))
+            writer.WriteNumberValue((double)value);
+        else if (objType == typeof(decimal))
+            writer.WriteNumberValue((decimal)value);
+        else if (objType == typeof(char))
+            writer.WriteNumberValue((char)value);
+        else if (objType == typeof(bool))
+            writer.WriteBooleanValue((bool)value);
+        else
+            writer.WriteStringValue(value.ToString());
+    }
+}
+
+public class DateTimeJsonConverter : JsonConverter<DateTime>
+{
+    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+    {
+        if (reader.TokenType == JsonTokenType.String)
+        {
+            if (DateTime.TryParse(reader.GetString(), out DateTime dateTime))
+            {
+                return dateTime;
+            }
+        }
+        return reader.GetDateTime();
+    }
+
+    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
+    {
+        writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
+    }
 }
 
 #endif

+ 1 - 1
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -18,7 +18,7 @@
         <Product>Masuit.Tools.AspNetCore</Product>
         <PackageId>Masuit.Tools.AspNetCore</PackageId>
         <LangVersion>latest</LangVersion>
-        <Version>1.2.8.9</Version>
+        <Version>1.2.9</Version>
         <RepositoryType></RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <FileVersion>1.1.9</FileVersion>

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -6,7 +6,7 @@
 官网教程:https://tools.masuit.org
 github:https://github.com/ldqk/Masuit.Tools
         </Description>
-        <Version>2.6.8.9</Version>
+        <Version>2.6.9</Version>
         <Copyright>Copyright © 懒得勤快</Copyright>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
         <PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>

+ 2 - 2
Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

@@ -3,7 +3,7 @@
         <TargetFramework>netstandard2.0</TargetFramework>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>1.2.8.9</Version>
+        <Version>1.2.9</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
         <Copyright>懒得勤快</Copyright>
@@ -38,7 +38,7 @@
       </None>
     </ItemGroup>
     <ItemGroup>
-        <PackageReference Include="EPPlus" Version="7.0.7" />
+        <PackageReference Include="EPPlus" Version="7.0.8" />
         <PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
     </ItemGroup>
     <ItemGroup>

+ 1 - 1
Masuit.Tools.Net45/package.nuspec

@@ -2,7 +2,7 @@
 <package>
   <metadata>
     <id>Masuit.Tools.Net45</id>
-    <version>2.6.8.9</version>
+    <version>2.6.9</version>
     <title>Masuit.Tools</title>
     <authors>懒得勤快</authors>
     <owners>masuit.com</owners>

+ 1 - 0
Masuit.Tools/Masuit.Tools.csproj

@@ -173,6 +173,7 @@
     <Compile Include="NoSQL\RedisHelper.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Strings\ValidateCode.cs" />
+    <Compile Include="Systems\IDictionaryExtensions.cs" />
     <Compile Include="Systems\Lock.cs" />
     <Compile Include="Systems\RedisLock.cs" />
   </ItemGroup>

+ 17 - 0
Masuit.Tools/Systems/IDictionaryExtensions.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+
+namespace Masuit.Tools.Systems;
+
+public static class IDictionaryExtensions
+{
+    public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
+    {
+        if (dictionary == null)
+            throw new ArgumentNullException(nameof(dictionary));
+        if (dictionary.IsReadOnly || dictionary.ContainsKey(key))
+            return false;
+        dictionary.Add(key, value);
+        return true;
+    }
+}

+ 4 - 4
Masuit.Tools/Systems/Lock.cs

@@ -22,18 +22,18 @@ namespace Masuit.Tools.Systems
         }
 
         /// <summary>
-        /// 
+        ///
         /// </summary>
         public RedisKey Resource { get; }
 
         /// <summary>
-        /// 
+        ///
         /// </summary>
         public RedisValue Value { get; }
 
         /// <summary>
-        /// 
+        ///
         /// </summary>
         public TimeSpan Validity { get; }
     }
-}
+}

+ 1 - 1
Masuit.Tools/package.nuspec

@@ -2,7 +2,7 @@
 <package>
   <metadata>
     <id>Masuit.Tools.Net</id>
-    <version>2.6.8.9</version>
+    <version>2.6.9</version>
     <title>Masuit.Tools</title>
     <authors>懒得勤快</authors>
     <owners>masuit.com</owners>

+ 2 - 2
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -98,10 +98,10 @@
       <Version>4.20.70</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestAdapter">
-      <Version>3.1.1</Version>
+      <Version>3.2.0</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestFramework">
-      <Version>3.1.1</Version>
+      <Version>3.2.0</Version>
     </PackageReference>
     <PackageReference Include="NUnit">
       <Version>4.0.1</Version>