一、定义一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection; namespace XXX.XXX.Utils
{
/// <summary>
/// 备注特性
/// </summary>
public class DescAttribute : Attribute
{
private string m_desc;
public DescAttribute(string desc)
{
this.m_desc = desc;
}
/// <summary>
/// 备注
/// </summary>
public string Desc
{
get { return m_desc; }
set { m_desc = value; }
}
/// <summary>
/// 获取枚举的备注信息
/// </summary>
/// <param name="val">枚举值</param>
/// <returns></returns>
public static string GetEnumDesc(Enum val)
{
Type type = val.GetType();
FieldInfo fd = type.GetField(val.ToString());
if (fd == null)
return string.Empty;
object[] attrs = fd.GetCustomAttributes(typeof(DescAttribute), false);
string name = string.Empty;
foreach (DescAttribute attr in attrs)
{
name = attr.Desc;
}
return name;
}
}
/// <summary>
/// 枚举扩展类
/// </summary>
public static class EnumExtension
{
/// <summary>
/// 获取枚举的备注信息
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
public static string GetDesc(this Enum em)
{
Type type = em.GetType();
FieldInfo fd = type.GetField(em.ToString());
if (fd == null)
return string.Empty;
object[] attrs = fd.GetCustomAttributes(typeof(DescAttribute), false);
string name = string.Empty;
foreach (DescAttribute attr in attrs)
{
name = attr.Desc;
}
return name;
}
}
}

 

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

public enum EnumCalculationTag
{
[Desc("This is description")]
A
}

  

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

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. Lazarus的二维码解决方案

    不解释,直接上图

  2. JeeSite 4.0

    http://jeesite.com/ JeeSite 是一个 Java EE 企业级快速开发平台,基于经典技术组合(Spring Boot.Spring MVC.Apache Shiro.MyBat ...

  3. RabbitVCS - Ubuntu VCS Graphical Client

    Easy version control for Linux RabbitVCS is a set of graphical tools written to provide simple and s ...

  4. 微信小程序bug

    2017-11-21 微信movable-view有bug,它不能在style里面设置z-index,一旦设置了,不是层间的元素就会有干扰,比如我移动0层的movable-view,但是1层的mova ...

  5. 软件测试基础Ⅲ(osi7层协议,测试模型,LoadRunner组件,软件质量模型)

    osi7层开放式系统互连网络模型 1.物理层:主要定义物理设备标准,如网线的接口类型.光纤的接口类型.各种传输介质的传输速率等.它的主要作用是传输比特流(就是由1.0转化为电流强弱来进行传输,到达目的 ...

  6. HACK字体安装

    参考:https://github.com/source-foundry/Hack Linux的 下载最新版本的Hack. 从存档中提取文件(.zip). 将字体文件复制到系统字体文件夹(通常/usr ...

  7. 2017/2/8 hibernate + oracle 实现id的自增 同时 hibernate项目跑起来 会自己增加字段的原因 oracle触发器的使用

    hibernate + oracle 实现id的自增 1.在oracle中先创建一个序列 : 序列语法 如下 create  sequence   (序列名称)seq_student_id minva ...

  8. 【Linux】CentOS 7.2 安装 MySQL 5.7.21 解压版

    安装环境/工具 1.Linux(CentOS 7.2版) 2.mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 安装步骤 1.下载mysql解压版(mysql-5. ...

  9. 浅谈System.gc()

      今天巩固给大家讲讲System.gc().Java的内存管理着实给各位编程者带来很大的方便,使我们不再需要为内存分配烦太多神.那么讲到垃圾回收机制,就不得不讲讲System.gc().   先简单 ...

  10. O365 Manager Plus详解