Files
adas-core/adas-core.Domain/Utils/EnumUtils.cs
2026-06-26 10:29:23 +02:00

29 lines
1.3 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>
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>
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;
}
}