2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.DTOs;
|
|
|
|
|
|
|
|
public static class EnumExtensions
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
public static string GetDescription<T>(this T enumerationValue)
|
|
|
|
where T : Enum
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
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()!;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return attrs.Length > 0 ? ((DescriptionAttribute) attrs[0]).Description : enumerationValue.ToString();
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
}
|