This commit is contained in:
2022-04-12 17:21:50 +08:00
parent 005bbf8419
commit 0bf0299c31
6 changed files with 115 additions and 72 deletions
@@ -30,8 +30,35 @@ namespace IRaCIS.Core.Infrastructure.Extention
}
return dict;
}
/// <summary>
/// 扩展方法,获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true)
{
return name;
}
return attribute?.Description;
}
}
}