You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
5.1 KiB
125 lines
5.1 KiB
using System.ComponentModel;
|
|
using System.Reflection;
|
|
|
|
namespace Common.Helpers;
|
|
|
|
public static class EnumHelpers
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <typeparam name="T">enumType of the enum</typeparam>
|
|
/// <param name="value">value to parse to enum</param>
|
|
/// <returns>returns enum object with value</returns>
|
|
public static T ParseEnum<T>(string value)
|
|
{
|
|
return (T)Enum.Parse(typeof(T), value, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an instance of the given <paramref name="enumType"/> if it is an enum type.
|
|
/// Throws an exception if it is not an enum type.
|
|
/// </summary>
|
|
/// <param name="enumType">type of target enum</param>
|
|
/// <returns>instance of enum tye with default value</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the searchedDescription of an Enum value.
|
|
/// If there is no Description set, the Enum Value will be converted to string.
|
|
/// </summary>
|
|
/// <param name="enumObject">Enum Object to get Description of.</param>
|
|
/// <returns>String with Content of DescriptionAttribute of Enum object.</returns>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all Description Attributes of a Enum enumType.
|
|
/// Descriptions must be unique!
|
|
/// </summary>
|
|
/// <param name="enumObject">An object of the Enum. Will be searched for other Descriptions</param>
|
|
/// <returns>Key Value Pair of Description and respective Enum Value</returns>
|
|
public static Dictionary<string, Enum> GetAllEnumDescriptions(Enum enumObject)
|
|
{
|
|
// guard argument null
|
|
if (enumObject == null) { throw new ArgumentNullException(nameof(enumObject)); }
|
|
|
|
Dictionary<string, Enum> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Searches and returns the Enum Value of which the <see cref="DescriptionAttribute"/> equals the <paramref name="searchedDescription"/>.
|
|
/// Descriptions of enumType of <paramref name="enumObj"/> must be uniqe!
|
|
/// </summary>
|
|
/// <param name="enumObj">any instance of the enum enumType</param>
|
|
/// <param name="searchedDescription">description that is compared to the <see cref="DescriptionAttribute"/></param>
|
|
/// <returns>enum object of enumType of <paramref name="enumObj"/> or null if no enum with matching description found</returns>
|
|
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}.");
|
|
}
|
|
}
|
|
|