/* EnumHelper.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ namespace HandBrake.ApplicationServices.Functions { using System; using System.ComponentModel; using System.Reflection; /// /// Enum Helpers /// /// /// The Type Parameter /// public class EnumHelper { /// /// Get the description of an Enum /// /// /// The value. /// /// /// The Description string /// public static string GetDescription(T value) { FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes( typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : value.ToString(); } /// /// Get the Enumeration for a given Enum Description /// /// The String description /// The Enum Value public static T GetValue(string description) { foreach (T val in Enum.GetValues(typeof(T))) { string currDescription = GetDescription(val); if (currDescription == description) { return val; } } throw new ArgumentOutOfRangeException("The Description for the enum was not recognized."); } } }