Skip to content

Instantly share code, notes, and snippets.

@Guzzter
Created December 8, 2015 14:07
Show Gist options
  • Save Guzzter/8b436de9bfbb3ce78ebb to your computer and use it in GitHub Desktop.
Save Guzzter/8b436de9bfbb3ce78ebb to your computer and use it in GitHub Desktop.
Enum helper class to convert a string to enum
public static class EnumHelper<T>
{
public static T Parse(string value)
{
return EnumHelper<T>.Parse(value, true);
}
public static T Parse(string value, bool ignoreCase)
{
return (T)Enum.Parse(typeof(T), value, ignoreCase);
}
public static bool TryParse(string value, out T returnedValue)
{
return EnumHelper<T>.TryParse(value, true, out returnedValue);
}
public static bool TryParse(string value, bool ignoreCase, out T returnedValue)
{
try
{
returnedValue = (T)Enum.Parse(typeof(T), value, ignoreCase);
return true;
}
catch
{
returnedValue = default(T);
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment