C# .NET 获取枚举值的自定义属性
一、定义一个类
- 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 获取枚举值的自定义属性的更多相关文章
- C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
一.引言 枚举为我看日常开发的可读性提供的非常好的支持,但是有时我们需要得到枚举值得描述信息或者是注释(备注)信息 比如要获得 TestEmun.aaa 属性值得备注 AAA,比较不方便得到. pub ...
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
- c# 枚举的定义,枚举的用法,获取枚举值
1.定义枚举类型 public enum Test { 男 = , 女 = } 2.获取枚举值 public void EnumsAction() { var s = Test.男;//男 var a ...
- C#记录日志、获取枚举值 等通用函数列表
) { ] >= && ipvals[] <= && ipval ...
- C# 获取枚举值/获取名字和值
枚举 int 转 枚举名称 public void Test() { //调用 string name1= ConvertEnumToString<ActionLogType>(1); s ...
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- c#枚举 获取枚举键值对、描述等
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...
- c#枚举值增加特性说明
c#枚举值增加特性说明 通过特性给一个枚举类型每个值增加一个字符串说明,用于打印或显示. 自定义打印特性 [AttributeUsage(AttributeTargets.Field)] public ...
- 获取Enum枚举值描述的几法方法
原文:获取Enum枚举值描述的几法方法 1.定义枚举时直接用中文 由于VS对中文支持的很不错,所以很多程序员都采用了此方案. 缺点:1.不适合多语言 2.感觉不太完美,毕竟大部分程序员大部分代码都使用 ...
随机推荐
- ipv6地址累加函数
#include <stdio.h> #include <arpa/inet.h> int main() { int i; int ret; struct in6_addr a ...
- Linux网络端口命名规则,一致性网络设备命名
参考文档: https://www.cnblogs.com/pipci/p/9229571.html 一致性网络设备命名,即Consistent Network Device Naming. 一.服务 ...
- Two Sum III - Data structure design LT170
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- GRADLE下运行main函数/执行测试用例
group 'gongsibao.ged' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'idea' sourceCompati ...
- How to Create Triggers in MySQL
https://www.sitepoint.com/how-to-create-mysql-triggers/ I created two tables: CREATE TABLE `sw_user` ...
- 数的划分(NOIP2001&水题测试2017082401)
题目链接:数的划分 这题直接搜索就行了.给代码,思路没什么好讲的,要讲的放在代码后面: #include<bits/stdc++.h> using namespace std; int d ...
- 金币(NOIP2015)
先给题目:金币 又是很水的题,很简单,直接上代码: #include<bits/stdc++.h> using namespace std; int main(){ int n; scan ...
- KBMMW 4.84.00 发布
kbmMW is a portable, highly scalable, high end application server and enterprise architecture integr ...
- JS页面跳转大全
所谓的js页面跳转就是利用javesrcipt对打开的页面ULR进行跳转,如我们打开的是A页面,通过javsrcipt脚本就会跳转到B页面.目前很多垃圾站经常用js跳转将正常页面跳转到广告页面,当然也 ...
- 2019.01.08 codeforces 1009F. Dominant Indices(长链剖分)
传送门 长链剖分模板题. 题意:给出一棵树,设fi,jf_{i,j}fi,j表示iii的子树中距离点iii距离为jjj的点的个数,现在对于每个点iii要求出使得fif_ifi取得最大值的那个jjj ...