Refactor enum value retrieval to use EnumUtils for better compatibility with NetStandard 2.0 and AOT

This commit is contained in:
Phil Scott 2024-11-19 23:15:35 -05:00 committed by Patrik Svensson
parent 4802751357
commit 10773a5625
3 changed files with 17 additions and 3 deletions

View File

@ -54,8 +54,7 @@ internal static class DecorationTable
{ {
var result = new List<string>(); var result = new List<string>();
Enum.GetValues(typeof(Decoration)) EnumUtils.GetValues<Decoration>()
.Cast<Decoration>()
.Where(flag => (decoration & flag) != 0) .Where(flag => (decoration & flag) != 0)
.ForEach(flag => .ForEach(flag =>
{ {

View File

@ -140,7 +140,7 @@ internal struct FileSize
bytes *= 8; bytes *= 8;
} }
foreach (var prefix in (FileSizePrefix[])Enum.GetValues(typeof(FileSizePrefix))) foreach (var prefix in EnumUtils.GetValues<FileSizePrefix>())
{ {
// Trying to find the largest unit, that the number of bytes can fit under. Ex. 40kb < 1mb // Trying to find the largest unit, that the number of bytes can fit under. Ex. 40kb < 1mb
if (bytes < Math.Pow((int)_prefixBase, (int)prefix + 1)) if (bytes < Math.Pow((int)_prefixBase, (int)prefix + 1))

View File

@ -0,0 +1,15 @@
namespace Spectre.Console;
internal static class EnumUtils
{
public static T[] GetValues<T>()
where T : struct, Enum
{
return
#if NET6_0_OR_GREATER
Enum.GetValues<T>();
#else
(T[])Enum.GetValues(typeof(T));
#endif
}
}