ifdef out unsupported Enum underlying types for nativeaot (#79472)
authorStephen Toub <stoub@microsoft.com>
Thu, 15 Dec 2022 01:02:42 +0000 (20:02 -0500)
committerGitHub <noreply@github.com>
Thu, 15 Dec 2022 01:02:42 +0000 (17:02 -0800)
* ifdef out unsupported Enum underlying types for nativeaot

* Use dedicated ifdef

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
src/libraries/Microsoft.VisualBasic.Core/tests/ConversionsTests.cs
src/libraries/System.Linq.Expressions/tests/Cast/CastTests.cs
src/libraries/System.Private.CoreLib/src/System/Enum.cs
src/libraries/System.Reflection.Emit.ILGeneration/tests/CustomAttributeBuilderTests.cs
src/libraries/System.Runtime/tests/System/EnumTests.cs

index 33cec4f747145a62130897aa8d1e41bddda6ba60..0f33b2a664b8eeb595812e95630f7dc1a2ee3ce8 100644 (file)
@@ -189,6 +189,8 @@ namespace System
 
         public static bool IsPreciseGcSupported => !IsMonoRuntime;
 
+        public static bool IsRareEnumsSupported => !IsNativeAot;
+
         public static bool IsNotIntMaxValueArrayIndexSupported => s_largeArrayIsNotSupported.Value;
 
         public static bool IsAssemblyLoadingSupported => !IsNativeAot;
index 354a744d0188821b6e2cea2c7710c2e1e5f75118..3d0366a96bb2fdcd73f76402d1a0b1d274f007f7 100644 (file)
@@ -13,29 +13,6 @@ namespace Microsoft.VisualBasic.Tests
 {
     public class ConversionsTests
     {
-        private static bool? s_reflectionEmitSupported = null;
-
-        public static bool ReflectionEmitSupported
-        {
-            get
-            {
-                if (s_reflectionEmitSupported == null)
-                {
-                    try
-                    {
-                        object o = FloatEnum;
-                        s_reflectionEmitSupported = true;
-                    }
-                    catch (PlatformNotSupportedException)
-                    {
-                        s_reflectionEmitSupported = false;
-                    }
-                }
-
-                return s_reflectionEmitSupported.Value;
-            }
-        }
-
         public static IEnumerable<object[]> InvalidString_TestData()
         {
             yield return new object[] { "" };
@@ -49,7 +26,7 @@ namespace Microsoft.VisualBasic.Tests
 
         public static IEnumerable<object[]> InvalidBool_TestData()
         {
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { FloatEnum };
                 yield return new object[] { DoubleEnum };
@@ -65,7 +42,7 @@ namespace Microsoft.VisualBasic.Tests
             yield return new object[] { char.MaxValue };
             yield return new object[] { new DateTime(10) };
             yield return new object[] { new object() };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { CharEnum };
             }
@@ -182,7 +159,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, byte.MaxValue };
             yield return new object[] { false, byte.MinValue };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, byte.MinValue };
             }
@@ -363,7 +340,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, (sbyte)(-1) };
             yield return new object[] { false, (sbyte)0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, (sbyte)0 };
             }
@@ -537,7 +514,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, ushort.MaxValue };
             yield return new object[] { false, ushort.MinValue };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, ushort.MinValue };
             }
@@ -721,7 +698,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, (short)(-1) };
             yield return new object[] { false, (short)0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, (short)0 };
             }
@@ -899,7 +876,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, uint.MaxValue };
             yield return new object[] { false, uint.MinValue };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, uint.MinValue };
             }
@@ -1089,7 +1066,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, -1 };
             yield return new object[] { false, 0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, 0 };
             }
@@ -1275,7 +1252,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, ulong.MaxValue };
             yield return new object[] { false, ulong.MinValue };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, ulong.MinValue };
             }
@@ -1503,7 +1480,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, (long)(-1) };
             yield return new object[] { false, (long)0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, (long)0 };
             }
@@ -1729,7 +1706,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, (float)(-1) };
             yield return new object[] { false, (float)0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, (float)0 };
             }
@@ -1922,7 +1899,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, (double)(-1) };
             yield return new object[] { false, (double)0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, (double)0 };
             }
@@ -2118,7 +2095,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, (decimal)(-1) };
             yield return new object[] { false, (decimal)0 };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, (decimal)0 };
             }
@@ -2344,7 +2321,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, true };
             yield return new object[] { false, false };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, false };
             }
@@ -2659,7 +2636,7 @@ namespace Microsoft.VisualBasic.Tests
             // bool.
             yield return new object[] { true, "True" };
             yield return new object[] { false, "False" };
-            if (ReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { BoolEnum, "False" };
             }
index 0f0294a58465c4121d4bfee8519590de91206398..8a3b0569baa2cca8ec4d097943dbe7e475767ea1 100644 (file)
@@ -2373,7 +2373,7 @@ namespace System.Linq.Expressions.Tests
             yield return typeof(Int64Enum);
             yield return typeof(UInt64Enum);
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return NonCSharpTypes.CharEnumType;
                 yield return NonCSharpTypes.BoolEnumType;
index 0b3f59beaad68f9abe94e1babca8fa845811bbc1..992448c3304439b323018504afb65455c6575f00 100644 (file)
@@ -1,6 +1,11 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+// Rare enums types not expressible in C# are not supported in native AOT
+#if !NATIVEAOT
+#define RARE_ENUMS
+#endif
+
 using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
@@ -41,21 +46,22 @@ namespace System
             RuntimeType rt = (RuntimeType)typeof(TEnum);
             Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
 
+            if (underlyingType == typeof(sbyte)) return GetNameInlined(GetEnumInfo<sbyte>(rt), *(sbyte*)&value);
+            if (underlyingType == typeof(byte)) return GetNameInlined(GetEnumInfo<byte>(rt), *(byte*)&value);
+            if (underlyingType == typeof(short)) return GetNameInlined(GetEnumInfo<short>(rt), *(short*)&value);
+            if (underlyingType == typeof(ushort)) return GetNameInlined(GetEnumInfo<ushort>(rt), *(ushort*)&value);
             if (underlyingType == typeof(int)) return GetNameInlined(GetEnumInfo<int>(rt), *(int*)&value);
             if (underlyingType == typeof(uint)) return GetNameInlined(GetEnumInfo<uint>(rt), *(uint*)&value);
             if (underlyingType == typeof(long)) return GetNameInlined(GetEnumInfo<long>(rt), *(long*)&value);
             if (underlyingType == typeof(ulong)) return GetNameInlined(GetEnumInfo<ulong>(rt), *(ulong*)&value);
-            if (underlyingType == typeof(byte)) return GetNameInlined(GetEnumInfo<byte>(rt), *(byte*)&value);
-            if (underlyingType == typeof(sbyte)) return GetNameInlined(GetEnumInfo<sbyte>(rt), *(sbyte*)&value);
-            if (underlyingType == typeof(short)) return GetNameInlined(GetEnumInfo<short>(rt), *(short*)&value);
-            if (underlyingType == typeof(ushort)) return GetNameInlined(GetEnumInfo<ushort>(rt), *(ushort*)&value);
+#if RARE_ENUMS
             if (underlyingType == typeof(nint)) return GetNameInlined(GetEnumInfo<nint>(rt), *(nint*)&value);
             if (underlyingType == typeof(nuint)) return GetNameInlined(GetEnumInfo<nuint>(rt), *(nuint*)&value);
-            if (underlyingType == typeof(char)) return GetNameInlined(GetEnumInfo<char>(rt), *(char*)&value);
             if (underlyingType == typeof(float)) return GetNameInlined(GetEnumInfo<float>(rt), *(float*)&value);
             if (underlyingType == typeof(double)) return GetNameInlined(GetEnumInfo<double>(rt), *(double*)&value);
+            if (underlyingType == typeof(char)) return GetNameInlined(GetEnumInfo<char>(rt), *(char*)&value);
             if (underlyingType == typeof(bool)) return GetNameInlined(GetEnumInfo<byte>(rt), *(bool*)&value ? (byte)1 : (byte)0);
-
+#endif
             throw CreateUnknownEnumTypeException();
         }
 
@@ -100,10 +106,6 @@ namespace System
                     if (uint64Value > byte.MaxValue) return null;
                     return GetName(GetEnumInfo<byte>(enumType), (byte)uint64Value);
 
-                case TypeCode.Boolean:
-                    if (uint64Value > 1) return null;
-                    return GetName(GetEnumInfo<byte>(enumType), (byte)uint64Value);
-
                 case TypeCode.Int16:
                     if ((long)uint64Value < short.MinValue || (long)uint64Value > short.MaxValue) return null;
                     return GetName(GetEnumInfo<short>(enumType), (short)uint64Value);
@@ -112,25 +114,32 @@ namespace System
                     if (uint64Value > ushort.MaxValue) return null;
                     return GetName(GetEnumInfo<ushort>(enumType), (ushort)uint64Value);
 
-                case TypeCode.Char:
-                    if (uint64Value > char.MaxValue) return null;
-                    return GetName(GetEnumInfo<char>(enumType), (char)uint64Value);
+                case TypeCode.Int32:
+                    if ((long)uint64Value < int.MinValue || (long)uint64Value > int.MaxValue) return null;
+                    return GetName(GetEnumInfo<int>(enumType), (int)uint64Value);
 
                 case TypeCode.UInt32:
                     if (uint64Value > uint.MaxValue) return null;
                     return GetName(GetEnumInfo<uint>(enumType), (uint)uint64Value);
 
-                case TypeCode.Int32:
-                    if ((long)uint64Value < int.MinValue || (long)uint64Value > int.MaxValue) return null;
-                    return GetName(GetEnumInfo<int>(enumType), (int)uint64Value);
+                case TypeCode.Int64:
+                    return GetName(GetEnumInfo<long>(enumType), (long)uint64Value);
 
                 case TypeCode.UInt64:
                     return GetName(GetEnumInfo<ulong>(enumType), uint64Value);
 
-                case TypeCode.Int64:
-                    return GetName(GetEnumInfo<long>(enumType), (long)uint64Value);
+#if RARE_ENUMS
+                case TypeCode.Char:
+                    if (uint64Value > char.MaxValue) return null;
+                    return GetName(GetEnumInfo<char>(enumType), (char)uint64Value);
+
+                case TypeCode.Boolean:
+                    if (uint64Value > 1) return null;
+                    return GetName(GetEnumInfo<byte>(enumType), (byte)uint64Value);
+#endif
             };
 
+#if RARE_ENUMS
             if (underlyingType == typeof(nint))
             {
                 if ((long)uint64Value < nint.MinValue || (long)uint64Value > nint.MaxValue) return null;
@@ -142,6 +151,7 @@ namespace System
                 if (uint64Value > nuint.MaxValue) return null;
                 return GetName(GetEnumInfo<nuint>(enumType), (nuint)uint64Value);
             }
+#endif
 
             throw CreateUnknownEnumTypeException();
         }
@@ -211,20 +221,22 @@ namespace System
             Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
 
             // Get the cached names array.
-            if (underlyingType == typeof(int)) names = GetEnumInfo<int>(rt).Names;
-            else if (underlyingType == typeof(uint)) names = GetEnumInfo<uint>(rt).Names;
-            else if (underlyingType == typeof(long)) names = GetEnumInfo<long>(rt).Names;
-            else if (underlyingType == typeof(ulong)) names = GetEnumInfo<ulong>(rt).Names;
+            if (underlyingType == typeof(sbyte)) names = GetEnumInfo<sbyte>(rt).Names;
             else if (underlyingType == typeof(byte)) names = GetEnumInfo<byte>(rt).Names;
-            else if (underlyingType == typeof(sbyte)) names = GetEnumInfo<sbyte>(rt).Names;
             else if (underlyingType == typeof(short)) names = GetEnumInfo<short>(rt).Names;
             else if (underlyingType == typeof(ushort)) names = GetEnumInfo<ushort>(rt).Names;
+            else if (underlyingType == typeof(int)) names = GetEnumInfo<int>(rt).Names;
+            else if (underlyingType == typeof(uint)) names = GetEnumInfo<uint>(rt).Names;
+            else if (underlyingType == typeof(long)) names = GetEnumInfo<long>(rt).Names;
+            else if (underlyingType == typeof(ulong)) names = GetEnumInfo<ulong>(rt).Names;
+#if RARE_ENUMS
             else if (underlyingType == typeof(nint)) names = GetEnumInfo<nint>(rt).Names;
             else if (underlyingType == typeof(nuint)) names = GetEnumInfo<nuint>(rt).Names;
-            else if (underlyingType == typeof(char)) names = GetEnumInfo<char>(rt).Names;
             else if (underlyingType == typeof(float)) names = GetEnumInfo<float>(rt).Names;
             else if (underlyingType == typeof(double)) names = GetEnumInfo<double>(rt).Names;
+            else if (underlyingType == typeof(char)) names = GetEnumInfo<char>(rt).Names;
             else if (underlyingType == typeof(bool)) names = GetEnumInfo<byte>(rt).Names;
+#endif
             else throw CreateUnknownEnumTypeException();
 
             // Return a clone of the array to avoid exposing the cached array instance.
@@ -258,12 +270,14 @@ namespace System
                 CorElementType.ELEMENT_TYPE_U4 => GetEnumInfo<uint>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_I8 => GetEnumInfo<long>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_U8 => GetEnumInfo<ulong>(enumType).Names,
+#if RARE_ENUMS
                 CorElementType.ELEMENT_TYPE_R4 => GetEnumInfo<float>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_R8 => GetEnumInfo<double>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_I => GetEnumInfo<nint>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_U => GetEnumInfo<nuint>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo<char>(enumType).Names,
                 CorElementType.ELEMENT_TYPE_BOOLEAN => GetEnumInfo<byte>(enumType).Names,
+#endif
                 _ => throw CreateUnknownEnumTypeException(),
             };
         }
@@ -345,12 +359,14 @@ namespace System
                 CorElementType.ELEMENT_TYPE_U4 => GetEnumInfo<uint>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_I8 => GetEnumInfo<long>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_U8 => GetEnumInfo<ulong>(enumType, getNames: false).CloneValues(),
+#if RARE_ENUMS
                 CorElementType.ELEMENT_TYPE_R4 => GetEnumInfo<float>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_R8 => GetEnumInfo<double>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_I => GetEnumInfo<nint>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_U => GetEnumInfo<nuint>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo<char>(enumType, getNames: false).CloneValues(),
                 CorElementType.ELEMENT_TYPE_BOOLEAN => CopyByteArrayToNewBoolArray(GetEnumInfo<byte>(enumType, getNames: false).Values),
+#endif
                 _ => throw CreateUnknownEnumTypeException(),
             };
         }
@@ -371,12 +387,14 @@ namespace System
                 CorElementType.ELEMENT_TYPE_U4 => GetEnumInfo<uint>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_I8 => GetEnumInfo<long>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_U8 => GetEnumInfo<ulong>(enumType, getNames: false).Values,
+#if RARE_ENUMS
                 CorElementType.ELEMENT_TYPE_R4 => GetEnumInfo<float>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_R8 => GetEnumInfo<double>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_I => GetEnumInfo<nint>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_U => GetEnumInfo<nuint>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_CHAR => GetEnumInfo<char>(enumType, getNames: false).Values,
                 CorElementType.ELEMENT_TYPE_BOOLEAN => CopyByteArrayToNewBoolArray(GetEnumInfo<byte>(enumType, getNames: false).Values), // this is the only case that clones, out of necessity
+#endif
                 _ => throw CreateUnknownEnumTypeException(),
             };
         }
@@ -400,7 +418,6 @@ namespace System
             {
                 case CorElementType.ELEMENT_TYPE_I1:
                 case CorElementType.ELEMENT_TYPE_U1:
-                case CorElementType.ELEMENT_TYPE_BOOLEAN:
                     {
                         byte flagsValue = pFlagsValue;
                         return (pThisValue & flagsValue) == flagsValue;
@@ -408,7 +425,6 @@ namespace System
 
                 case CorElementType.ELEMENT_TYPE_I2:
                 case CorElementType.ELEMENT_TYPE_U2:
-                case CorElementType.ELEMENT_TYPE_CHAR:
                     {
                         ushort flagsValue = Unsafe.As<byte, ushort>(ref pFlagsValue);
                         return (Unsafe.As<byte, ushort>(ref pThisValue) & flagsValue) == flagsValue;
@@ -416,11 +432,6 @@ namespace System
 
                 case CorElementType.ELEMENT_TYPE_I4:
                 case CorElementType.ELEMENT_TYPE_U4:
-#if TARGET_32BIT
-                case CorElementType.ELEMENT_TYPE_I:
-                case CorElementType.ELEMENT_TYPE_U:
-#endif
-                case CorElementType.ELEMENT_TYPE_R4:
                     {
                         uint flagsValue = Unsafe.As<byte, uint>(ref pFlagsValue);
                         return (Unsafe.As<byte, uint>(ref pThisValue) & flagsValue) == flagsValue;
@@ -428,16 +439,33 @@ namespace System
 
                 case CorElementType.ELEMENT_TYPE_I8:
                 case CorElementType.ELEMENT_TYPE_U8:
-#if TARGET_64BIT
-                case CorElementType.ELEMENT_TYPE_I:
-                case CorElementType.ELEMENT_TYPE_U:
-#endif
-                case CorElementType.ELEMENT_TYPE_R8:
                     {
                         ulong flagsValue = Unsafe.As<byte, ulong>(ref pFlagsValue);
                         return (Unsafe.As<byte, ulong>(ref pThisValue) & flagsValue) == flagsValue;
                     }
 
+#if RARE_ENUMS
+                case CorElementType.ELEMENT_TYPE_BOOLEAN:
+                    goto case CorElementType.ELEMENT_TYPE_U1;
+
+                case CorElementType.ELEMENT_TYPE_CHAR:
+                    goto case CorElementType.ELEMENT_TYPE_U2;
+
+                case CorElementType.ELEMENT_TYPE_R4:
+                    goto case CorElementType.ELEMENT_TYPE_U4;
+
+                case CorElementType.ELEMENT_TYPE_R8:
+                    goto case CorElementType.ELEMENT_TYPE_U8;
+
+                case CorElementType.ELEMENT_TYPE_I:
+                case CorElementType.ELEMENT_TYPE_U:
+#if TARGET_32BIT
+                    goto case CorElementType.ELEMENT_TYPE_U4;
+#else
+                    goto case CorElementType.ELEMENT_TYPE_U8;
+#endif
+#endif
+
                 default:
                     Debug.Fail("Unknown enum underlying type");
                     return false;
@@ -471,20 +499,22 @@ namespace System
             RuntimeType rt = (RuntimeType)typeof(TEnum);
             Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
 
+            if (underlyingType == typeof(sbyte)) return IsDefinedPrimitive(rt, *(sbyte*)&value);
+            if (underlyingType == typeof(byte)) return IsDefinedPrimitive(rt, *(byte*)&value);
+            if (underlyingType == typeof(short)) return IsDefinedPrimitive(rt, *(short*)&value);
+            if (underlyingType == typeof(ushort)) return IsDefinedPrimitive(rt, *(ushort*)&value);
             if (underlyingType == typeof(int)) return IsDefinedPrimitive(rt, *(int*)&value);
             if (underlyingType == typeof(uint)) return IsDefinedPrimitive(rt, *(uint*)&value);
             if (underlyingType == typeof(long)) return IsDefinedPrimitive(rt, *(long*)&value);
             if (underlyingType == typeof(ulong)) return IsDefinedPrimitive(rt, *(ulong*)&value);
-            if (underlyingType == typeof(byte)) return IsDefinedPrimitive(rt, *(byte*)&value);
-            if (underlyingType == typeof(sbyte)) return IsDefinedPrimitive(rt, *(sbyte*)&value);
-            if (underlyingType == typeof(short)) return IsDefinedPrimitive(rt, *(short*)&value);
-            if (underlyingType == typeof(ushort)) return IsDefinedPrimitive(rt, *(ushort*)&value);
+#if RARE_ENUMS
             if (underlyingType == typeof(nint)) return IsDefinedPrimitive(rt, *(nint*)&value);
             if (underlyingType == typeof(nuint)) return IsDefinedPrimitive(rt, *(nuint*)&value);
-            if (underlyingType == typeof(char)) return IsDefinedPrimitive(rt, *(char*)&value);
             if (underlyingType == typeof(float)) return IsDefinedPrimitive(rt, *(float*)&value);
             if (underlyingType == typeof(double)) return IsDefinedPrimitive(rt, *(double*)&value);
+            if (underlyingType == typeof(char)) return IsDefinedPrimitive(rt, *(char*)&value);
             if (underlyingType == typeof(bool)) return IsDefinedPrimitive(rt, *(bool*)&value ? (byte)1 : (byte)0);
+#endif
 
             throw CreateUnknownEnumTypeException();
         }
@@ -774,6 +804,7 @@ namespace System
             [MethodImpl(MethodImplOptions.NoInlining)]
             static bool TryParseRareTypes(RuntimeType rt, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, [NotNullWhen(true)] out long result)
             {
+#if RARE_ENUMS
                 bool parsed;
 
                 switch (InternalGetCorElementType(rt))
@@ -825,6 +856,9 @@ namespace System
                 }
 
                 return parsed;
+#else
+                throw CreateUnknownEnumTypeException();
+#endif
             }
         }
 
@@ -895,27 +929,24 @@ namespace System
             RuntimeType rt = (RuntimeType)typeof(TEnum);
             Type underlyingType = typeof(TEnum).GetEnumUnderlyingType();
 
+            if (underlyingType == typeof(sbyte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, sbyte>(ref result));
+            if (underlyingType == typeof(byte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, byte>(ref result));
+            if (underlyingType == typeof(short)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, short>(ref result));
+            if (underlyingType == typeof(ushort)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, ushort>(ref result));
             if (underlyingType == typeof(int)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, int>(ref result));
             if (underlyingType == typeof(uint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, uint>(ref result));
             if (underlyingType == typeof(long)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, long>(ref result));
             if (underlyingType == typeof(ulong)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, ulong>(ref result));
-            if (underlyingType == typeof(byte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, byte>(ref result));
-            if (underlyingType == typeof(sbyte)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, sbyte>(ref result));
-            if (underlyingType == typeof(short)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, short>(ref result));
-            if (underlyingType == typeof(ushort)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, ushort>(ref result));
+#if RARE_ENUMS
             if (underlyingType == typeof(float)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, float>(ref result));
             if (underlyingType == typeof(double)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, double>(ref result));
-            if (underlyingType == typeof(char)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, char>(ref result));
             if (underlyingType == typeof(nint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, nint>(ref result));
             if (underlyingType == typeof(nuint)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, nuint>(ref result));
+            if (underlyingType == typeof(char)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, char>(ref result));
             if (underlyingType == typeof(bool)) return TryParseByValueOrName(rt, value, ignoreCase, throwOnFailure, out Unsafe.As<TEnum, byte>(ref result));
+#endif
 
-            if (throwOnFailure)
-            {
-                throw CreateUnknownEnumTypeException();
-            }
-            result = default;
-            return false;
+            throw CreateUnknownEnumTypeException();
         }
 
         /// <summary>Core implementation for all {Try}Parse methods, both generic and non-generic, parsing either by value or by name.</summary>
@@ -1010,7 +1041,7 @@ namespace System
                 }
                 else
                 {
-                    // Rare types not expressible in C#
+#if RARE_ENUMS
                     Type underlyingType = GetUnderlyingType(enumType);
                     try
                     {
@@ -1025,6 +1056,9 @@ namespace System
                     {
                         status = Number.ParsingStatus.Overflow; // fall through to returning failure
                     }
+#else
+                    throw CreateUnknownEnumTypeException();
+#endif
                 }
 
                 if (status != Number.ParsingStatus.Overflow)
@@ -1145,14 +1179,14 @@ namespace System
             {
                 case TypeCode.SByte: return (ulong)(sbyte)value;
                 case TypeCode.Byte: return (byte)value;
-                case TypeCode.Boolean: return (bool)value ? 1UL : 0UL;
                 case TypeCode.Int16: return (ulong)(short)value;
                 case TypeCode.UInt16: return (ushort)value;
-                case TypeCode.Char: return (char)value;
-                case TypeCode.UInt32: return (uint)value;
                 case TypeCode.Int32: return (ulong)(int)value;
-                case TypeCode.UInt64: return (ulong)value;
                 case TypeCode.Int64: return (ulong)(long)value;
+                case TypeCode.UInt32: return (uint)value;
+                case TypeCode.UInt64: return (ulong)value;
+                case TypeCode.Char: return (char)value;
+                case TypeCode.Boolean: return (bool)value ? 1UL : 0UL;
             };
 
             if (value is not null)
@@ -1184,12 +1218,14 @@ namespace System
                 CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref data),
                 CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref data),
                 CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref data),
+#if RARE_ENUMS
                 CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref data),
                 CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref data),
                 CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref data),
                 CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref data),
                 CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref data),
                 CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref data),
+#endif
                 _ => throw CreateUnknownEnumTypeException(),
             };
         }
@@ -1213,31 +1249,41 @@ namespace System
             {
                 case CorElementType.ELEMENT_TYPE_I1:
                 case CorElementType.ELEMENT_TYPE_U1:
-                case CorElementType.ELEMENT_TYPE_BOOLEAN:
                     return pThisValue == pOtherValue;
 
                 case CorElementType.ELEMENT_TYPE_I2:
                 case CorElementType.ELEMENT_TYPE_U2:
-                case CorElementType.ELEMENT_TYPE_CHAR:
                     return Unsafe.As<byte, ushort>(ref pThisValue) == Unsafe.As<byte, ushort>(ref pOtherValue);
 
                 case CorElementType.ELEMENT_TYPE_I4:
                 case CorElementType.ELEMENT_TYPE_U4:
-#if TARGET_32BIT
-                case CorElementType.ELEMENT_TYPE_I:
-                case CorElementType.ELEMENT_TYPE_U:
-#endif
-                case CorElementType.ELEMENT_TYPE_R4:
                     return Unsafe.As<byte, uint>(ref pThisValue) == Unsafe.As<byte, uint>(ref pOtherValue);
 
                 case CorElementType.ELEMENT_TYPE_I8:
                 case CorElementType.ELEMENT_TYPE_U8:
-#if TARGET_64BIT
+                    return Unsafe.As<byte, ulong>(ref pThisValue) == Unsafe.As<byte, ulong>(ref pOtherValue);
+
+#if RARE_ENUMS
+                case CorElementType.ELEMENT_TYPE_BOOLEAN:
+                    goto case CorElementType.ELEMENT_TYPE_U1;
+
+                case CorElementType.ELEMENT_TYPE_CHAR:
+                    goto case CorElementType.ELEMENT_TYPE_U2;
+
+                case CorElementType.ELEMENT_TYPE_R4:
+                    goto case CorElementType.ELEMENT_TYPE_U4;
+
+                case CorElementType.ELEMENT_TYPE_R8:
+                    goto case CorElementType.ELEMENT_TYPE_U8;
+
                 case CorElementType.ELEMENT_TYPE_I:
                 case CorElementType.ELEMENT_TYPE_U:
+#if TARGET_32BIT
+                    goto case CorElementType.ELEMENT_TYPE_U4;
+#else
+                    goto case CorElementType.ELEMENT_TYPE_U8;
+#endif
 #endif
-                case CorElementType.ELEMENT_TYPE_R8:
-                    return Unsafe.As<byte, ulong>(ref pThisValue) == Unsafe.As<byte, ulong>(ref pOtherValue);
 
                 default:
                     Debug.Fail("Unknown enum underlying type");
@@ -1262,12 +1308,14 @@ namespace System
                 CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref data).GetHashCode(),
+#if RARE_ENUMS
                 CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref data).GetHashCode(),
                 CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref data).GetHashCode(),
+#endif
                 _ => throw CreateUnknownEnumTypeException(),
             };
         }
@@ -1293,46 +1341,52 @@ namespace System
                     return Unsafe.As<byte, sbyte>(ref pThisValue).CompareTo(Unsafe.As<byte, sbyte>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_U1:
-                case CorElementType.ELEMENT_TYPE_BOOLEAN:
                     return pThisValue.CompareTo(pTargetValue);
 
                 case CorElementType.ELEMENT_TYPE_I2:
                     return Unsafe.As<byte, short>(ref pThisValue).CompareTo(Unsafe.As<byte, short>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_U2:
-                case CorElementType.ELEMENT_TYPE_CHAR:
                     return Unsafe.As<byte, ushort>(ref pThisValue).CompareTo(Unsafe.As<byte, ushort>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_I4:
-#if TARGET_32BIT
-                case CorElementType.ELEMENT_TYPE_I:
-#endif
                     return Unsafe.As<byte, int>(ref pThisValue).CompareTo(Unsafe.As<byte, int>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_U4:
-#if TARGET_32BIT
-                case CorElementType.ELEMENT_TYPE_U:
-#endif
                     return Unsafe.As<byte, uint>(ref pThisValue).CompareTo(Unsafe.As<byte, uint>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_I8:
-#if TARGET_64BIT
-                case CorElementType.ELEMENT_TYPE_I:
-#endif
                     return Unsafe.As<byte, long>(ref pThisValue).CompareTo(Unsafe.As<byte, long>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_U8:
-#if TARGET_64BIT
-                case CorElementType.ELEMENT_TYPE_U:
-#endif
                     return Unsafe.As<byte, ulong>(ref pThisValue).CompareTo(Unsafe.As<byte, ulong>(ref pTargetValue));
 
+#if RARE_ENUMS
                 case CorElementType.ELEMENT_TYPE_R4:
                     return Unsafe.As<byte, float>(ref pThisValue).CompareTo(Unsafe.As<byte, float>(ref pTargetValue));
 
                 case CorElementType.ELEMENT_TYPE_R8:
                     return Unsafe.As<byte, double>(ref pThisValue).CompareTo(Unsafe.As<byte, double>(ref pTargetValue));
 
+                case CorElementType.ELEMENT_TYPE_BOOLEAN:
+                    goto case CorElementType.ELEMENT_TYPE_U1;
+
+                case CorElementType.ELEMENT_TYPE_CHAR:
+                    goto case CorElementType.ELEMENT_TYPE_U2;
+
+#if TARGET_32BIT
+                case CorElementType.ELEMENT_TYPE_I:
+                    goto case CorElementType.ELEMENT_TYPE_I4;
+                case CorElementType.ELEMENT_TYPE_U:
+                    goto case CorElementType.ELEMENT_TYPE_U4;
+#else
+                case CorElementType.ELEMENT_TYPE_I:
+                    goto case CorElementType.ELEMENT_TYPE_I8;
+                case CorElementType.ELEMENT_TYPE_U:
+                    goto case CorElementType.ELEMENT_TYPE_U8;
+#endif
+#endif
+
                 default:
                     Debug.Fail("Unknown enum underlying type");
                     return 0;
@@ -1361,6 +1415,7 @@ namespace System
 
             [MethodImpl(MethodImplOptions.NoInlining)]
             static string HandleRareTypes(RuntimeType enumType, ref byte rawData) =>
+#if RARE_ENUMS
                 InternalGetCorElementType(enumType) switch
                 {
                     CorElementType.ELEMENT_TYPE_R4 => ToString<float>(enumType, ref rawData),
@@ -1371,6 +1426,9 @@ namespace System
                     CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(enumType, null, ref rawData),
                     _ => throw CreateUnknownEnumTypeException(),
                 };
+#else
+                throw CreateUnknownEnumTypeException();
+#endif
         }
 
         /// <summary>Converts the value of this instance to its equivalent string representation using the specified format.</summary>
@@ -1409,6 +1467,7 @@ namespace System
 
             [MethodImpl(MethodImplOptions.NoInlining)]
             static string HandleRareTypes(RuntimeType enumType, char formatChar, ref byte rawData) =>
+#if RARE_ENUMS
                 InternalGetCorElementType(enumType) switch
                 {
                     CorElementType.ELEMENT_TYPE_R4 => ToString<float>(enumType, formatChar, ref rawData),
@@ -1419,6 +1478,9 @@ namespace System
                     CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(enumType, formatChar.ToString(), ref rawData),
                     _ => throw CreateUnknownEnumTypeException(),
                 };
+#else
+                throw CreateUnknownEnumTypeException();
+#endif
         }
 
         /// <summary>This method overload is obsolete; use <see cref="ToString()"/>.</summary>
@@ -1704,12 +1766,14 @@ namespace System
                         CorElementType.ELEMENT_TYPE_U4 => ToString<uint>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_I8 => ToString<long>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_U8 => ToString<ulong>(rtType, formatChar, ref rawData),
+#if RARE_ENUMS
                         CorElementType.ELEMENT_TYPE_R4 => ToString<float>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_R8 => ToString<double>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_I => ToString<nint>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_U => ToString<nuint>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_CHAR => ToString<char>(rtType, formatChar, ref rawData),
                         CorElementType.ELEMENT_TYPE_BOOLEAN => ToStringBool(rtType, format, ref rawData),
+#endif
                         _ => throw CreateUnknownEnumTypeException(),
                     };
                 }
@@ -1742,12 +1806,14 @@ namespace System
                     CorElementType.ELEMENT_TYPE_U4 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, uint>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_I8 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, long>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_U8 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, ulong>(ref rawData), destination, out charsWritten),
+#if RARE_ENUMS
                     CorElementType.ELEMENT_TYPE_R4 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, float>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_R8 => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, double>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_I => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, nint>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_U => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, nuint>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_CHAR => TryFormatPrimitiveDefault(enumType, Unsafe.As<byte, char>(ref rawData), destination, out charsWritten),
                     CorElementType.ELEMENT_TYPE_BOOLEAN => TryFormatBool(enumType, rawData != 0, destination, out charsWritten, format),
+#endif
                     _ => throw CreateUnknownEnumTypeException(),
                 };
             }
@@ -1763,12 +1829,14 @@ namespace System
                     CorElementType.ELEMENT_TYPE_U4 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, uint>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_I8 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, long>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_U8 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, ulong>(ref rawData), destination, out charsWritten, format),
+#if RARE_ENUMS
                     CorElementType.ELEMENT_TYPE_R4 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, float>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_R8 => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, double>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_I => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, nint>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_U => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, nuint>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_CHAR => TryFormatPrimitiveNonDefault(enumType, Unsafe.As<byte, char>(ref rawData), destination, out charsWritten, format),
                     CorElementType.ELEMENT_TYPE_BOOLEAN => TryFormatBool(enumType, rawData != 0, destination, out charsWritten, format),
+#endif
                     _ => throw CreateUnknownEnumTypeException(),
                 };
             }
@@ -1801,12 +1869,14 @@ namespace System
                 if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveDefault(rt, *(sbyte*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(short)) return TryFormatPrimitiveDefault(rt, *(short*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(ushort)) return TryFormatPrimitiveDefault(rt, *(ushort*)&value, destination, out charsWritten);
+#if RARE_ENUMS
                 if (underlyingType == typeof(nint)) return TryFormatPrimitiveDefault(rt, *(nint*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(nuint)) return TryFormatPrimitiveDefault(rt, *(nuint*)&value, destination, out charsWritten);
-                if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(float)) return TryFormatPrimitiveDefault(rt, *(float*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(double)) return TryFormatPrimitiveDefault(rt, *(double*)&value, destination, out charsWritten);
+                if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format: default);
+#endif
             }
             else
             {
@@ -1818,12 +1888,14 @@ namespace System
                 if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveNonDefault(rt, *(sbyte*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(short)) return TryFormatPrimitiveNonDefault(rt, *(short*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(ushort)) return TryFormatPrimitiveNonDefault(rt, *(ushort*)&value, destination, out charsWritten, format);
+#if RARE_ENUMS
                 if (underlyingType == typeof(nint)) return TryFormatPrimitiveNonDefault(rt, *(nint*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(nuint)) return TryFormatPrimitiveNonDefault(rt, *(nuint*)&value, destination, out charsWritten, format);
-                if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(float)) return TryFormatPrimitiveNonDefault(rt, *(float*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(double)) return TryFormatPrimitiveNonDefault(rt, *(double*)&value, destination, out charsWritten, format);
+                if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format);
+#endif
             }
 
             throw CreateUnknownEnumTypeException();
@@ -1858,12 +1930,14 @@ namespace System
                 if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveDefault(rt, *(sbyte*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(short)) return TryFormatPrimitiveDefault(rt, *(short*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(ushort)) return TryFormatPrimitiveDefault(rt, *(ushort*)&value, destination, out charsWritten);
+#if RARE_ENUMS
                 if (underlyingType == typeof(nint)) return TryFormatPrimitiveDefault(rt, *(nint*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(nuint)) return TryFormatPrimitiveDefault(rt, *(nuint*)&value, destination, out charsWritten);
-                if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(float)) return TryFormatPrimitiveDefault(rt, *(float*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(double)) return TryFormatPrimitiveDefault(rt, *(double*)&value, destination, out charsWritten);
+                if (underlyingType == typeof(char)) return TryFormatPrimitiveDefault(rt, *(char*)&value, destination, out charsWritten);
                 if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format: default);
+#endif
             }
             else
             {
@@ -1875,12 +1949,14 @@ namespace System
                 if (underlyingType == typeof(sbyte)) return TryFormatPrimitiveNonDefault(rt, *(sbyte*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(short)) return TryFormatPrimitiveNonDefault(rt, *(short*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(ushort)) return TryFormatPrimitiveNonDefault(rt, *(ushort*)&value, destination, out charsWritten, format);
+#if RARE_ENUMS
                 if (underlyingType == typeof(nint)) return TryFormatPrimitiveNonDefault(rt, *(nint*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(nuint)) return TryFormatPrimitiveNonDefault(rt, *(nuint*)&value, destination, out charsWritten, format);
-                if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(float)) return TryFormatPrimitiveNonDefault(rt, *(float*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(double)) return TryFormatPrimitiveNonDefault(rt, *(double*)&value, destination, out charsWritten, format);
+                if (underlyingType == typeof(char)) return TryFormatPrimitiveNonDefault(rt, *(char*)&value, destination, out charsWritten, format);
                 if (underlyingType == typeof(bool)) return TryFormatBool(rt, *(bool*)&value, destination, out charsWritten, format);
+#endif
             }
 
             throw CreateUnknownEnumTypeException();
@@ -2235,10 +2311,10 @@ namespace System
                 case TypeCode.Byte: return ToObject(enumType, (byte)value);
                 case TypeCode.UInt16: return ToObject(enumType, (ushort)value);
                 case TypeCode.UInt64: return ToObject(enumType, (ulong)value);
-                case TypeCode.Char: return ToObject(enumType, (char)value);
-                case TypeCode.Boolean: return ToObject(enumType, (bool)value ? 1L : 0L);
                 case TypeCode.Single: return ToObject(enumType, BitConverter.SingleToInt32Bits((float)value));
                 case TypeCode.Double: return ToObject(enumType, BitConverter.DoubleToInt64Bits((double)value));
+                case TypeCode.Char: return ToObject(enumType, (char)value);
+                case TypeCode.Boolean: return ToObject(enumType, (bool)value ? 1L : 0L);
             };
 
             Type valueType = value.GetType();
index 60e319982fd297c6275a261fd2cef4730d0e8a07..b349b1796d5876d18e45309795e5d8363248b7ce 100644 (file)
@@ -577,14 +577,20 @@ namespace System.Reflection.Emit.Tests
         public static IEnumerable<object[]> NotSupportedObject_Constructor_TestData()
         {
             yield return new object[] { new int[0, 0] };
-            yield return new object[] { Enum.GetValues(CreateEnum(typeof(char), 'a')).GetValue(0) };
-            yield return new object[] { Enum.GetValues(CreateEnum(typeof(bool), true)).GetValue(0) };
+            if (PlatformDetection.IsRareEnumsSupported)
+            {
+                yield return new object[] { Enum.GetValues(CreateEnum(typeof(char), 'a')).GetValue(0) };
+                yield return new object[] { Enum.GetValues(CreateEnum(typeof(bool), true)).GetValue(0) };
+            }
         }
 
         public static IEnumerable<object[]> FloatEnum_DoubleEnum_TestData()
         {
-            yield return new object[] { Enum.GetValues(CreateEnum(typeof(float), 0.0f)).GetValue(0) };
-            yield return new object[] { Enum.GetValues(CreateEnum(typeof(double), 0.0)).GetValue(0) };
+            if (PlatformDetection.IsRareEnumsSupported)
+            {
+                yield return new object[] { Enum.GetValues(CreateEnum(typeof(float), 0.0f)).GetValue(0) };
+                yield return new object[] { Enum.GetValues(CreateEnum(typeof(double), 0.0)).GetValue(0) };
+            }
         }
 
         public static IEnumerable<object[]> NotSupportedObject_Others_TestData()
@@ -639,12 +645,15 @@ namespace System.Reflection.Emit.Tests
         {
             yield return new object[] { typeof(Guid), new Guid() };
             yield return new object[] { typeof(int[,]), new int[5, 5] };
-            yield return new object[] { CreateEnum(typeof(char), 'a'), 'a' };
-            yield return new object[] { CreateEnum(typeof(bool), false), true };
-            yield return new object[] { CreateEnum(typeof(float), 1.0f), 1.0f };
-            yield return new object[] { CreateEnum(typeof(double), 1.0), 1.0 };
-            yield return new object[] { CreateEnum(typeof(IntPtr)), (IntPtr)1 };
-            yield return new object[] { CreateEnum(typeof(UIntPtr)), (UIntPtr)1 };
+            if (PlatformDetection.IsRareEnumsSupported)
+            {
+                yield return new object[] { CreateEnum(typeof(char), 'a'), 'a' };
+                yield return new object[] { CreateEnum(typeof(bool), false), true };
+                yield return new object[] { CreateEnum(typeof(float), 1.0f), 1.0f };
+                yield return new object[] { CreateEnum(typeof(double), 1.0), 1.0 };
+                yield return new object[] { CreateEnum(typeof(IntPtr)), (IntPtr)1 };
+                yield return new object[] { CreateEnum(typeof(UIntPtr)), (UIntPtr)1 };
+            }
         }
 
         [Theory]
index 5c0928a5900d96f02d6461143bbe7555e776a2ab..d87abc5bdbc7c6b8cd1fe7bcd5355194567b02b1 100644 (file)
@@ -77,7 +77,7 @@ namespace System.Tests
             yield return new object[] { ulong.MinValue.ToString(), false, (UInt64Enum)ulong.MinValue };
             yield return new object[] { ulong.MaxValue.ToString(), false, (UInt64Enum)ulong.MaxValue };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // Char
                 yield return new object[] { "Value1", false, Enum.ToObject(s_charEnumType, (char)1) };
@@ -204,7 +204,7 @@ namespace System.Tests
             yield return new object[] { typeof(UInt64Enum), "-1", false, typeof(OverflowException) };
             yield return new object[] { typeof(UInt64Enum), "18446744073709551616", false, typeof(OverflowException) };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // Char
                 yield return new object[] { s_charEnumType, ((char)1).ToString(), false, typeof(ArgumentException) };
@@ -456,7 +456,7 @@ namespace System.Tests
             yield return new object[] { (char)4, null };
         }
 
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         [MemberData(nameof(GetName_CharEnum_TestData))]
         public void GetName_InvokeCharEnum_ReturnsExpected(object value, string expected)
         {
@@ -471,7 +471,7 @@ namespace System.Tests
             yield return new object[] { false, "Value2" };
         }
 
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         [MemberData(nameof(GetName_BoolEnum_TestData))]
         public void GetName_InvokeBoolEnum_ReturnsExpected(object value, string expected)
         {
@@ -712,7 +712,7 @@ namespace System.Tests
             yield return new object[] { (char)99, false };
         }
 
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         [MemberData(nameof(IsDefined_CharEnum_TestData))]
         public void IsDefined_InvokeCharEnum_ReturnsExpected(object value, bool expected)
         {
@@ -729,7 +729,7 @@ namespace System.Tests
             yield return new object[] { false, true };
         }
 
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         [MemberData(nameof(IsDefined_BoolEnum_TestData))]
         public void IsDefined_InvokeBoolEnum_ReturnsExpected(object value, bool expected)
         {
@@ -845,7 +845,7 @@ namespace System.Tests
             yield return new object[] { (UInt64Enum)0x3f06, (UInt64Enum)0x0010, false };
             yield return new object[] { (UInt64Enum)0x3f06, (UInt64Enum)0x3f16, false };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // Char
                 yield return new object[] { Enum.Parse(s_charEnumType, "Value0x3f06"), Enum.Parse(s_charEnumType, "Value0x3000"), true };
@@ -962,7 +962,7 @@ namespace System.Tests
             yield return new object[] { typeof(UInt64Enum), (ulong)77, (UInt64Enum)77 };
             yield return new object[] { typeof(UInt64Enum), (ulong)0x0123456789abcdefL, (UInt64Enum)0x0123456789abcdefL };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // Char
                 yield return new object[] { s_charEnumType, (char)1, Enum.Parse(s_charEnumType, "Value1") };
@@ -995,7 +995,7 @@ namespace System.Tests
             yield return new object[] { typeof(Enum), typeof(ArgumentException) };
             yield return new object[] { typeof(object), typeof(ArgumentException) };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
                 yield return new object[] { GetNonRuntimeEnumTypeBuilder(typeof(int)), typeof(ArgumentException) };
         }
 
@@ -1020,7 +1020,7 @@ namespace System.Tests
             yield return new object[] { typeof(SimpleEnum), null, typeof(ArgumentNullException) };
             yield return new object[] { typeof(SimpleEnum), "Hello", typeof(ArgumentException) };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { s_intPtrEnumType, (IntPtr)1, typeof(ArgumentException) };
                 yield return new object[] { s_uintPtrEnumType, (UIntPtr)1, typeof(ArgumentException) };
@@ -1105,7 +1105,7 @@ namespace System.Tests
             yield return new object[] { UInt64Enum.One, new object(), false };
             yield return new object[] { UInt64Enum.One, null, false };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // Char
                 yield return new object[] { Enum.Parse(s_charEnumType, "Value1"), Enum.Parse(s_charEnumType, "Value1"), true };
@@ -1216,7 +1216,7 @@ namespace System.Tests
             yield return new object[] { UInt64Enum.One, UInt64Enum.Max, -1 };
             yield return new object[] { UInt64Enum.One, null, 1 };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // Char
                 yield return new object[] { Enum.Parse(s_charEnumType, "Value2"), Enum.Parse(s_charEnumType, "Value2"), 0 };
@@ -1281,7 +1281,7 @@ namespace System.Tests
             yield return new object[] { typeof(Int64Enum), typeof(long) };
             yield return new object[] { typeof(UInt64Enum), typeof(ulong) };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { s_charEnumType, typeof(char) };
                 yield return new object[] { s_boolEnumType, typeof(bool) };
@@ -1387,7 +1387,7 @@ namespace System.Tests
             Assert.Equal(expected, Enum.GetNames<UInt64Enum>());
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetNames_InvokeCharEnum_ReturnsExpected()
         {
             var expected = new string[] { "Value0x0000", "Value1", "Value2", "Value0x0010", "Value0x0f06", "Value0x1000", "Value0x3000", "Value0x3f06", "Value0x3f16" };
@@ -1395,7 +1395,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetNames(s_charEnumType), Enum.GetNames(s_charEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetNames_InvokeBoolEnum_ReturnsExpected()
         {
             var expected = new string[] { "Value2", "Value1" };
@@ -1403,7 +1403,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetNames(s_boolEnumType), Enum.GetNames(s_boolEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetNames_InvokeSingleEnum_ReturnsExpected()
         {
             var expected = new string[] { "Value0x0000", "Value1", "Value2", "Value0x0010", "Value0x0f06", "Value0x1000", "Value0x3000", "Value0x3f06", "Value0x3f16" };
@@ -1411,7 +1411,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetNames(s_floatEnumType), Enum.GetNames(s_floatEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetNames_InvokeDoubleEnum_ReturnsExpected()
         {
             var expected = new string[] { "Value0x0000", "Value1", "Value2", "Value0x0010", "Value0x0f06", "Value0x1000", "Value0x3000", "Value0x3f06", "Value0x3f16" };
@@ -1419,7 +1419,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetNames(s_doubleEnumType), Enum.GetNames(s_doubleEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetNames_InvokeIntPtrEnum_ReturnsExpected()
         {
             var expected = new string[0];
@@ -1427,7 +1427,7 @@ namespace System.Tests
             Assert.Same(Enum.GetNames(s_intPtrEnumType), Enum.GetNames(s_intPtrEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetNames_InvokeUIntPtrEnum_ReturnsExpected()
         {
             var expected = new string[0];
@@ -1532,7 +1532,7 @@ namespace System.Tests
             Assert.Equal(expected, Enum.GetValues<UInt64Enum>());
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetValues_InvokeCharEnum_ReturnsExpected()
         {
             var expected = new object[] { Enum.Parse(s_charEnumType, "Value0x0000"), Enum.Parse(s_charEnumType, "Value1"), Enum.Parse(s_charEnumType, "Value2"), Enum.Parse(s_charEnumType, "Value0x0010"), Enum.Parse(s_charEnumType, "Value0x0f06"), Enum.Parse(s_charEnumType, "Value0x1000"), Enum.Parse(s_charEnumType, "Value0x3000"), Enum.Parse(s_charEnumType, "Value0x3f06"), Enum.Parse(s_charEnumType, "Value0x3f16") };
@@ -1540,7 +1540,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetValues(s_charEnumType), Enum.GetValues(s_charEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetValues_InvokeBoolEnum_ReturnsExpected()
         {
             var expected = new object[] { Enum.Parse(s_boolEnumType, "Value2"), Enum.Parse(s_boolEnumType, "Value1") };
@@ -1548,7 +1548,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetValues(s_boolEnumType), Enum.GetValues(s_boolEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetValues_InvokeSingleEnum_ReturnsExpected()
         {
             var expected = new object[] { Enum.Parse(s_floatEnumType, "Value0x0000"), Enum.Parse(s_floatEnumType, "Value1"), Enum.Parse(s_floatEnumType, "Value2"), Enum.Parse(s_floatEnumType, "Value0x0010"), Enum.Parse(s_floatEnumType, "Value0x0f06"), Enum.Parse(s_floatEnumType, "Value0x1000"), Enum.Parse(s_floatEnumType, "Value0x3000"), Enum.Parse(s_floatEnumType, "Value0x3f06"), Enum.Parse(s_floatEnumType, "Value0x3f16") };
@@ -1556,7 +1556,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetValues(s_floatEnumType), Enum.GetValues(s_floatEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetValues_InvokeDoubleEnum_ReturnsExpected()
         {
             var expected = new object[] { Enum.Parse(s_doubleEnumType, "Value0x0000"), Enum.Parse(s_doubleEnumType, "Value1"), Enum.Parse(s_doubleEnumType, "Value2"), Enum.Parse(s_doubleEnumType, "Value0x0010"), Enum.Parse(s_doubleEnumType, "Value0x0f06"), Enum.Parse(s_doubleEnumType, "Value0x1000"), Enum.Parse(s_doubleEnumType, "Value0x3000"), Enum.Parse(s_doubleEnumType, "Value0x3f06"), Enum.Parse(s_doubleEnumType, "Value0x3f16") };
@@ -1564,7 +1564,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetValues(s_doubleEnumType), Enum.GetValues(s_doubleEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetValues_InvokeIntPtrEnum_ReturnsExpected()
         {
             var expected = new object[0];
@@ -1572,7 +1572,7 @@ namespace System.Tests
             Assert.NotSame(Enum.GetValues(s_intPtrEnumType), Enum.GetValues(s_intPtrEnumType));
         }
 
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         public void GetValues_InvokeUIntPtrEnum_ReturnsExpected()
         {
             var expected = new object[0];
@@ -1751,7 +1751,7 @@ namespace System.Tests
             yield return new object[] { (UInt64Enum)99, "D", "99" };
             yield return new object[] { UInt64Enum.Max, "D", "18446744073709551615" };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // "D": Char
                 yield return new object[] { Enum.ToObject(s_charEnumType, (char)0), "D", ((char)0).ToString() };
@@ -1834,7 +1834,7 @@ namespace System.Tests
             yield return new object[] { (UInt64Enum)99, "X", "0000000000000063" };
             yield return new object[] { UInt64Enum.Max, "X", "FFFFFFFFFFFFFFFF" };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // "X": Char
                 yield return new object[] { Enum.ToObject(s_charEnumType, (char)0), "X", "0000" };
@@ -1900,7 +1900,7 @@ namespace System.Tests
             yield return new object[] { (UInt64Enum)5, "F", "5" };
             yield return new object[] { UInt64Enum.Max, "F", "Max" };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // "F": Char
                 yield return new object[] { Enum.ToObject(s_charEnumType, (char)1), "F", "Value1" };
@@ -1928,7 +1928,7 @@ namespace System.Tests
             // "F": Flags Attribute
             yield return new object[] { AttributeTargets.Class | AttributeTargets.Delegate, "F", "Class, Delegate" };
 
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 // "G": Char
                 yield return new object[] { Enum.ToObject(s_charEnumType, char.MaxValue), "G", char.MaxValue.ToString() };
@@ -2289,7 +2289,7 @@ namespace System.Tests
             yield return new object[] { typeof(AttributeTargets), (int)(AttributeTargets.Class | AttributeTargets.Delegate), "G", "Class, Delegate" };
 
             // nint/nuint types
-            if (PlatformDetection.IsReflectionEmitSupported)
+            if (PlatformDetection.IsReflectionEmitSupported && PlatformDetection.IsRareEnumsSupported)
             {
                 yield return new object[] { s_intPtrEnumType, (nint)1, "G", "1" };
                 yield return new object[] { s_uintPtrEnumType, (nuint)2, "F", "2" };
@@ -2416,7 +2416,7 @@ namespace System.Tests
             yield return new object[] { Enum.ToObject(s_doubleEnumType, 2) };
         }
 
-        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
+        [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported), nameof(PlatformDetection.IsRareEnumsSupported))]
         [MemberData(nameof(UnsupportedEnum_TestData))]
         public static void ToString_UnsupportedEnumType_ThrowsArgumentException(Enum e)
         {
@@ -2427,7 +2427,7 @@ namespace System.Tests
 
         private static EnumBuilder GetNonRuntimeEnumTypeBuilder(Type underlyingType)
         {
-            if (!PlatformDetection.IsReflectionEmitSupported)
+            if (!PlatformDetection.IsReflectionEmitSupported || !PlatformDetection.IsRareEnumsSupported)
                 return null;
 
             AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Name"), AssemblyBuilderAccess.Run);