枚举的应用 |
您所在的位置:网站首页 › java枚举使用场景 › 枚举的应用 |
using System; using System.ComponentModel; namespace TestEnum { //基础枚举 public enum Name { [Description("鲍勃")] boob, [Description("马宇")] JackMa, [Description("陈龙")] JackChan, [Description("丽莎")] Lisa, [Description("保罗")] Poro } //主要这个GetDescription()方法,看不懂没关系,如何调用 static class EnumExtensions { public static string GetDescription(this Enum val) { var field = val.GetType().GetField(val.ToString()); var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description; } } } string str = Name.boob.GetDescription(); |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |