@ -6,9 +6,11 @@ namespace Common.Helpers;
public static class EnumHelpers
{
/// <summary>
/// Parse enum from string to enum type.
/// 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">type of the enum</typeparam>
/// <typeparam name="T">enumT ype 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 )
@ -17,7 +19,24 @@ public static class EnumHelpers
}
/// <summary>
/// Gets the description of an Enum value.
/// 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>
@ -27,7 +46,7 @@ public static class EnumHelpers
// guard argument null
if ( enumObject = = null ) { throw new ArgumentNullException ( nameof ( enumObject ) ) ; }
// get field info from enum t ype
// get field info from enum enumT ype
FieldInfo ? fieldInfo = enumObject . GetType ( ) . GetField ( enumObject . ToString ( ) ) ;
// return string of enum value if there is no field info
if ( fieldInfo = = null )
@ -35,14 +54,72 @@ public static class EnumHelpers
return enumObject . ToString ( ) ;
}
// get description attribute and return if it is present
// get searche dD escription attribute and return if it is present
DescriptionAttribute ? descAttrib = ( DescriptionAttribute ? ) fieldInfo . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
if ( descAttrib ! = null )
{
return descAttrib . Description ;
}
// if no description attribute was found => return string of enum value
// if no searche dD escription 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}.");
}
}