Last active
August 22, 2018 12:03
-
-
Save werwolfby/d25f7140ac5eb834b014d0a2baf5d5e4 to your computer and use it in GitHub Desktop.
Helper method to convert to any Enum from int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EnumConverter<TEnum> | |
where TEnum : Enum | |
{ | |
private static readonly Dictionary<int, TEnum> _values; | |
static EnumConverter() | |
{ | |
_values = new Dictionary<int, TEnum>(); | |
var enumValues = Enum.GetValues(typeof(TEnum)); | |
foreach (var item in enumValues) | |
{ | |
_values.Add(Convert.ToInt32(item), (TEnum)item); | |
} | |
} | |
public static TEnum Get(int value) => _values[value]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment