获取枚举类型描述
Posted liuqiyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取枚举类型描述相关的知识,希望对你有一定的参考价值。
代码修改前:
switch (months) { case "1": nums = "1"; break; case "2": nums = "2"; break; case "3": nums = "3"; break; case "4": nums = "4"; break; case "5": nums = "5"; break; case "6": nums = "6"; break; case "7": nums = "7"; break; case "8": nums = "8"; break; case "9": nums = "9"; break; case "10": nums = "A"; break; case "11": nums = "B"; break; case "12": nums = "C"; break; }
代码修改后:
class Program { static void Main(string[] args) { Console.WriteLine(GetMonth("1")); Console.WriteLine(GetMonth("10")); Console.ReadKey(); } public static string GetMonth(string month) { var enumValue = (month)Enum.Parse(typeof(month), month); var enumAttributes = enumValue.GetType().GetField(enumValue.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false); var attributeValue = ((DescriptionAttribute[])enumAttributes)[0].Description; return attributeValue; } } public enum month { [Description("1")] Jan=1, [Description("2")] Feb =2, [Description("3")] Mar =3, [Description("4")] Apr =4, [Description("5")] May =5, [Description("6")] Jun =6, [Description("7")] Jul =7, [Description("8")] Aug =8, [Description("9")] Sep =9, [Description("A")] Oct =10, [Description("B")] Nov =11, [Description("C")] Dec =12 }
以上是关于获取枚举类型描述的主要内容,如果未能解决你的问题,请参考以下文章
从匹配类型的字符串中获取 Swift 枚举类型,而不是 rawValue
我的Android进阶之旅关于Android平台获取文件的mime类型:为啥不传小写后缀名就获取不到mimeType?为啥android 4.4系统获取不到webp格式的mimeType呢?(代码片段
我的Android进阶之旅关于Android平台获取文件的mime类型:为啥不传小写后缀名就获取不到mimeType?为啥android 4.4系统获取不到webp格式的mimeType呢?(代码片段