diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..bc23fdc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ + +[*.{cs,vb}] + +# CA1710: 标识符应具有正确的后缀 +dotnet_diagnostic.CA1710.severity = none diff --git a/Extensions/EnumerableExtensions.cs b/Shared/Extensions/EnumerableExtensions.cs similarity index 83% rename from Extensions/EnumerableExtensions.cs rename to Shared/Extensions/EnumerableExtensions.cs index edfef2c..fe1f51d 100644 --- a/Extensions/EnumerableExtensions.cs +++ b/Shared/Extensions/EnumerableExtensions.cs @@ -1,4 +1,6 @@ -namespace MeloongCore.Extensions; +using System.Globalization; + +namespace MeloongCore.Extensions; public static class EnumerableExtensions { #region Distinct @@ -37,16 +39,16 @@ public static class EnumerableExtensions { /// 选择所有最大值对应的对象。 /// 若没有元素则返回空列表。 /// - public static List MaxByAll(this IEnumerable source, Func selector) where C : IComparable { - var results = new List(); + public static List MaxByAll(this IEnumerable source, Func selector) where TValue : IComparable { + var results = new List(); using var enumerator = source.GetEnumerator(); if (!enumerator.MoveNext()) return results; - T maxItem = enumerator.Current; - C maxValue = selector(maxItem); + TKey maxItem = enumerator.Current; + TValue maxValue = selector(maxItem); results.Add(maxItem); while (enumerator.MoveNext()) { - T currentItem = enumerator.Current; - C currentValue = selector(currentItem); + TKey currentItem = enumerator.Current; + TValue currentValue = selector(currentItem); int comparisonResult = currentValue.CompareTo(maxValue); if (comparisonResult > 0) { maxValue = currentValue; @@ -62,16 +64,16 @@ public static class EnumerableExtensions { /// 选择所有最小值对应的对象。 /// 若没有元素则返回空列表。 /// - public static List MinByAll(this IEnumerable source, Func selector) where C : IComparable { - var results = new List(); + public static List MinByAll(this IEnumerable source, Func selector) where TValue : IComparable { + var results = new List(); using var enumerator = source.GetEnumerator(); if (!enumerator.MoveNext()) return results; - T minItem = enumerator.Current; - C minValue = selector(minItem); + TKey minItem = enumerator.Current; + TValue minValue = selector(minItem); results.Add(minItem); while (enumerator.MoveNext()) { - T currentItem = enumerator.Current; - C currentValue = selector(currentItem); + TKey currentItem = enumerator.Current; + TValue currentValue = selector(currentItem); int comparisonResult = currentValue.CompareTo(minValue); if (comparisonResult < 0) { minValue = currentValue; @@ -88,13 +90,13 @@ public static class EnumerableExtensions { /// 选择最大值对应的对象。 /// 若没有元素则返回 null。 /// - public static T? MaxBy(this IEnumerable source, Func selector) where C : IComparable { + public static TKey? MaxBy(this IEnumerable source, Func selector) where TValue : IComparable { using var enumerator = source.GetEnumerator(); if (!enumerator.MoveNext()) return default; - T maxItem = enumerator.Current; - C maxValue = selector(maxItem); + TKey maxItem = enumerator.Current; + TValue maxValue = selector(maxItem); while (enumerator.MoveNext()) { - C value = selector(enumerator.Current); + TValue value = selector(enumerator.Current); if (value.CompareTo(maxValue) <= 0) continue; maxItem = enumerator.Current; maxValue = value; @@ -105,13 +107,13 @@ public static class EnumerableExtensions { /// 选择最小值对应的对象。 /// 若没有元素则返回 null。 /// - public static T? MinBy(this IEnumerable List, Func Selector) where C : IComparable { + public static TKey? MinBy(this IEnumerable List, Func Selector) where TValue : IComparable { using var enumerator = List.GetEnumerator(); if (!enumerator.MoveNext()) { return default; } - T minItem = enumerator.Current; - C minValue = Selector(minItem); + TKey minItem = enumerator.Current; + TValue minValue = Selector(minItem); while (enumerator.MoveNext()) { - C value = Selector(enumerator.Current); + TValue value = Selector(enumerator.Current); if (value.CompareTo(minValue) >= 0) { continue; } minItem = enumerator.Current; minValue = value; @@ -204,7 +206,7 @@ public static class EnumerableExtensions { /// 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); + var number = Convert.ToInt64(element, CultureInfo.InvariantCulture); if (number == 0 || (number & (number - 1)) != 0) continue; // 跳过 0 和非 2 的幂的值 if (value.HasFlag(element)) yield return element; } diff --git a/Extensions/StringExtensions.cs b/Shared/Extensions/StringExtensions.cs similarity index 100% rename from Extensions/StringExtensions.cs rename to Shared/Extensions/StringExtensions.cs diff --git a/General/ConcurrentList.cs b/Shared/General/ConcurrentList.cs similarity index 100% rename from General/ConcurrentList.cs rename to Shared/General/ConcurrentList.cs diff --git a/General/MsDelta.cs b/Shared/General/MsDelta.cs similarity index 100% rename from General/MsDelta.cs rename to Shared/General/MsDelta.cs diff --git a/General/OneOf.cs b/Shared/General/OneOf.cs similarity index 100% rename from General/OneOf.cs rename to Shared/General/OneOf.cs diff --git a/MeloongCore.csproj b/Shared/MeloongCore.csproj similarity index 94% rename from MeloongCore.csproj rename to Shared/MeloongCore.csproj index 4d6c546..54fec6c 100644 --- a/MeloongCore.csproj +++ b/Shared/MeloongCore.csproj @@ -11,7 +11,8 @@ true 13.0 enable - 8.0 + latest-recommended + True @@ -23,7 +24,6 @@ - diff --git a/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs similarity index 100% rename from Utils/DirectoryUtils.cs rename to Shared/Utils/DirectoryUtils.cs diff --git a/Utils/EscapeUtils.cs b/Shared/Utils/EscapeUtils.cs similarity index 100% rename from Utils/EscapeUtils.cs rename to Shared/Utils/EscapeUtils.cs diff --git a/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs similarity index 91% rename from Utils/FileUtils.cs rename to Shared/Utils/FileUtils.cs index 8091a52..1a291b1 100644 --- a/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -64,12 +64,14 @@ public static class FileUtils { if (pathToShorten.Length <= 10) return fullName; // 缩短路径 - var buffer = new StringBuilder(260); - if (GetShortPathNameW(pathToShorten, buffer, buffer.Capacity) == 0) return fullName; - return Path.Combine(buffer.ToString(), pathToKeep); + char[] buffer = new char[260]; + int result = GetShortPathNameW(pathToShorten, buffer, buffer.Length); + if (result == 0) return fullName; + string shortPath = new(buffer, 0, result); + return Path.Combine(shortPath, pathToKeep); } [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] - private static extern int GetShortPathNameW(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer); + private static extern int GetShortPathNameW(string lpszLongPath, [Out] char[] buffer, int cchBuffer); #endregion diff --git a/Utils/MathUtils.cs b/Shared/Utils/MathUtils.cs similarity index 100% rename from Utils/MathUtils.cs rename to Shared/Utils/MathUtils.cs diff --git a/Extensions/WpfExtensions.cs b/Wpf/Extensions/WpfExtensions.cs similarity index 80% rename from Extensions/WpfExtensions.cs rename to Wpf/Extensions/WpfExtensions.cs index 502877f..b169caf 100644 --- a/Extensions/WpfExtensions.cs +++ b/Wpf/Extensions/WpfExtensions.cs @@ -1,6 +1,6 @@ using System.Windows.Controls; -namespace MeloongCore.Extensions; +namespace MeloongCore.Wpf.Extensions; public static class WpfExtensions { public static bool Any(this UIElementCollection? Arr) => Arr?.Count > 0; diff --git a/Wpf/MeloongCoreWpf.csproj b/Wpf/MeloongCoreWpf.csproj new file mode 100644 index 0000000..8df8427 --- /dev/null +++ b/Wpf/MeloongCoreWpf.csproj @@ -0,0 +1,65 @@ + + + + net462 + Library + True + bin\ + false + false + Debug;Release;Snapshot + true + 13.0 + enable + latest-recommended + MeloongCore.Wpf + MeloongCore.Wpf + True + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(DefineConstants);SNAPSHOT + + + + + + True + + +