zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C# 枚举操作工具类

c#工具 操作 枚举
2023-09-14 09:03:18 时间
/// param name="enumName" 需要获取枚举描述的枚举 /param /// returns 描述内容 /returns public static string GetDescription(Enum enumName) string _description = string.Empty; FieldInfo _fieldInfo = enumName.GetType().GetField(enumName.ToString()); DescriptionAttribute[] _attributes = GetDescriptAttr(_fieldInfo); if (_attributes != null _attributes.Length 0) _description = _attributes[0].Description; else _description = enumName.ToString(); return _description; catch (Exception) throw; #endregion #region 获取字段Description(private) /// summary /// 获取字段Description /// /summary /// param name="fieldInfo" FieldInfo /param /// returns DescriptionAttribute[] /returns private static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo) if (fieldInfo != null) return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return null; catch (Exception) throw; #endregion #region 根据Description获取枚举定义字符串 /// summary /// 根据Description获取枚举定义字符串 /// /summary /// typeparam name="T" 枚举类型 /typeparam /// param name="description" 枚举描述 /param /// returns 枚举 /returns public static T GetEnumName T (string description) Type _type = typeof(T); foreach (FieldInfo field in _type.GetFields()) DescriptionAttribute[] _curDesc = field.GetDescriptAttr(); if (_curDesc != null _curDesc.Length 0) if (_curDesc[0].Description == description) return (T)field.GetValue(null); else if (field.Name == description) return (T)field.GetValue(null); throw new ArgumentException(string.Format("{0} 未能找到对应的枚举.", description), "Description"); #endregion #region 将枚举转换为ArrayList /// summary /// 将枚举转换为ArrayList /// 说明: /// 若不是枚举类型,则返回NULL /// /summary /// param name="type" 枚举类型 /param /// returns ArrayList /returns public static ArrayList ToArrayList(Type type) if (type.IsEnum) ArrayList _array = new ArrayList(); Array _enumValues = Enum.GetValues(type); foreach (Enum value in _enumValues) _array.Add(new KeyValuePair Enum, string (value, GetDescription(value))); return _array; return null; catch (Exception) throw; #endregion #region 根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串 /// summary /// 根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串 /// /summary /// param name="value" /param /// param name="enumType" /param /// returns /returns public static String GetEnumDescriptionString(int value, Type enumType) NameValueCollection nvc = GetNVCFromEnumValue(enumType); return nvc[value.ToString()]; catch (Exception) throw; #endregion #region 根据枚举类型得到其所有的值 与 枚举定义Description属性 的集合 /// summary /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合 /// /summary /// param name="enumType" /param /// returns /returns public static NameValueCollection GetNVCFromEnumValue(Type enumType) NameValueCollection nvc = new NameValueCollection(); Type typeDescription = typeof(DescriptionAttribute); System.Reflection.FieldInfo[] fields = enumType.GetFields(); string strText = string.Empty; string strValue = string.Empty; foreach (FieldInfo field in fields) if (field.FieldType.IsEnum) strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString(); object[] arr = field.GetCustomAttributes(typeDescription, true); if (arr.Length 0) DescriptionAttribute aa = (DescriptionAttribute)arr[0]; strText = aa.Description; else strText = ""; nvc.Add(strValue, strText); return nvc; catch (Exception) throw; #endregion #region 根据枚举值得到相应的枚举定义字符串 /// summary ///根据枚举值得到相应的枚举定义字符串 /// /summary /// param name="value" /param /// param name="enumType" /param /// returns /returns public static String GetEnumString(int value, Type enumType) NameValueCollection nvc = GetEnumStringFromEnumValue(enumType); return nvc[value.ToString()]; catch (Exception) throw; #endregion #region 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合 /// summary /// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合 /// /summary /// param name="enumType" /param /// returns /returns public static NameValueCollection GetEnumStringFromEnumValue(Type enumType) NameValueCollection nvc = new NameValueCollection(); Type typeDescription = typeof(DescriptionAttribute); System.Reflection.FieldInfo[] fields = enumType.GetFields(); string strText = string.Empty; string strValue = string.Empty; foreach (FieldInfo field in fields) if (field.FieldType.IsEnum) strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString(); nvc.Add(strValue, field.Name); return nvc; catch (Exception) throw; #endregion } /span


如果知道枚举的编号就是那个int类型的数字,获取name直接强转就可以 (枚举类型)(int值)


小注:
       对于基础公共方法类来说,其中最好不是获取异常直接抛出,切记不要吞掉或者只抛出部分信息。

异常部分可以参考:点击打开链接

本人参考:http://blog.csdn.net/yenange/article/details/7788332

     


Java 中数组 binarySearch 方法and拷贝对象工具类CopyUtils-可忽略覆盖Null值详解 [1] 该搜索键在范围内,但不是数组元素,由1开始计数,得“ - 插入点索引值”; [2] 该搜索键在范围内,且是数组元素,由0开始计数,得搜索值的索引值; [3] 该搜索键不在范围内,且小于范围(数组)内元素,返回–(fromIndex + 1); [4] 该搜索键不在范围内,且大于范围(数组)内元素,返回 –(toIndex + 1)。
Java基础(七)| 类、对象、封装和构造详解 ⭐本专栏旨在对JAVA的基础语法及知识点进行全面且详细的讲解,完成从0到1的java学习,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握JAVA编程,同时为后续的框架学习,进阶开发的代码能力打下坚实的基础。
[Java Base] 类,接口,枚举,静态常量到底应该放在哪? 静态常量什么时候适合放在类中?什么时候适合放在接口中?什么时候适合放在枚举中呢?放在不同的type中会引发什么不良后果呢?本篇就来解析一下~常量类应该是final,不变的,而接口里的参数是final,也是不变的。那么,看起来接口是放常量没有一定问题,还省去了final的输入,非常的合适。但是,类是只能单继承的,接口是允许多实现的。要是类实现的多个接口出现重名的常量,会报错,必须要在实现类明确常量用的是哪个接口的。