diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Augments/ReflectionAugments.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Augments/ReflectionAugments.cs index 48e00e07c6674..c16bcda048d2c 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Augments/ReflectionAugments.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Augments/ReflectionAugments.cs @@ -165,8 +165,7 @@ public abstract object ActivatorCreateInstance( public abstract Assembly[] GetLoadedAssemblies(); - public abstract EnumInfo GetEnumInfo(Type type) - where TUnderlyingValue : struct, INumber; + public abstract EnumInfo GetEnumInfo(Type type, Func create); public abstract DynamicInvokeInfo GetDelegateDynamicInvokeInfo(Type type); } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs index 6bdb4725be726..4f197b12e0684 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Enum.NativeAot.cs @@ -47,7 +47,15 @@ internal static EnumInfo GetEnumInfo(Type en Debug.Assert(enumType is RuntimeType); Debug.Assert(enumType.IsEnum); - return ReflectionAugments.ReflectionCoreCallbacks.GetEnumInfo(enumType); + return (EnumInfo)ReflectionAugments.ReflectionCoreCallbacks.GetEnumInfo(enumType, + static (underlyingType, names, valuesAsObject, isFlags) => + { + // Only after we've sorted, create the underlying array. + var values = new TUnderlyingValue[valuesAsObject.Length]; + for (int i = 0; i < valuesAsObject.Length; i++) + values[i] = (TUnderlyingValue)valuesAsObject[i]; + return new EnumInfo(underlyingType, values, names, isFlags); + }); } #pragma warning restore diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs index 996f77b66b38b..5c7cb3dc89c75 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ReflectionCoreCallbacksImplementation.cs @@ -404,11 +404,11 @@ public sealed override void MakeTypedReference(object target, FieldInfo[] flds, public sealed override Assembly[] GetLoadedAssemblies() => RuntimeAssemblyInfo.GetLoadedAssemblies(); - public sealed override EnumInfo GetEnumInfo(Type type) + public sealed override EnumInfo GetEnumInfo(Type type, Func create) { RuntimeTypeInfo runtimeType = type.CastToRuntimeTypeInfo(); - EnumInfo? info = runtimeType.GenericCache as EnumInfo; + var info = runtimeType.GenericCache as EnumInfo; if (info != null) return info; @@ -418,12 +418,8 @@ public sealed override EnumInfo GetEnumInfo( // That codepath would bring functionality to compare everything that was ever allocated in the program. ArraySortHelper.IntrospectiveSort(unsortedValues, unsortedNames, EnumUnderlyingTypeComparer.Instance); - // Only after we've sorted, create the underlying array. - var values = new TUnderlyingValue[unsortedValues.Length]; - for (int i = 0; i < unsortedValues.Length; i++) - values[i] = (TUnderlyingValue)unsortedValues[i]; + info = create(RuntimeAugments.GetEnumUnderlyingType(type.TypeHandle), unsortedNames, unsortedValues, isFlags); - info = new EnumInfo(RuntimeAugments.GetEnumUnderlyingType(runtimeType.TypeHandle), values, unsortedNames, isFlags); runtimeType.GenericCache = info; return info; } diff --git a/src/coreclr/nativeaot/System.Private.DisabledReflection/src/Internal/Reflection/ReflectionCoreCallbacksImplementation.cs b/src/coreclr/nativeaot/System.Private.DisabledReflection/src/Internal/Reflection/ReflectionCoreCallbacksImplementation.cs index 74436dccf1f0a..143c65f40037f 100644 --- a/src/coreclr/nativeaot/System.Private.DisabledReflection/src/Internal/Reflection/ReflectionCoreCallbacksImplementation.cs +++ b/src/coreclr/nativeaot/System.Private.DisabledReflection/src/Internal/Reflection/ReflectionCoreCallbacksImplementation.cs @@ -12,12 +12,12 @@ namespace Internal.Reflection { internal class ReflectionCoreCallbacksImplementation : ReflectionCoreCallbacks { - public override EnumInfo GetEnumInfo(Type type) => - new EnumInfo( + public override EnumInfo GetEnumInfo(Type type, Func create) => + create( RuntimeAugments.GetEnumUnderlyingType(type.TypeHandle), - values: Array.Empty(), - names: Array.Empty(), - isFlags: false); + Array.Empty(), + Array.Empty(), + false); public override DynamicInvokeInfo GetDelegateDynamicInvokeInfo(Type type) => throw new NotSupportedException(SR.Reflection_Disabled);