Skip to content

Instantly share code, notes, and snippets.

@iSynaptic
Created June 29, 2015 18:32
Show Gist options
  • Save iSynaptic/2bcc8dbe517036604d77 to your computer and use it in GitHub Desktop.
Save iSynaptic/2bcc8dbe517036604d77 to your computer and use it in GitHub Desktop.
Enum validation
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