blob: 4ab3a66310b6c3422b635c0cfd52d1281ca02620 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System.ComponentModel;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace Enmarcha.Backend.Helpers;
public static class EnumExtensions
{
public static string GetDescription(this Enum value)
{
var field = value.GetType().GetField(value.ToString());
var attr = field?.GetCustomAttribute<DescriptionAttribute>();
return attr?.Description ?? value.ToString();
}
public static IEnumerable<SelectListItem> ToSelectList<TEnum>() where TEnum : struct, Enum =>
Enum.GetValues<TEnum>().Select(e => new SelectListItem
{
Value = e.ToString(),
Text = e.GetDescription()
});
}
|