diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index 38ed61a36610c..d69077cfb9c52 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -258,11 +258,26 @@ internal static ulong ToUInt64(object value) return result; } + private static ulong ToUInt64(TEnum value) where TEnum : struct, Enum => + Type.GetTypeCode(typeof(TEnum)) switch + { + TypeCode.SByte => (ulong)Unsafe.As(ref value), + TypeCode.Byte => Unsafe.As(ref value), + TypeCode.Boolean => Convert.ToByte(Unsafe.As(ref value)), + TypeCode.Int16 => (ulong)Unsafe.As(ref value), + TypeCode.UInt16 => Unsafe.As(ref value), + TypeCode.Char => Unsafe.As(ref value), + TypeCode.UInt32 => Unsafe.As(ref value), + TypeCode.Int32 => (ulong)Unsafe.As(ref value), + TypeCode.UInt64 => Unsafe.As(ref value), + TypeCode.Int64 => (ulong)Unsafe.As(ref value), + _ => throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType), + }; #endregion #region Public Static Methods public static string? GetName(TEnum value) where TEnum : struct, Enum - => GetName(typeof(TEnum), value); + => GetEnumName((RuntimeType)typeof(TEnum), ToUInt64(value)); public static string? GetName(Type enumType, object value) { @@ -273,7 +288,7 @@ internal static ulong ToUInt64(object value) } public static string[] GetNames() where TEnum : struct, Enum - => GetNames(typeof(TEnum)); + => new ReadOnlySpan(InternalGetNames((RuntimeType)typeof(TEnum))).ToArray(); public static string[] GetNames(Type enumType) {