/// <summary> /// Converts the specified string value to its corresponding enumeration. /// </summary> /// <typeparam name="T">The enum type.</typeparam> /// <param name="value">The enum value.</param> /// <param name="ignoreCase">A flag indicating whether or not to ignore the case of the enum value.</param> /// <returns>An enumeration of the specified type.</returns> public static T Parse <T>(string value, bool ignoreCase) { ParameterValidator.AssertIsEnum <T>(); ParameterValidator.AssertIsNotNullOrWhiteSpace("value", value); return((T)Enum.Parse(typeof(T), value, ignoreCase)); }
/// <summary> /// Scans Description values of an enum, returning matched elements /// </summary> /// <typeparam name="T">The enum type.</typeparam> /// <param name="description">The enum description.</param> /// <returns>An enumeration of the specified type.</returns> public static IEnumerable <T> GetForDescription <T>(string description) { ParameterValidator.AssertIsEnum <T>(); ParameterValidator.AssertIsNotNullOrWhiteSpace("description", description); Type enumType = typeof(T); Collection <T> enums = new Collection <T>(); IEnumerable <FieldInfo> fieldInfos = enumType.GetFields().Where(o => o.FieldType == enumType); foreach (FieldInfo fieldInfo in fieldInfos) { DescriptionAttribute attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() as DescriptionAttribute; if (attribute != null && attribute.Description == description) { T enumFieldValue = (T)fieldInfo.GetValue(default(T)); enums.Add(enumFieldValue); } } return(enums); }