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     }

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

  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/ILoveSleep/p/3261200.html
Copyright © 2011-2022 走看看