Created
December 8, 2015 14:07
-
-
Save Guzzter/8b436de9bfbb3ce78ebb to your computer and use it in GitHub Desktop.
Enum helper class to convert a string to enum
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 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