Created
July 28, 2010 15:14
-
-
Save johnnonolan/494836 to your computer and use it in GitHub Desktop.
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
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