mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
24 lines
846 B
C#
24 lines
846 B
C#
|
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();
|
||
|
}
|
||
|
}
|
||
|
}
|