C# 添加枚舉中文資源
阿新 • • 發佈:2019-03-25
scrip value getfields bubuko ext lose tostring reac 一個
在業務開發過程中,添加枚舉,在固定枚舉值的同時,也需要中文的文案。
如果不想添加語言資源項、添加枚舉轉語資源項,可以使用特性標記。
屬性描述 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 }View Code
C# 添加枚舉中文資源