C# 枚举 Enum 工具类
来源:互联网 发布:ipad照片怎么导入mac 编辑:程序博客网 时间:2024/06/05 04:45
public static class EnumUtil { /// <summary> /// 枚举子项实体 /// </summary> public class EnumItem { /// <summary> /// 子项名称 /// </summary> public string Name { get; set; } /// <summary> /// 子项描述标注 /// </summary> public string Description { get; set; } /// <summary> /// 子项 int 值 /// </summary> public int IntValue { get; set; } } public class EnumItem<T> : EnumItem where T : Attribute { /// <summary> /// 自定义扩展 /// </summary> public T Attribute { get; set; } } /// <summary> /// 获取枚举子项列表 /// </summary> /// <param name="enumType"></param> /// <returns></returns> public static List<EnumItem> GetEnumListItem(this Type enumType) { List<EnumItem> result = new List<EnumItem>(); //获取枚举类型的所有子项 //排除 名为 value__ 的项,这个是系统保留项,不处理。 FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray(); foreach (FieldInfo field in fields) { EnumItem item = new EnumItem(); //子项名字 item.Name = field.Name; //获取标注 DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (EnumAttributes != null && EnumAttributes.Length > 0) { item.Description = EnumAttributes[0].Description; } //获取 int 值 item.IntValue = (int)System.Enum.Parse(enumType, field.Name); result.Add(item); } return result; } public static List<EnumItem<T>> GetEnumListItem<T>(this Type enumType) where T : Attribute { List<EnumItem<T>> result = new List<EnumItem<T>>(); //获取枚举类型的所有子项 //排除 名为 value__ 的项,这个是系统保留项,不处理。 FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray(); foreach (FieldInfo field in fields) { EnumItem<T> item = new EnumItem<T>(); //子项名字 item.Name = field.Name; //获取标注 DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (EnumAttributes != null && EnumAttributes.Length > 0) { item.Description = EnumAttributes[0].Description; } //获取自定义标注 T[] customAttributes = (T[])field.GetCustomAttributes(typeof(T), false); if (customAttributes != null && customAttributes.Length > 0) { item.Attribute = customAttributes as T; } //获取 int 值 item.IntValue = (int)System.Enum.Parse(enumType, field.Name); result.Add(item); } return result; } public static EnumItem<T> GetEnum<T>(this Type enumType, string nameOrDescript) where T : Attribute { EnumItem<T> result = null; var list = GetEnumListItem<T>(enumType); foreach (var item in list) { if (item.Name == nameOrDescript || item.Description == nameOrDescript) { result = item; break; } } return result; } public static EnumItem<T> GetEnum<T>(this Type enumType, int value) where T : Attribute { EnumItem<T> result = null; var list = GetEnumListItem<T>(enumType); foreach (var item in list) { if (item.IntValue == value) { result = item; break; } } return result; } public static EnumItem GetEnum(this Type enumType, string nameOrDescript) { EnumItem result = null; var list = GetEnumListItem(enumType); foreach (var item in list) { if (item.Name == nameOrDescript || item.Description == nameOrDescript) { result = item; break; } } return result; } public static EnumItem GetEnum(this Type enumType, int value) { EnumItem result = null; var list = GetEnumListItem(enumType); foreach (var item in list) { if (item.IntValue == value) { result = item; break; } } return result; } /// <summary> /// 获取枚举的描述 /// </summary> /// <param name="enumType"></param> /// <param name="enum_item_name"></param> /// <returns></returns> public static string GetEnumDescription(this Type enumType, string enum_item_name) { string result = string.Empty; if (ExistsEnumItem(enumType, enum_item_name)) { //直接通过子项名字找出子项的反射类型。 FieldInfo field = enumType.GetField(enum_item_name); //反射类型获取出子项上的标注 DescriptionAttribute[] Descriptions = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (Descriptions != null && Descriptions.Length > 0) { result = Descriptions[0].Description; } } return result; } /// <summary> /// 获取枚举的描述 /// </summary> /// <param name="enumType"></param> /// <param name="enum_item_value"></param> /// <returns></returns> public static string GetEnumDescription(this Type enumType, int enum_item_value) { string result = string.Empty; FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray(); foreach (FieldInfo field in fields) { if ((int)System.Enum.Parse(enumType, field.Name) == enum_item_value) { DescriptionAttribute[] Descriptions = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (Descriptions != null && Descriptions.Length > 0) { result = Descriptions[0].Description; } break; } } return result; } /// <summary> /// 判断给定的名字是否是枚举里的子项 /// </summary> /// <param name="enumType"></param> /// <param name="enum_item_name"></param> /// <returns></returns> public static bool ExistsEnumItem(this Type enumType, string enum_item_name) { return enumType.GetField(enum_item_name) != null; } /// <summary> /// 判断给定的值是否是枚举里的子项的值 /// </summary> /// <param name="enumType"></param> /// <param name="enum_item_value"></param> /// <returns></returns> public static bool ExistsEnumItem(this Type enumType, int enum_item_value) { bool result = false; FieldInfo[] fields = enumType.GetFields().Where(s => s.Name != "value__").ToArray(); foreach (FieldInfo field in fields) { if ((int)System.Enum.Parse(enumType, field.Name) == enum_item_value) { result = true; break; } } return result; } }
0 0
- C# 枚举 Enum 工具类
- C#中的enum枚举类
- C# Idioms: Enum还是Enum Class(枚举类)
- C# Idioms: Enum还是Enum Class(枚举类)
- C# Idioms:Enum还是Enum Class(枚举类)
- C# Idioms: Enum还是Enum Class(枚举类)
- C#枚举类型enum
- C#枚举类型enum
- C#枚举类型enum
- C# Enum (枚举)
- c# 枚举 enum
- c# 枚举 enum 类型
- C# Enum (枚举)
- c#中的枚举(enum)
- C#枚举遍历--Enum
- C# 枚举(Enum)
- c#枚举enum
- C#梳理【枚举Enum】
- 交叉编译sqlite3
- 从 windows 到 linux(1):g++不能用函数内定义的类型做模板参数
- 第7周项目1电阻串联
- QtQuick桌面应用开发指导 4)动态管理Note对象_B 5)外观加强 6)更多改进
- linux对源文件的编译过程
- C# 枚举 Enum 工具类
- WPF界面刷新
- 使用RadUpload控件上传大文件时出现如下bug,请大虾帮忙解决下RadUpload Ajax callback error.Source url returned invalid content
- tomcat启用gzip压缩
- 函数返回值的问题
- 《算法竞赛入门经典(第2版)》代码 Chapter 1
- TCP/IP详解--滑动窗口、拥塞窗口、慢启动、Negle算法
- UVA11248 网络流
- uva532