using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace JT809.Protocol.JT809Extensions { /// /// 枚举扩展 /// public static class JT809EnumExtensions { /// /// 转为整型 /// /// /// /// public static int ToValue(this T t) where T : struct { return Convert.ToInt32(t); } /// /// 转为Byte /// /// /// /// public static byte ToByteValue(this T t) where T : struct { return Convert.ToByte(t); } /// /// 转为整型 /// /// /// /// public static string ToValueString(this T t) where T : struct { return Convert.ToInt32(t).ToString(); } /// /// 字符转枚举 /// /// /// /// public static T ToEnum(this string value) where T : struct { return (T)Enum.Parse(typeof(T), value); } /// /// 获取枚举字符串 /// /// public static string GetName(this Enum valueEnum) { return valueEnum.ToString(); } /// /// 获取DescriptionAttribute特性枚举值的描述 /// /// /// public static string GetDescription(this Enum value) { var attribute = value.GetAttribute(); return attribute == null ? value.ToString() : attribute.Description; } /// /// 验证是否是枚举类型 /// /// /// /// public static bool IsEnumValid(this int enumValue) { return Enum.IsDefined(typeof(TEnum), enumValue); } /// /// 获取DescriptionAttribute特性枚举及描述 /// /// /// public static Dictionary GetDescriptionAttributeDictionary(this Enum value) { Dictionary dictionary = new Dictionary(); var fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public); foreach (var fi in fields) { DescriptionAttribute attr = Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute), false) as DescriptionAttribute; dictionary.Add(fi.Name, attr != null ? attr.Description : ""); } return dictionary; } /// /// 获取DisplayNameAttribute特性枚举值的描述 /// /// 枚举值 /// public static string GetDisplayName(this Enum value) { var attribute = value.GetAttribute(); return attribute == null ? value.ToString() : attribute.DisplayName; } /// /// 获取DisplayNameAttribute特性枚举及描述 /// /// /// public static Dictionary GetDisplayNameAttributeDictionary(this Enum value) { Dictionary dictionary = new Dictionary(); var fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public); foreach (var fi in fields) { DisplayNameAttribute attr = Attribute.GetCustomAttribute(fi, typeof(DisplayNameAttribute), false) as DisplayNameAttribute; dictionary.Add(fi.Name, attr != null ? attr.DisplayName : ""); } return dictionary; } /// /// 获取枚举对应特性 /// /// /// /// public static T GetAttribute(this Enum value) where T : Attribute { try { var type = value.GetType(); var memberInfo = type.GetMember(value.ToString()); var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false); return (T)attributes[0]; } catch { return default; } } /// /// 根据值获取对应枚举类型集合 /// /// 具体枚举类型 /// 枚举值 /// 位数(8,16,32) /// public static IEnumerable GetEnumTypes(this int value,int digit) where T : Enum { return GetEnumTypes(value, digit,true); } /// /// 根据值获取对应枚举类型集合 /// /// 具体枚举类型 /// 枚举值 /// 位数(8,16,32) /// 是否忽略未知数据 /// public static IEnumerable GetEnumTypes(this int value, int digit,bool ignoreUnknown) where T : Enum { List values = new List(); for (int i = 0; i < digit; i++) { if (Math.Pow(2, i) <= value) continue; values.Add((T)Enum.ToObject(typeof(T), (int)Math.Pow(2, i - 1))); value = value - (int)Math.Pow(2, i - 1); i = 0; if (value <= 0) break; } if (ignoreUnknown) { List results = new List(); foreach (var item in values) { foreach (string itemChild in Enum.GetNames(typeof(T))) { if (item.ToString() == itemChild) { results.Add(item); break; } } } return results; } else { return values; } } } }