diff --git a/Lina/Lina.csproj b/Lina/Lina.csproj index a264636..755749a 100644 --- a/Lina/Lina.csproj +++ b/Lina/Lina.csproj @@ -5,7 +5,7 @@ enable enable true - 2.0.0-preview.6 + 2.0.0-preview.8 TakasakiStudio.Lina TakasakiStudio https://github.com/Takasaki-Studio/Lina diff --git a/Lina/UtilsExtensions/EnumExtensions.cs b/Lina/UtilsExtensions/EnumExtensions.cs index fbb7af8..1218155 100644 --- a/Lina/UtilsExtensions/EnumExtensions.cs +++ b/Lina/UtilsExtensions/EnumExtensions.cs @@ -33,12 +33,31 @@ public static T ToFlags(this IEnumerable list) where T : struct, Enum { var result = 0UL; + var underlyingType = Enum.GetUnderlyingType(typeof(T)); foreach (var flag in list) { - var flagInNumber = Convert.ToUInt64(flag); - result |= flagInNumber; + var flagInNumber = Convert.ChangeType(flag, underlyingType).ToString(); + if (flagInNumber is not null) + { + result |= uint.Parse(flagInNumber); + } } - return (T)Enum.ToObject(typeof(T), result); + return (T)Enum.ToObject(typeof(T), Convert.ChangeType(result, underlyingType)); + } + + public static IEnumerable ToEnumerable(this T flag) + where T : struct, Enum + { + var flagInNumber = Convert.ToUInt64(flag); + var underlyingType = Enum.GetUnderlyingType(typeof(T)); + return Enum.GetValues() + .Select(x => + { + var stringConverted = Convert.ChangeType(x, underlyingType).ToString(); + return ulong.Parse(stringConverted ?? "0"); + }) + .Where(x => (x & flagInNumber) == x) + .Select(x => (T)Enum.ToObject(typeof(T), x)); } } \ No newline at end of file