Created
June 29, 2015 18:32
-
-
Save iSynaptic/2bcc8dbe517036604d77 to your computer and use it in GitHub Desktop.
Enum validation
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
void Main() | |
{ | |
EnumValidator.IsDefined<MyEnum>("4"); | |
} | |
public static class EnumValidator | |
{ | |
public static bool IsDefined<T>(string value) | |
{ | |
if(!typeof(T).IsEnum) throw new ArgumentException("Type argument must be an enum type.", "T"); | |
T v = (T)Enum.Parse(typeof(T), value); | |
return Enum.IsDefined(typeof(T), v); | |
} | |
} | |
public enum MyEnum | |
{ | |
Foo, | |
Bar = 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment