C# 给枚举类型增加一个备注特性
/// <summary>
/// 备注特性
/// </summary>
public class RemarkAttribute : Attribute
{
/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
public RemarkAttribute(string remark)
{
this.Remark = remark;
}
}
/// <summary>
/// 枚举扩展类
/// </summary>
public static class EnumExtension
{
/// <summary>
/// 获取枚举的备注信息
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
public static string GetRemark(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi == null)
{
return value.ToString();
}
object[] attributes = fi.GetCustomAttributes(typeof(RemarkAttribute), false);
if (attributes.Length > 0)
{
return ((RemarkAttribute)attributes[0]).Remark;
}
else
{
return value.ToString();
}
}
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
}
var aaa = UserType.Type1.GetRemark();
var aab = UserType.Type2.GetEnumDescription();
一、引言
枚举为我看日常开发的可读性提供的非常好的支持,但是有时我们需要得到枚举值得描述信息或者是注释(备注)信息
比如要获得 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