From b1b18d4bb30e53093f18905dffba91fc966c11fd 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: Thu, 14 May 2026 17:04:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=A0=E4=B8=AA=E6=96=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Extensions/EnumerableExtensions.cs | 48 +++++++++++++++++------ Shared/Utils/EnumUtils.cs | 34 ++++++++++++++++ 2 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 Shared/Utils/EnumUtils.cs diff --git a/Shared/Extensions/EnumerableExtensions.cs b/Shared/Extensions/EnumerableExtensions.cs index 807e4f4..3c708c5 100644 --- a/Shared/Extensions/EnumerableExtensions.cs +++ b/Shared/Extensions/EnumerableExtensions.cs @@ -1,10 +1,9 @@ using System.Collections.Concurrent; -using System.Globalization; namespace MeloongCore.Extensions; public static class EnumerableExtensions { - #region Distinct + #region 去重 /// /// 通过比较器进行去重。 @@ -38,7 +37,7 @@ public static class EnumerableExtensions { #endregion - #region MinBy / MaxBy + #region 最大 / 最小 /// /// 选择所有最大值对应的对象。 @@ -128,6 +127,22 @@ public static class EnumerableExtensions { #endregion + #region 排序 + + /// + /// 使用默认比较器对集合进行排序。顺序为从小到大。 + /// + public static IEnumerable Ordered(this IEnumerable source) where T : IComparable + => source.OrderBy(x => x); + + /// + /// 使用默认比较器对集合进行排序。顺序为从大到小。 + /// + public static IEnumerable OrderedDescending(this IEnumerable source) where T : IComparable + => source.OrderByDescending(x => x); + + #endregion + #region Join /// @@ -187,7 +202,7 @@ public static class EnumerableExtensions { /// /// 尝试从字典中获取某项,如果该项不存在,则返回默认值。 /// - public static TValue? GetOrDefault(this IDictionary dict, TKey key, TValue defaultValue = default) + public static TValue? GetOrDefault(this IDictionary dict, TKey key, TValue? defaultValue = default) => dict.TryGetValue(key, out var result) ? result : defaultValue; /// @@ -210,19 +225,30 @@ public static class EnumerableExtensions { /// /// 会跳过:0、非 2 的幂的值、不在枚举值中的数、组合值、对应同一个枚举项的多个别名。 /// - public static IEnumerable Flags(this T value) where T : struct, Enum { - foreach (var element in Enum.GetValues(typeof(T)).Cast().Distinct()) { - var number = Convert.ToInt64(element, CultureInfo.InvariantCulture); - if (number == 0 || (number & (number - 1)) != 0) continue; // 跳过 0 和非 2 的幂的值 - if (value.HasFlag(element)) yield return element; - } + public static IEnumerable Flags(this T value) where T : struct, Enum + => EnumUtils.GetAllFlags().Where(element => value.HasFlag(element)); + + /// + /// 从集合中移除满足条件的元素。 + /// + public static void RemoveIf(this ICollection source, Func predicate) { + var itemsToRemove = source.Where(predicate).ToList(); + foreach (var item in itemsToRemove) source.Remove(item); + } + + /// + /// 仅保留满足条件的元素,移除不满足条件的元素。 + /// + public static void KeepIf(this ICollection source, Func predicate) { + var itemsToRemove = source.Where(item => !predicate(item)).ToList(); + foreach (var item in itemsToRemove) source.Remove(item); } /// /// 判断集合是否有且仅有一个元素。 /// 在输入 null 时返回 false。 /// - public static bool IsSingle([AllowNull] this IEnumerable source) { + public static bool IsSingle(this IEnumerable? source) { if (source is null) return false; if (source is IList list) return list.Count == 1; using var enumerator = source.GetEnumerator(); diff --git a/Shared/Utils/EnumUtils.cs b/Shared/Utils/EnumUtils.cs new file mode 100644 index 0000000..6c3ef0f --- /dev/null +++ b/Shared/Utils/EnumUtils.cs @@ -0,0 +1,34 @@ +using System.Collections.Concurrent; +using System.Globalization; + +namespace MeloongCore; +public static class EnumUtils { + + /// + /// 获取枚举的所有值。 + /// + private static IEnumerable GetAllValues() where T : struct, Enum + => Enum.GetValues(typeof(T)).Cast(); + + /// + /// 对于具有 的枚举,返回其所有 Flag 值。 + /// 建议仅对没有极端值的正数枚举使用。 + /// + /// + /// 会跳过:0、非 2 的幂的值、不在枚举值中的数、组合值、对应同一个枚举项的多个别名。 + /// + public static List GetAllFlags() where T : struct, Enum { + if (allFlagsCache.TryGetValue(typeof(T), out var cached)) return cached.Cast().ToList(); + if (!typeof(T).IsDefined(typeof(FlagsAttribute), inherit: false)) throw new ArgumentException($"枚举 {typeof(T).FullName} 没有定义 FlagsAttribute。"); + var flags = new List(); + foreach (var element in EnumUtils.GetAllValues().Distinct()) { + var number = Convert.ToInt64(element, CultureInfo.InvariantCulture); + if (number == 0 || (number & (number - 1)) != 0) continue; // 跳过 0 和非 2 的幂的值 + flags.Add(element); + } + allFlagsCache[typeof(T)] = flags.Cast().ToList(); + return flags; + } + private static readonly ConcurrentDictionary> allFlagsCache = new(); + +}