一、定义一个类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7.  
  8. namespace XXX.XXX.Utils
  9. {
  10. /// <summary>
  11. /// 备注特性
  12. /// </summary>
  13. public class DescAttribute : Attribute
  14. {
  15. private string m_desc;
  16. public DescAttribute(string desc)
  17. {
  18. this.m_desc = desc;
  19. }
  20. /// <summary>
  21. /// 备注
  22. /// </summary>
  23. public string Desc
  24. {
  25. get { return m_desc; }
  26. set { m_desc = value; }
  27. }
  28. /// <summary>
  29. /// 获取枚举的备注信息
  30. /// </summary>
  31. /// <param name="val">枚举值</param>
  32. /// <returns></returns>
  33. public static string GetEnumDesc(Enum val)
  34. {
  35. Type type = val.GetType();
  36. FieldInfo fd = type.GetField(val.ToString());
  37. if (fd == null)
  38. return string.Empty;
  39. object[] attrs = fd.GetCustomAttributes(typeof(DescAttribute), false);
  40. string name = string.Empty;
  41. foreach (DescAttribute attr in attrs)
  42. {
  43. name = attr.Desc;
  44. }
  45. return name;
  46. }
  47. }
  48. /// <summary>
  49. /// 枚举扩展类
  50. /// </summary>
  51. public static class EnumExtension
  52. {
  53. /// <summary>
  54. /// 获取枚举的备注信息
  55. /// </summary>
  56. /// <param name="em"></param>
  57. /// <returns></returns>
  58. public static string GetDesc(this Enum em)
  59. {
  60. Type type = em.GetType();
  61. FieldInfo fd = type.GetField(em.ToString());
  62. if (fd == null)
  63. return string.Empty;
  64. object[] attrs = fd.GetCustomAttributes(typeof(DescAttribute), false);
  65. string name = string.Empty;
  66. foreach (DescAttribute attr in attrs)
  67. {
  68. name = attr.Desc;
  69. }
  70. return name;
  71. }
  72. }
  73. }

 

二、定义一个枚举,并引用如上命名空间

  1. public enum EnumCalculationTag
  2. {
  3. [Desc("This is description")]
  4. A
  5. }

  

三、获取注解(需引用“一”中的命名空间)

  1. EnumCalculationTag.A.GetDesc()

  

C# .NET 获取枚举值的自定义属性的更多相关文章

  1. C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息

    一.引言 枚举为我看日常开发的可读性提供的非常好的支持,但是有时我们需要得到枚举值得描述信息或者是注释(备注)信息 比如要获得 TestEmun.aaa 属性值得备注 AAA,比较不方便得到. pub ...

  2. 获取枚举值上的Description特性说明

    /// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...

  3. c# 枚举的定义,枚举的用法,获取枚举值

    1.定义枚举类型 public enum Test { 男 = , 女 = } 2.获取枚举值 public void EnumsAction() { var s = Test.男;//男 var a ...

  4. C#记录日志、获取枚举值 等通用函数列表

    )             {                 ] >=  && ipvals[] <=                  && ipval ...

  5. C# 获取枚举值/获取名字和值

    枚举 int 转 枚举名称 public void Test() { //调用 string name1= ConvertEnumToString<ActionLogType>(1); s ...

  6. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

  7. c#枚举 获取枚举键值对、描述等

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...

  8. c#枚举值增加特性说明

    c#枚举值增加特性说明 通过特性给一个枚举类型每个值增加一个字符串说明,用于打印或显示. 自定义打印特性 [AttributeUsage(AttributeTargets.Field)] public ...

  9. 获取Enum枚举值描述的几法方法

    原文:获取Enum枚举值描述的几法方法 1.定义枚举时直接用中文 由于VS对中文支持的很不错,所以很多程序员都采用了此方案. 缺点:1.不适合多语言 2.感觉不太完美,毕竟大部分程序员大部分代码都使用 ...

随机推荐

  1. ipv6地址累加函数

    #include <stdio.h> #include <arpa/inet.h> int main() { int i; int ret; struct in6_addr a ...

  2. Linux网络端口命名规则,一致性网络设备命名

    参考文档: https://www.cnblogs.com/pipci/p/9229571.html 一致性网络设备命名,即Consistent Network Device Naming. 一.服务 ...

  3. Two Sum III - Data structure design LT170

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. GRADLE下运行main函数/执行测试用例

    group 'gongsibao.ged' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'idea' sourceCompati ...

  5. How to Create Triggers in MySQL

    https://www.sitepoint.com/how-to-create-mysql-triggers/ I created two tables: CREATE TABLE `sw_user` ...

  6. 数的划分(NOIP2001&水题测试2017082401)

    题目链接:数的划分 这题直接搜索就行了.给代码,思路没什么好讲的,要讲的放在代码后面: #include<bits/stdc++.h> using namespace std; int d ...

  7. 金币(NOIP2015)

    先给题目:金币 又是很水的题,很简单,直接上代码: #include<bits/stdc++.h> using namespace std; int main(){ int n; scan ...

  8. KBMMW 4.84.00 发布

    kbmMW is a portable, highly scalable, high end application server and enterprise architecture integr ...

  9. JS页面跳转大全

    所谓的js页面跳转就是利用javesrcipt对打开的页面ULR进行跳转,如我们打开的是A页面,通过javsrcipt脚本就会跳转到B页面.目前很多垃圾站经常用js跳转将正常页面跳转到广告页面,当然也 ...

  10. 2019.01.08 codeforces 1009F. Dominant Indices(长链剖分)

    传送门 长链剖分模板题. 题意:给出一棵树,设fi,jf_{i,j}fi,j​表示iii的子树中距离点iii距离为jjj的点的个数,现在对于每个点iii要求出使得fif_ifi​取得最大值的那个jjj ...