C# 给枚举类型增加一个备注特性
生活随笔
收集整理的這篇文章主要介紹了
C# 给枚举类型增加一个备注特性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/// <summary>/// 備注特性/// </summary>public class RemarkAttribute : Attribute{/// <summary>/// 備注/// </summary>public string Remark { get; set; }public RemarkAttribute(string remark){this.Remark = remark;}} /// <summary>/// 枚舉擴展類/// </summary>public static class EnumExtension{/// <summary>/// 獲取枚舉的備注信息/// </summary>/// <param name="em"></param>/// <returns></returns>public static string GetRemark(this Enum value){FieldInfo fi = value.GetType().GetField(value.ToString());if (fi == null){return value.ToString();}object[] attributes = fi.GetCustomAttributes(typeof(RemarkAttribute), false);if (attributes.Length > 0){return ((RemarkAttribute)attributes[0]).Remark;}else{return value.ToString();}}public static string GetEnumDescription(this Enum value){FieldInfo fi = value.GetType().GetField(value.ToString());DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);if (attributes.Length > 0){return attributes[0].Description;}else{return value.ToString();}}} var aaa = UserType.Type1.GetRemark();var aab = UserType.Type2.GetEnumDescription();
?
轉載于:https://www.cnblogs.com/hantianwei/p/3191055.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C# 给枚举类型增加一个备注特性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: love2d杂记9--光照效果
- 下一篇: Testin云测试:QQ(4.2.0)安