12345678910111213141516171819202122232425262728293031323334 |
- using System;
- using System.ComponentModel;
- using System.Reflection;
- namespace WCS_Client.Extensions
- {
- public static class EnumExtensions
- {
- /// <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;
- }
- }
- }
|