Enum 枚举值 (一) 获取描述信息
封装了方法:
public static class EnumOperate
{
public class BaseDescriptionAttribute : DescriptionAttribute
{
public BaseDescriptionAttribute(string descriptionCN)
: base(descriptionCN)
{
}
public BaseDescriptionAttribute(string descriptionCN, string descriptionEN)
: base(descriptionCN)
{
this.descriptionEN = descriptionEN;
}
private string descriptionEN;
public string DescriptionEN
{
get { return descriptionEN; }
set { descriptionEN = value; }
}
}
/// <summary>
/// 扩展方法,获得枚举的Description中英文
public static EnumItem GetBaseDescription(this Enum value, Boolean nameInstead = true)
{
EnumItem e = new EnumItem();
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
BaseDescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(BaseDescriptionAttribute)) as BaseDescriptionAttribute;
e.EnumKey = Convert.ToInt32(value);
e.EnumValue = value.ToString();
if (attribute == null && nameInstead == true)
{
e.DescriptionCN = name;
e.DescriptionEN = name;
return e;
}
else
{
e.DescriptionCN = attribute.Description;
e.DescriptionEN = attribute.DescriptionEN;
return e;
}
}
/// <summary>
/// 扩展方法,获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true)
{
return name;
}
return attribute == null ? null : attribute.Description;
}
/// <summary>
/// 把枚举转换为键值对集合
/// 枚举转换为键值对集合
/// Dictionary<Int32, String> dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription());
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="getText">获得值得文本</param>
/// <returns>以枚举值为key,枚举文本为value的键值对集合</returns>
public static Dictionary<Int32, String> EnumToDictionary(Type enumType, Func<Enum, String> getText)
{
if (!enumType.IsEnum)
{
throw new ArgumentException("传入的参数必须是枚举类型!", "enumType");
}
Dictionary<Int32, String> enumDic = new Dictionary<int, string>();
Array enumValues = Enum.GetValues(enumType);
foreach (Enum enumValue in enumValues)
{
Int32 key = Convert.ToInt32(enumValue);
String value = getText(enumValue);
enumDic.Add(key, value);
}
return enumDic;
}
/// <summary>
/// 获取枚举的相关信息
/// </summary>
/// <param name="e">枚举的类型</param>
/// <returns></returns>
public static List<EnumItem> GetEnumItems(Type e)
{
List<EnumItem> itemList = new List<EnumItem>();
foreach (Enum v in Enum.GetValues(e))
{
EnumItem item = new EnumItem();
// TODO: 遍历操作
var q=GetBaseDescription(v);
item.EnumKey = q.EnumKey;
item.EnumValue = q.EnumValue;
item.DescriptionCN = q.DescriptionCN;
item.DescriptionEN = q.DescriptionEN;
itemList.Add(item);
}
return itemList;
}
public class EnumItem
{
public int EnumKey { get; set; }
public string EnumValue { get; set; }
public string DescriptionCN { get; set; }
public string DescriptionEN { get; set; }
}
}
枚举:
//港口类型
public enum HarborType
{
[Description("Seaport")]
[EnumOperate.BaseDescription("Seaport", "海港")]
Seaport = 0,
[Description("Airport")]
[EnumOperate.BaseDescription("Airport", "空港")]
Airport = 1,
}
使用:
static void Main(string[] args)
{
//获取枚举信息
var q = EnumOperate.GetEnumItems(typeof(HarborType));
Console.ReadLine();
}
Enum 枚举值 (一) 获取描述信息的更多相关文章
- 获取Enum枚举值描述的几法方法
原文:获取Enum枚举值描述的几法方法 1.定义枚举时直接用中文 由于VS对中文支持的很不错,所以很多程序员都采用了此方案. 缺点:1.不适合多语言 2.感觉不太完美,毕竟大部分程序员大部分代码都使用 ...
- dedecms 获取描述信息限制字数
对于我刚刚刚开始对于获取到了描述的信息,但是有些字数简直是太多了,显示的样式不好看,所以我就希望限制字数,所以我来告诉你们获取描述信息限制字数的语法吧[field:description functi ...
- MVC3不能正确识别JSON中的Enum枚举值
一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...
- 如何在类中根据枚举值,获取枚举的message的工具类
枚举类为: public enum OrderStatusEnum implements CondeEnum{ NEW(0, "新订单"), FINISHED(1, "完 ...
- java enum 枚举值
public enum PieChartEnum { PIE00("pie00"), PIE10("pie10"), PIE11("pie11&quo ...
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
- 利用DescriptionAttribute定义枚举值的描述信息 z
System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类 ...
- C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
一.引言 枚举为我看日常开发的可读性提供的非常好的支持,但是有时我们需要得到枚举值得描述信息或者是注释(备注)信息 比如要获得 TestEmun.aaa 属性值得备注 AAA,比较不方便得到. pub ...
- c#枚举 获取枚举键值对、描述等
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...
随机推荐
- sql的一个查询,情景:a表中存在的数据,且在b表中不存在 (not in,not exists
这里需要强调的是b表中关联字段的值是唯一的这种情况,并且b表尽量是列举类型的,意味着表比较小. ==================== 准备数据: 1. 建两个类似表,test1,test2,只有i ...
- 将jar文件加到Maven的local repository中
对于Maven项目来说,日常使用的多数第三方java库文件都可以从Maven的Central Repository中自动下载,但是如果我们需要的jar文件不在Central Repository中,那 ...
- 在win10环境中安装xilinx vivado IDE时出现的问题及解决方法
1.问题:There is no valid Xilinx installation that this Update can be applied to. 解决方法一:下载的是更新包,如果设备没有预 ...
- 一个IOS自动化打包的脚本
网上找了一个脚本,在其中进行了修改,只需要一条命令就可以了 支持自动导入配置文件 支持自动安装p12证书 支持自动修改版本号和build版本号 支持自动修改app显示名称 支持自动修改bundle i ...
- ME_PROCESS_PO_CUST 实现采购订单行项目增强
用户希望创建采购订单时,输入行项目时,能根据采购订单类型,自动带出科目分类类别. 业务顾问看了一下配置,不能实现这个功能,所以用增强实现. 采购订单BADI增强:ME_PROCESS_PO_CUST. ...
- 2017-2018 Exp6 信息搜集与漏洞扫描 20155214
目录 Exp6 信息搜集与漏洞扫描 实验内容 信息收集 漏洞扫描 知识点 Exp6 信息搜集与漏洞扫描 收集渗透目标的情报是最重要的阶段.如果收集到有用的情报资料的话,可以大大提高对渗透测试的成功性. ...
- 20155218《网络对抗》Exp3 免杀原理与实践
20155218<网络对抗>Exp3 免杀原理与实践 一.使用msf生成后门程序的检测 (1)将上周msf生成的后门文件放在virscan.org中进行扫描,截图如下: (2)使用msf时 ...
- 20155232《网络对抗》Exp4 恶意代码分析
20155232<网络对抗>Exp4 恶意代码分析 1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门 ...
- 20155338《网络对抗》Exp6 信息搜集与漏洞扫描
20155338<网络对抗>Exp6 信息搜集与漏洞扫描 实验过程 外围信息搜集 (1)whois域名注册信息查询 下面是搜索hao123.com得到的结果 下面这个也是同理 (2)nsl ...
- 在服务器运行一个jar包,不用时终止它
1.打成jar包后,输入命令 nohup java -jar floodlight.jar >log.txt >& &//nohup 不生成 nohup.out的方法noh ...