C# 添加枚举中文资源
在业务开发过程中,添加枚举,在固定枚举值的同时,也需要中文的文案。
如果不想添加语言资源项、添加枚举转语资源项,可以使用特性标记。
属性描述 DescriptionAttribute
先看案例:
1 public enum WheelchairDataType 2 { 3 [Description("前进加速")] 4 ForwardAdd, 5 [Description("前进减速")] 6 ForwardReduce, 7 [Description("后退加速")] 8 BackwardAdd, 9 [Description("后退减速")] 10 BackwardReduce, 11 [Description("转弯加速")] 12 TurningAdd, 13 [Description("转弯减速")] 14 TurningReduce 15 }
1 static void Main(string[] args) 2 { 3 var enumDescriptionDict = GetEnumDescriptionDict(WheelchairDataType.BackwardAdd.GetType()); 4 var enumDescription = enumDescriptionDict[WheelchairDataType.BackwardAdd.ToString()]; 5 Console.WriteLine($"{ WheelchairDataType.BackwardAdd.ToString()}:{enumDescription}"); 6 Console.ReadLine(); 7 }
以上,能够直接获取到枚举的描述值。所以我们可以用Description标记,取代我们经常要对枚举添加的中文注释,既是注释也是一种简便的语言项资源。
DescriptionAttribute继承自Attribute,所以枚举的中文标记值,可以通过反射获取:
1 /// <summary> 2 /// 获取枚举/中文字典 3 /// </summary> 4 /// <param name="enumType"></param> 5 /// <returns></returns> 6 public static Dictionary<string, string> GetEnumDescriptionDict(Type enumType) 7 { 8 Dictionary<string, string> dict = new Dictionary<string, string>(); 9 FieldInfo[] fields = enumType.GetFields(); 10 foreach (FieldInfo field in fields) 11 { 12 if (field.FieldType.IsEnum) 13 { 14 var customAttributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false).ToList(); 15 dict.Add(field.Name, ((DescriptionAttribute)customAttributes[0]).Description); 16 } 17 } 18 19 return dict; 20 }
自定义枚举的描述标记EnumDetailAttribute
如果不想用DescriptionAttribute,或者需要额外的描述信息,可以自定义一个标记。比如:
1 public class EnumDetailAttribute : Attribute 2 { 3 public string Name { get; set; } 4 5 public int SpecialOrder { get; set; } 6 }
1 public enum WheelchairDataType 2 { 3 [EnumDetail(Name = "前进加速")] 4 ForwardAdd, 5 [EnumDetail(Name = "前进减速")] 6 ForwardReduce, 7 [EnumDetail(Name = "后退加速")] 8 BackwardAdd, 9 [EnumDetail(Name = "后退减速")] 10 BackwardReduce, 11 [EnumDetail(Name = "转弯加速")] 12 TurningAdd, 13 [EnumDetail(Name = "转弯减速")] 14 TurningReduce 15 }
1 static void Main(string[] args) 2 { 3 Console.WriteLine($"{ WheelchairDataType.BackwardAdd.ToString()}:{ WheelchairDataType.BackwardAdd.GetName()}"); 4 Console.ReadLine(); 5 }
枚举的描述值获取:

1 public static class EnumExtensions 2 { 3 /// <summary> 4 /// 获取枚举/中文字典 5 /// </summary> 6 /// <param name="enumValue"></param> 7 /// <returns></returns> 8 public static Dictionary<string, string> GetEnumDict<TEnum>(this TEnum enumValue) where TEnum : struct 9 { 10 Type type = enumValue.GetType(); 11 12 Dictionary<string, string> dict = new Dictionary<string, string>(); 13 FieldInfo[] fields = type.GetFields(); 14 foreach (FieldInfo field in fields) 15 { 16 if (field.FieldType.IsEnum) 17 { 18 var customAttributes = field.GetCustomAttributes(typeof(EnumDetailAttribute), false).ToList(); 19 dict.Add(field.Name, ((EnumDetailAttribute)customAttributes[0]).Name); 20 } 21 } 22 23 return dict; 24 } 25 /// <summary> 26 /// 获取枚举描述特性值 27 /// </summary> 28 /// <typeparam name="TEnum"></typeparam> 29 /// <param name="enumValue">枚举值</param> 30 /// <returns>枚举值的描述</returns> 31 public static string GetName<TEnum>(this TEnum enumValue) where TEnum : struct 32 { 33 Type type = enumValue.GetType(); 34 //枚举的成员信息 35 foreach (var memberInfo in type.GetMembers()) 36 { 37 if (memberInfo.Name != enumValue.ToString()) continue; 38 //获取自定义标记 39 foreach (Attribute attr in memberInfo.GetCustomAttributes(typeof(EnumDetailAttribute), false)) 40 { 41 var attribute = attr as EnumDetailAttribute; 42 if (attribute == null) continue; 43 return attribute.Name; 44 } 45 } 46 return string.Empty; 47 } 48 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)