Files
Generic.CL/Services/EnumHelper.cs
T

27 lines
789 B
C#

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace Generic.Services;
public static class EnumExtensions
{
public static string GetDisplayName(this Enum value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
// Get the enum field name
string name = value.ToString();
// Get the field info for the enum value
FieldInfo? field = value.GetType().GetField(name);
if (field == null)
return name; // Not a defined enum value (e.g., flags combination)
// Check for the [Display] attribute
DisplayAttribute? displayAttribute = field.GetCustomAttribute<DisplayAttribute>();
return displayAttribute?.GetName() ?? name;
}
}