Skip to content

Instantly share code, notes, and snippets.

@johnnonolan
Created July 28, 2010 15:14
Show Gist options
  • Save johnnonolan/494836 to your computer and use it in GitHub Desktop.
Save johnnonolan/494836 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumTest
{
public enum MyEnum
{
MySearch,
YourSearch,
SomeOtherSearch
};
public static class ClassExts
{
public static MyEnum Parse(this String s)
{
return (MyEnum)Enum.Parse(typeof(MyEnum), s);
}
public static T GetEnumValue<T>(this String value) where T : struct
{
if (!typeof(T).IsEnum) throw new Exception(typeof(T).FullName);
return (T)Enum.Parse(typeof(MyEnum), value);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MyEnum.MySearch.ToString());
Console.WriteLine( "MySearch".Parse());
Console.WriteLine("MySearch".GetEnumValue<MyEnum>());
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment