using AspectCore.Extensions.Reflection; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace wms.util.Ext { public static class EnumExt { public static string GetDescription(this Enum obj) { var fi = obj.GetType().GetField(obj.ToString()); var arrDesc = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); return arrDesc[0]?.Description; } public static string GetDescription(this Enum obj) { var refactor = typeof(T).GetField(obj.ToString()).GetReflector(); var arrDesc = refactor.GetCustomAttributes(typeof(DescriptionAttribute)); return ((DescriptionAttribute)arrDesc[0]).Description; } public static T ToEnum(this string obj) { return (T)Enum.Parse(typeof(T), obj); } public static string GetDescription(string obj) { var refactor = typeof(T).GetField(obj).GetReflector(); var arrDesc = refactor.GetCustomAttributes(typeof(DescriptionAttribute)); return ((DescriptionAttribute)arrDesc[0]).Description; } public static Dictionary ToDict(this Enum obj) { var enumDict = new Dictionary(); foreach (int enumValue in Enum.GetValues(obj.GetType())) { enumDict.Add(enumValue, enumValue.ToString()); } return enumDict; } public static List ToList() { //Type t = typeof(T); //var ty = t.GetFields(BindingFlags.Public | BindingFlags.Static) //.Where(p => t.IsAssignableFrom(p.FieldType)) //.Select(pi => (T)pi.GetValue(null)) //.ToList(); return Enum.GetValues(typeof(T)).Cast().Select(x => x.ToString()).ToList(); } public static List> ToKVList() { var keys = new List>(); foreach (var e in Enum.GetValues(typeof(T))) { object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); DescriptionAttribute da = objArr?[0] as DescriptionAttribute; keys.Add(new KeyValuePair(Convert.ToInt32(e), da.Description)); } return keys; } public static List> ToKVListLinq() { Type t = typeof(T); return t.GetFields(BindingFlags.Public | BindingFlags.Static) .Where(p => t.IsAssignableFrom(p.FieldType)) .Select(a => new KeyValuePair(Convert.ToInt32(a.GetValue(null)), (a.GetCustomAttributes(typeof(DescriptionAttribute), true)?[0] as DescriptionAttribute).Description) ).ToList(); } /// /// 根据指定的属性返回集合中的非重复元素 /// /// 数据源的类型 /// 数据源需要进行筛选的属性 /// 数据源 /// 指定属性的函数表达式 /// public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) { var seenKeys = new HashSet(); foreach (var source1 in source) { if (seenKeys.Add(keySelector(source1))) yield return source1; } } /// /// 两个参数任意一个不重复即可 /// /// 数据源的类型 /// 数据源需要进行筛选的属性 /// 数据源需要进行筛选的属性 /// 数据源 /// 指定属性的函数表达式 /// 指定属性的函数表达式 /// public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector1, Func keySelector2) { var seenKeys1 = new HashSet(); var seenKeys2 = new HashSet(); foreach (var source1 in source) { if (seenKeys1.Add(keySelector1(source1))) //第一属性不重复 { seenKeys2.Add(keySelector2(source1)); //将第一属性对应的第二属性计入HashSet,避免第二属性重复 yield return source1; } else if (seenKeys2.Add(keySelector2(source1))) yield return source1; //第一属性重复,第二属性不重复 } } } }