From 608ab6fd7fee88362bd8374207ce2efc0dd623aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com>
Date: Mon, 27 Apr 2026 22:00:22 +0800
Subject: [PATCH] =?UTF-8?q?ref:=20=E8=BF=81=E7=A7=BB=20ToDictionary?=
=?UTF-8?q?=E3=80=81ConcurrentList?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Extensions/EnumerableExtensions.cs | 25 ++++++++++++++++++++++---
General/ConcurrentList.cs | 22 ++++++++++++++++++++++
MeloongCore.csproj | 2 ++
3 files changed, 46 insertions(+), 3 deletions(-)
create mode 100644 General/ConcurrentList.cs
diff --git a/Extensions/EnumerableExtensions.cs b/Extensions/EnumerableExtensions.cs
index 14f57e5..c2ff952 100644
--- a/Extensions/EnumerableExtensions.cs
+++ b/Extensions/EnumerableExtensions.cs
@@ -1,6 +1,4 @@
-using System.Collections.Concurrent;
-
-namespace MeloongCore.Extensions;
+namespace MeloongCore.Extensions;
public static class EnumerableExtensions {
#region Distinct
@@ -159,6 +157,27 @@ public static class EnumerableExtensions {
#endregion
+ #region Dictionary
+
+ ///
+ /// 从 中移除具有指定键的元素。
+ /// 返回是否确实移除了元素。
+ ///
+ public static bool Remove(this ConcurrentDictionary dict, TKey key) => dict.TryRemove(key, out _);
+
+ ///
+ /// 将二维数组转换为字典。
+ ///
+ public static Dictionary ToDictionary(this T[,] arr) where T : notnull {
+ Dictionary result = [];
+ if (arr.Length == 0) return result;
+ if (arr.GetLength(1) != 2) throw new ArgumentException("数组必须为两列,第一列为 Key,第二列为 Value。");
+ for (int i = 0; i < arr.GetLength(0); i++) result[arr[i, 0]] = arr[i, 1];
+ return result;
+ }
+
+ #endregion
+
///
/// 判断集合是否有且仅有一个元素。
/// 在输入 null 时返回 false。
diff --git a/General/ConcurrentList.cs b/General/ConcurrentList.cs
new file mode 100644
index 0000000..dc6cda8
--- /dev/null
+++ b/General/ConcurrentList.cs
@@ -0,0 +1,22 @@
+namespace MeloongCore;
+public class ConcurrentList : SynchronizedCollection, IEnumerable, IEnumerable {
+
+ // 构造函数
+ public ConcurrentList() : base() { }
+ public ConcurrentList(IEnumerable data) : base(new object(), data) { }
+ public static implicit operator ConcurrentList(List data) => new(data);
+ public static implicit operator List(ConcurrentList data) => new(data);
+
+ // 对枚举器进行覆写,返回一个临时副本
+ public new IEnumerator GetEnumerator() {
+ lock (SyncRoot) {
+ return Items.ToList().GetEnumerator();
+ }
+ }
+ IEnumerator IEnumerable.GetEnumerator() {
+ lock (SyncRoot) {
+ return Items.ToList().GetEnumerator();
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/MeloongCore.csproj b/MeloongCore.csproj
index f5cfb46..4d6c546 100644
--- a/MeloongCore.csproj
+++ b/MeloongCore.csproj
@@ -24,6 +24,7 @@
+
@@ -37,6 +38,7 @@
+