zoukankan      html  css  js  c++  java
  • 如何为枚举类型添加说明

    枚举类型大家在平时程序中经常会用到,先看下面的列子

    1 enum Orientation
    2         {
    3             East,
    4             South,
    5             West,
    6             North
    7         }

    上面代码定义了一个方向的枚举类型,包括东、南、西、北四个值,这样似乎也没有问题,但是我们是中国人,有时候看中文会更加习惯一些,更重要的是在做数据显示的时候,利用Enum.GetNames方法也只能获得像“North”这样的英文,而得不到理想中的“北”。这种情况,尤其是在数据绑定的时候将十分的麻烦,还需要另外写代码将“东、南、西、北”的字符定义出来,再与具体的枚举值关联起来。

    为了解决这个问题,可以直接将值的字符串定义成中文,如下所示。

    1 enum Orientation
    2         {
    3             东,
    4             南,
    5             西,
    6 7         }

    这样,通过Enum.GetNames方法就可以获得“东、南、西、北”这样的字符了。但是这样的编码也太违背编码习惯了,毕竟中文字符变量让人看起来十分的别扭。如何解决这个问题呢?答案就是使用Attribute!下面就是示例代码。

     1 enum Orientation
     2         {
     3             [DescriptionAttribute("")]
     4             East,
     5             [DescriptionAttribute("")]
     6             South,
     7             [DescriptionAttribute("西")]
     8             West,
     9             [DescriptionAttribute("")]
    10             North
    11         }

    但又如何获取DescriptionAttribute的内容呢?呵呵,答案就是通过反射。下面是我封装获取DescriptionAttribute内容的扩展方法。

    1 static string GetDescription<T>(this T value)
    2         {
    3             var memInfo = value.GetType().GetMember(value.ToString());
    4             var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>();
    5             if (attributes.Any())
    6                 return attributes.First().Description;
    7             return string.Empty;
    8         }

    下面就是测试的代码以及测试结果

     1 static class Program
     2     {
     3         enum Orientation
     4         {
     5             [DescriptionAttribute("")]
     6             East,
     7             [DescriptionAttribute("")]
     8             South,
     9             [DescriptionAttribute("西")]
    10             West,
    11             [DescriptionAttribute("")]
    12             North
    13         }
    14         static string GetDescription<T>(this T value)
    15         {
    16             var memInfo = value.GetType().GetMember(value.ToString());
    17             var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>();
    18             if (attributes.Any())
    19                 return attributes.First().Description;
    20             return string.Empty;
    21         }
    22         static void Main()
    23         {
    24             foreach (var s in Enum.GetValues(typeof(Orientation)))
    25             {
    26                 Console.WriteLine("值:{0}		说明:{1}",s.ToString(),s.GetDescription());
    27             }
    28         }        
    29     }

    从输出可以看出,程序成功的获取了枚举值的说明内容,这样就可以利用这种模式方便的对枚举值添加中文说明,然后在需要的时候提取出来,如:需要做数据绑定的时候。

  • 相关阅读:
    【游普罗旺斯薰衣草庄园】诗一首
    【甘道夫】Win7环境下Eclipse连接Hadoop2.2.0
    STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)
    JAVA基础之訪问控制权限(封装)
    Linux Android 多点触摸协议 原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/os/71/12306571.shtml
    block的知识点
    Zookeeper 集群搭建--单机伪分布式集群
    Zookeeper 四字命令 Four Letter Words
    Zookeeper权限acl,acl的构成 scheme与id
    Zookeeper命令行auth,digest
  • 原文地址:https://www.cnblogs.com/ILoveSleep/p/3261200.html
Copyright © 2011-2022 走看看