using iTextSharp.text.pdf.qrcode; using iWare_SCADA_Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace iWare_SCADA_BusinessLogical.Utils { /// /// 枚举类 /// public class EnumberHelper { /// /// 获取转换枚举值的字符串 /// /// /// public static string GetEnumName(int mode) where T : struct { T t = default(T); bool isRight = EnumberHelper.GetEnumObject(mode.ToString(), out t); if (isRight) { return t.ToString(); } return ""; } ///// ///// 获取转换枚举值的字符串 ///// ///// ///// //public static T GetEnum(int mode) where T : struct //{ // T t = default(T); // bool isRight = EnumberHelper.GetEnumObject(mode.ToString(), out t); // if (isRight) // { // return t; // } // throw new Exception("转换失败"); //} /// /// 根据枚举值转换枚举 /// /// /// /// public static bool GetEnumObject(string enumValue, out T t) where T : struct { bool right = Enum.TryParse(enumValue.ToString(), out t); if (right) { // 需要通过此方法再次确定是否是枚举实际定义的内容 if (!Enum.IsDefined(typeof(T), t)) { return false; } return true; } else { return false; } } public static T GetEnumForString(string enumValue) where T : struct { T t = default(T); bool isRight = EnumberHelper.GetEnumObject(enumValue, out t); return t; } /// /// 枚举转集合 /// /// /// public static List EnumToList() { List list = new List(); foreach (var e in Enum.GetValues(typeof(T))) { EnumberEntity m = new EnumberEntity(); object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); if (objArr != null && objArr.Length > 0) { DescriptionAttribute da = objArr[0] as DescriptionAttribute; m.Desction = da.Description; } m.EnumValue = Convert.ToInt32(e); m.EnumName = e.ToString(); list.Add(m); } return list; } /// /// 获取枚举的描述属性 /// /// 枚举值 /// 枚举的描述属性 public static string GetEnumDescription(Enum en) { Type type = en.GetType(); //获取类型 MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员 if (memberInfos != null && memberInfos.Length > 0) { DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性 if (attrs != null && attrs.Length > 0) { return attrs[0].Description; //返回当前描述 } } return en.ToString(); } } }