10 public static class Enum<T> where T : struct, IComparable, IFormattable, IConvertible
13 private static readonly Lazy<ReadOnlyCollection<T>> CachedValues =
new Lazy<ReadOnlyCollection<T>>(() =>
14 new ReadOnlyCollection<T>(
Enum.GetValues(typeof(T)).Cast<T>().ToList()));
17 public static IEnumerable<T>
Values => CachedValues.Value;
20 private static readonly Lazy<ReadOnlyCollection<string>> CachedNames =
new Lazy<ReadOnlyCollection<string>>(() =>
21 new ReadOnlyCollection<string>(
Enum.GetNames(typeof(T)).ToList()));
24 public static IEnumerable<string>
Names => CachedNames.Value;
27 private static readonly Lazy<Dictionary<string, T>> CachedParsingDict =
new Lazy<Dictionary<string, T>>(() =>
28 Names.ToDictionary(x => x, x => (T)
Enum.
Parse(typeof(T), x), StringComparer.OrdinalIgnoreCase));
32 public static T
Parse(
string value) => CachedParsingDict.Value[value];
36 public static bool TryParse(
string value, out T result) => CachedParsingDict.Value.TryGetValue(value, out result);
39 private static readonly Lazy<Dictionary<T, string>> CachedGetNameDict =
new Lazy<Dictionary<T, string>>(() =>
45 public static string GetName(T value) => CachedGetNameDict.Value[value];