String - ToEnum
A string extension method that converts the string object to an enum.
public static void Main() { string animal = @"Cat"; // C# Extension Method: String - ToEnum var enumValue = animal.ToEnum<AnimalType>(); Console.WriteLine(enumValue); } } public enum AnimalType { None, Cat = 1, Dog = 2
View Source
using System; public static partial class Extensions { /// <summary> /// A string extension method that converts the @this to an enum. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <returns>@this as a T.</returns> public static T ToEnum<T>(this string @this) { Type enumType = typeof (T); return (T) Enum.Parse(enumType, @this); } }