| | | 1 | | /******************************************************************************** |
| | | 2 | | * EnumExtensions.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Frozen; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | |
| | | 11 | | namespace NanoRoute.Internals |
| | | 12 | | { |
| | | 13 | | internal static class EnumExtensions |
| | | 14 | | { |
| | | 15 | | private static class Internal<TEnum> where TEnum : Enum |
| | | 16 | | { |
| | 2 | 17 | | public static readonly IReadOnlyCollection<string> Names = Enum.GetNames(typeof(TEnum)); |
| | | 18 | | |
| | 2 | 19 | | public static readonly FrozenDictionary<string, TEnum> EnumMapper = |
| | 2 | 20 | | //Enum.GetValues(typeof(TEnum)) cannot be used here as it is not AOT compatible |
| | 2 | 21 | | Names |
| | 2 | 22 | | .ToDictionary(static name => name, static name => (TEnum) Enum.Parse(typeof(TEnum), name)) |
| | 2 | 23 | | .ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | extension<TEnum>(TEnum) where TEnum : Enum |
| | | 27 | | { |
| | 1 | 28 | | public static IReadOnlyCollection<string> Names => Internal<TEnum>.Names; |
| | | 29 | | |
| | 2 | 30 | | public static bool TryParseFast(string s, out TEnum val) => Internal<TEnum>.EnumMapper.TryGetValue(s, out va |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | } |