using System.ComponentModel; using System.Reflection; namespace Common.Helpers; public static class EnumHelpers { /// /// Parse enum from string to enum enumType. /// String must be exactly the same as generated by the default Enum.ToString() /// method and cannot be a searchedDescription of the Enum value. /// /// enumType of the enum /// value to parse to enum /// returns enum object with value public static T ParseEnum(string value) { return (T)Enum.Parse(typeof(T), value, true); } /// /// Creates an instance of the given if it is an enum type. /// Throws an exception if it is not an enum type. /// /// type of target enum /// instance of enum tye with default value public static Enum CreateInstanceOfEnumType(Type enumType) { // generate instance of enum using target enumType if (Activator.CreateInstance(enumType) is not Enum enumObj) { throw new Exception($"'{nameof(CreateInstanceOfEnumType)}()' could not instanciate Enum of type {enumType}."); } return enumObj; } /// /// Gets the searchedDescription of an Enum value. /// If there is no Description set, the Enum Value will be converted to string. /// /// Enum Object to get Description of. /// String with Content of DescriptionAttribute of Enum object. public static string GetEnumDescription(Enum enumObject) { // guard argument null if (enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); } // get field info from enum enumType FieldInfo? fieldInfo = enumObject.GetType().GetField(enumObject.ToString()); // return string of enum value if there is no field info if (fieldInfo == null) { return enumObject.ToString(); } // get searchedDescription attribute and return if it is present DescriptionAttribute? descAttrib = (DescriptionAttribute?)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), true); if (descAttrib != null) { return descAttrib.Description; } // if no searchedDescription attribute was found => return string of enum value return enumObject.ToString(); } /// /// Gets all Description Attributes of a Enum enumType. /// Descriptions must be unique! /// /// An object of the Enum. Will be searched for other Descriptions /// Key Value Pair of Description and respective Enum Value public static Dictionary GetAllEnumDescriptions(Enum enumObject) { // guard argument null if (enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); } Dictionary descriptionsToEnumValues = new(); // get members of enum enumType var members = enumObject.GetType().GetMembers(); foreach (var member in members) { // get searchedDescription attributes of all members DescriptionAttribute? descAttrib = (DescriptionAttribute?)member.GetCustomAttribute(typeof(DescriptionAttribute), true); if (descAttrib != null) { // if a searchedDescription exists, add the searchedDescription and the enum value to the dictionary descriptionsToEnumValues.Add(descAttrib.Description, (Enum)Enum.Parse(enumObject.GetType(), member.Name)); } } return descriptionsToEnumValues; } /// /// Searches and returns the Enum Value of which the equals the . /// Descriptions of enumType of must be uniqe! /// /// any instance of the enum enumType /// description that is compared to the /// enum object of enumType of or null if no enum with matching description found public static Enum? GetEnumValueByDescription(Enum enumObj, string searchedDescription) { // guard null if(enumObj == null) { return null; } // get all descriptions of the enum var descriptions = GetAllEnumDescriptions(enumObj); // return if equal foreach (var desc in descriptions) { if (desc.Key.Equals(searchedDescription)) { return desc.Value; } } // nothing found return null; //throw new Exception($"{nameof(GetEnumValueByDescription)} could not find an enum value in enumType {typeof(enumObj)} that has the Description {searchedDescription}."); } }