标签:lan div his tar target ret bsp var 枚举
其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html
/// <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; }
随便整一个枚举
public enum ModuleType { /// <summary> /// 中国 /// </summary> [Description("中国")] ZH = 1, /// <summary> /// 美国 /// </summary> [Description("美国")] MG = 2 }
举例 :var AppId = ModuleType.ZH.GetDescription() // 得到“中国”
标签:lan div his tar target ret bsp var 枚举
原文地址:https://www.cnblogs.com/zhuanjiao/p/12082898.html