소스 검색

NullableDictionary增加支持委托的索引器

懒得勤快 1 년 전
부모
커밋
447972c28d

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

+ 93 - 7
Masuit.Tools.Abstractions/Systems/NullableConcurrentDictionary.cs

@@ -1,5 +1,7 @@
-using System.Collections.Concurrent;
+using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Linq;
 
 namespace Masuit.Tools.Systems;
 
@@ -35,11 +37,95 @@ public class NullableConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<N
         set => base[key] = value;
     }
 
-    //public virtual TValue this[TKey key]
-    //{
-    //    get => TryGetValue(key, out var value) ? value : FallbackValue;
-    //    set => base[key] = value;
-    //}
+    public TValue this[Func<KeyValuePair<TKey, TValue>, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    public virtual TValue this[Func<TKey, TValue, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    public virtual TValue this[Func<TKey, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    public virtual TValue this[Func<TValue, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Value)))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Value)))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    /// <summary>
+    ///
+    /// </summary>
+    /// <param name="key"></param>
+    public virtual TValue this[TKey key]
+    {
+        get => TryGetValue(new NullObject<TKey>(key), out var value) ? value : FallbackValue;
+        set => base[new NullObject<TKey>(key)] = value;
+    }
 
     /// <summary>
     /// 隐式转换
@@ -82,4 +168,4 @@ public class NullableConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<N
         }
         return newdic;
     }
-}
+}

+ 93 - 11
Masuit.Tools.Abstractions/Systems/NullableDictionary.cs

@@ -1,5 +1,7 @@
-using System.Collections.Concurrent;
+using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Linq;
 
 namespace Masuit.Tools.Systems;
 
@@ -51,15 +53,95 @@ public class NullableDictionary<TKey, TValue> : Dictionary<NullObject<TKey>, TVa
         set => base[key] = value;
     }
 
-    ///// <summary>
-    /////
-    ///// </summary>
-    ///// <param name="key"></param>
-    //public virtual TValue this[TKey key]
-    //{
-    //    get => TryGetValue(key, out var value) ? value : FallbackValue;
-    //    set => base[key] = value;
-    //}
+    public virtual TValue this[Func<KeyValuePair<TKey, TValue>, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(new KeyValuePair<TKey, TValue>(pair.Key.Item, pair.Value))))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    public virtual TValue this[Func<TKey, TValue, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item, pair.Value)))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    public virtual TValue this[Func<TKey, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Key.Item)))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    public virtual TValue this[Func<TValue, bool> condition]
+    {
+        get
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Value)))
+            {
+                return pair.Value;
+            }
+
+            return FallbackValue;
+        }
+        set
+        {
+            foreach (var pair in this.Where(pair => condition(pair.Value)))
+            {
+                this[pair.Key] = value;
+            }
+        }
+    }
+
+    /// <summary>
+    ///
+    /// </summary>
+    /// <param name="key"></param>
+    public virtual TValue this[TKey key]
+    {
+        get => TryGetValue(new NullObject<TKey>(key), out var value) ? value : FallbackValue;
+        set => base[new NullObject<TKey>(key)] = value;
+    }
 
     /// <summary>
     /// 隐式转换
@@ -102,4 +184,4 @@ public class NullableDictionary<TKey, TValue> : Dictionary<NullObject<TKey>, TVa
         }
         return newdic;
     }
-}
+}

+ 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.9.6</Version>
+        <Version>1.2.9.7</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.9.6</Version>
+        <Version>2.6.9.7</Version>
         <Copyright>Copyright © 懒得勤快</Copyright>
         <PackageProjectUrl>https://github.com/ldqk/Masuit.Tools</PackageProjectUrl>
         <PackageTags>Masuit.Tools,工具库,Utility,Crypt,Extensions</PackageTags>

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

@@ -3,7 +3,7 @@
         <TargetFramework>netstandard2.0</TargetFramework>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>1.2.9.6</Version>
+        <Version>1.2.9.7</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
         <Copyright>懒得勤快</Copyright>

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

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

+ 1 - 1
Masuit.Tools/package.nuspec

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