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         /// 
 2         /// 获取枚举/中文字典
 3         /// 
 4         /// 
 5         /// 
 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         /// 
 4         /// 获取枚举/中文字典
 5         /// 
 6         /// 
 7         /// 
 8         public static Dictionary<string, string> GetEnumDict(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         /// 
26         /// 获取枚举描述特性值
27         /// 
28         /// 
29         /// 枚举值
30         /// 枚举值的描述
31         public static string GetName(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     }
View Code

 

你可能感兴趣的:(C# 添加枚举中文资源)