C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
一、引言
枚举为我看日常开发的可读性提供的非常好的支持,但是有时我们需要得到枚举值得描述信息或者是注释(备注)信息
比如要获得 TestEmun.aaa 属性值得备注 AAA,比较不方便得到。
public enum TestEmun
{
/// AAA
aaa =1,
/// BBB
bbb =2,
/// CCC
ccc =3
}
如果要得到类似的效果,我们就需要修改一下代码了,使用反射很容易得到这个结果。
二、定义特性
using System;
using System.Reflection;
namespace Lib.DataModel.SysEnum
{
/// <summary>
/// 备注特性
/// </summary>
public class RemarkAttribute : Attribute
{
private string m_remark;
public RemarkAttribute(string remark)
{
this.m_remark = remark;
}
/// <summary>
/// 备注
/// </summary>
public string Remark
{
get { return m_remark; }
set { m_remark = value; }
}
/// <summary>
/// 获取枚举的备注信息
/// </summary>
/// <param name="val">枚举值</param>
/// <returns></returns>
public static string GetEnumRemark(Enum val)
{
Type type = val.GetType();
FieldInfo fd = type.GetField(val.ToString());
if (fd == null)
return string.Empty;
object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
string name = string.Empty;
foreach (RemarkAttribute attr in attrs)
{
name = attr.Remark;
}
return name;
}
}
/// <summary>
/// 枚举扩展类
/// </summary>
public static class EnumExtension
{
/// <summary>
/// 获取枚举的备注信息
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
public static string GetRemark(this Enum em)
{
Type type = em.GetType();
FieldInfo fd = type.GetField(em.ToString());
if (fd == null)
return string.Empty;
object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
string name = string.Empty;
foreach (RemarkAttribute attr in attrs)
{
name = attr.Remark;
}
return name;
}
}
}
三 、测试代码
public class UnitTest
{
public enum TestEmun
{
[Remark("AAA")]
aaa,
[Remark("BBB")]
bbb,
[Remark("CCC")]
ccc
}
public void GetEnumName()
{
//需要引用 Lib.DataModel.SysEnum 命名空间才可以使用 扩展方法
string name = TestEmun.aaa.GetRemark();
/*
name 值为 AAA
*/
}
}
四、扩展
//获取枚举的所有属性名称
var fields = typeof(MyEnum).GetFields(BindingFlags.Static | BindingFlags.Public);
foreach (var fi in fields)
Console.WriteLine(fi.Name);
原文地址:http://blog.csdn.net/xxj_jing/article/details/8556780
C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息的更多相关文章
- C# .NET 获取枚举值的自定义属性
一.定义一个类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 获取枚举值上的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#枚举值增加特性说明
c#枚举值增加特性说明 通过特性给一个枚举类型每个值增加一个字符串说明,用于打印或显示. 自定义打印特性 [AttributeUsage(AttributeTargets.Field)] public ...
- c#枚举 获取枚举键值对、描述等
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...
- Spring HTTP状态码枚举值对照表
使用Spring时总去查HTTP状态码对应的Spring枚举值的那篇代码,有点不方便,把代码拷贝出来统一替换格式做成了表格,放在这里,方便大家使用.(枚举类为HttpStatus) 枚举值 HTTP状 ...
随机推荐
- UIScrollView的属性总结
contentSize是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下 ...
- UI进阶 数据请求
一.HTTP和HTTPS协议 URL URL全称是Uniform Resource Locator(统一资源定位符)通过1个URL,能找到互联网上唯一的1个资源,也被称为网址,因特网上标准的资源网址 ...
- Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
- CSS文本与文字
-255之间 14.2 CSS中的文字属性 属性名称 属性值 说明 font-style norma ...
- webservice 地址
快递查询WEB服务 http://webservice.36wu.com/ExpressService.asmx 支持上百家快递/物流查询,准确高效,所有数据均来自快递服务商.此数据返回类型进行了封装 ...
- CentOS 下SSH无密码登录的配置
CentOS 下SSH无密码登录的配置 最近学习Hadoop.它要求各节点之间通过SSH无密码登录,配置SSH的时候费了一番功夫,记录下来,以备忘. 配置SSH无密码登录需要3步: 1.生成公钥和私钥 ...
- UITableview 中获取非选中的cell
实现效果如图: 在cell中有一个button,选中cell改变button的选择状态 yes,选中另外一个cell,别的cell中的button选择状态变成false. //获取当前可显示的cell ...
- Android 调用系统的分享[完美实现同一时候分享图片和文字]
android 系统的分享功能 private void share(String content, Uri uri){ Intent shareIntent = new Intent(Intent. ...
- air写文件 SecurityError: fileWriteResource 时报错的解决方法
用 File.applicationDerectoryPath.resolv("text.txt")会报SecuriyError错误! 解决: var _Path:File = F ...
- 分布式应用处理方式 - Remoting
分布式应用程序 所谓分布式计算是一门计算机科学,它研究如何把一个需要非常巨大的计算能力才能解决的问题分成许多小的部分,然后把这些部分分配给许多计算机进行处理,最后把这些计算结果综合起来得到最终的结果. ...