Skip to content

Instantly share code, notes, and snippets.

@werwolfby
Last active August 22, 2018 12:03
Show Gist options
  • Save werwolfby/d25f7140ac5eb834b014d0a2baf5d5e4 to your computer and use it in GitHub Desktop.
Save werwolfby/d25f7140ac5eb834b014d0a2baf5d5e4 to your computer and use it in GitHub Desktop.
Helper method to convert to any Enum from int
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