C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
Enum.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4using System.Linq;
5
7{
10 public static class Enum<T> where T : struct, IComparable, IFormattable, IConvertible
11 {
13 private static readonly Lazy<ReadOnlyCollection<T>> CachedValues = new Lazy<ReadOnlyCollection<T>>(() =>
14 new ReadOnlyCollection<T>(Enum.GetValues(typeof(T)).Cast<T>().ToList()));
15
17 public static IEnumerable<T> Values => CachedValues.Value;
18
20 private static readonly Lazy<ReadOnlyCollection<string>> CachedNames = new Lazy<ReadOnlyCollection<string>>(() =>
21 new ReadOnlyCollection<string>(Enum.GetNames(typeof(T)).ToList()));
22
24 public static IEnumerable<string> Names => CachedNames.Value;
25
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));
29
32 public static T Parse(string value) => CachedParsingDict.Value[value];
33
36 public static bool TryParse(string value, out T result) => CachedParsingDict.Value.TryGetValue(value, out result);
37
39 private static readonly Lazy<Dictionary<T, string>> CachedGetNameDict = new Lazy<Dictionary<T, string>>(() =>
40 Values.ToDictionary(v => v, v => Enum.GetName(typeof(T), v)));
41
45 public static string GetName(T value) => CachedGetNameDict.Value[value];
46 }
47}
Helper class for extending the functionality of the static Enumclass with compile-time-type-specific ...
Definition Enum.cs:11
static IEnumerable< string > Names
Get all names in an enumeration.
Definition Enum.cs:24
static string GetName(T value)
Convert the enumeration value to a string.
static IEnumerable< T > Values
Get all values in an enumeration.
Definition Enum.cs:17
static T Parse(string value)
Convert the string to an enumeration value.
static bool TryParse(string value, out T result)
Try to convert the string to an enumeration value.