wabbajack/Wabbajack.DTOs/Extensions/EnumExtensions.cs

24 lines
846 B
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.ComponentModel;
namespace Wabbajack.DTOs
{
public static class EnumExtensions
{
public static string GetDescription<T>(this T enumerationValue)
where T : Enum
{
var type = enumerationValue.GetType();
if (!type.IsEnum)
throw new ArgumentException($"{nameof(enumerationValue)} must be of Enum type",
nameof(enumerationValue));
var memberInfo = type.GetMember(enumerationValue.ToString()!);
if (memberInfo.Length <= 0)
return enumerationValue.ToString()!;
var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
return attrs.Length > 0 ? ((DescriptionAttribute)attrs[0]).Description : enumerationValue.ToString();
}
}
}