700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 在C#中读取枚举值的描述属性

在C#中读取枚举值的描述属性

时间:2023-07-08 02:22:50

相关推荐

在C#中读取枚举值的描述属性

枚举:public enum EnumLanugage{[ponentModel.Description("中文")]Chinese,English}获取值描述的方法:public string GetEnumDescription(Enum enumValue){string str = enumValue.ToString();System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);object[] objs = field.GetCustomAttributes(typeof(ponentModel.DescriptionAttribute), false);if (objs == null || objs.Length == 0) return str;ponentModel.DescriptionAttribute da = (ponentModel.DescriptionAttribute)objs[0];return da.Description;}调用 GetEnumDescription(EnumLanguage.Chinese) 后 将返回“中文”

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。