Files
adas-core/adas-core.Domain/Utils/EnumUtils.cs
T

31 lines
1.4 KiB
C#

using System.ComponentModel;
using System.Reflection;
namespace adas_core.Domain.Utils;
/// <summary>
/// Provides static utility methods for working with <see cref="System.Enum"/> types.
/// </summary>
/// <!-- aidoc:v1 sig=429f818 -->
public static class EnumUtils
{
/// <summary>
/// Retrieves the human-readable description for an enumeration value by inspecting its <see cref="DescriptionAttribute"/>.
/// Falls back to the enum's string representation when no description attribute is defined on the member.
/// </summary>
/// <param name="value">The enumeration value whose description should be obtained.</param>
/// <returns>The description text from the <see cref="DescriptionAttribute"/> if present; otherwise, the string representation of the enum value.</returns>
/// <!-- aidoc:v1 sig=48c6985 body=d45f10c -->
public static string GetDescription(Enum value)
{
var enumMember = value.GetType().GetMember(value.ToString()).FirstOrDefault();
var descriptionAttribute =
enumMember == null
? null
: enumMember.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
return
descriptionAttribute == null
? value.ToString()
: descriptionAttribute.Description;
}
}