ref: 迁移 GetOrDefault、AddIntoValueCollection、Flags

This commit is contained in:
龙腾猫跃 committed 2026-04-27 23:33:50 +08:00
1 parent 608ab6fd7f
commit b904ca23af
1 file changed
+32
+32
View File
@@ -176,8 +176,40 @@ public static class EnumerableExtensions {
return result; return result;
} }
/// <summary>
/// 尝试从字典中获取某项,如果该项不存在,则返回默认值。
/// </summary>
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default!) =>
dict.TryGetValue(key, out var result) ? result : defaultValue;
/// <summary>
/// 将值添加到字典的对应项的列表中。
/// </summary>
public static void AddIntoValueCollection<TKey, TValue>(this IDictionary<TKey, List<TValue>> dict, TKey key, TValue value) {
if (dict.TryGetValue(key, out var collection)) {
collection.Add(value);
} else {
dict.Add(key, [value]);
}
}
#endregion #endregion
/// <summary>
/// 将 <see cref="FlagsAttribute"/> 枚举值解包为它包含的单一 Flag 成员的集合。
/// 建议仅对没有极端值的正数枚举使用。
/// </summary>
/// <remarks>
/// 会跳过:0、非 2 的幂的值、不在枚举值中的数、组合值、对应同一个枚举项的多个别名。
/// </remarks>
public static IEnumerable<T> Flags<T>(this T value) where T : struct, Enum {
foreach (var element in Enum.GetValues(typeof(T)).Cast<T>().Distinct()) {
var number = Convert.ToInt64(element);
if (number == 0 || (number & (number - 1)) != 0) continue; // 跳过 0 和非 2 的幂的值
if (value.HasFlag(element)) yield return element;
}
}
/// <summary> /// <summary>
/// 判断集合是否有且仅有一个元素。 /// 判断集合是否有且仅有一个元素。
/// 在输入 null 时返回 false。 /// 在输入 null 时返回 false。