using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace Assets.Plugins.CommonTool { public static class ListHelper { /// /// 自定义Distinct扩展方法 /// /// 要去重的对象类 /// 自定义去重的字段类型 /// 要去重的对象 /// 获取自定义去重字段的委托 /// public static IEnumerable IwbDistinct(this IEnumerable source, Func getField) { return source.Distinct(new Compare(getField)); } /// /// 合并字典 /// /// /// 字典1 /// 字典2 /// 字典2是否覆盖字典1,默认 true /// public static Dictionary MergeDictionary(this Dictionary first, Dictionary second ,bool isCover= true) { if (first == null) first = new Dictionary(); if (second == null) return first; foreach (string key in second.Keys) { if (first.ContainsKey(key)) { if (isCover) { first[key] = second[key]; } } else { first.Add(key, second[key]); } } return first; } /// /// 合并字典 /// /// 字典1 /// 字典2 /// 字典2是否覆盖字典1,默认 true /// public static Hashtable MergeHashtable(this Hashtable first, Hashtable second ,bool isCover= true) { if (first == null) first = new Hashtable(); if (second == null) return first; foreach (string key in second.Keys) { if (first.ContainsKey(key)) { if (isCover) { first[key] = second[key]; } } else { first.Add(key, second[key]); } } return first; } } public class Compare : IEqualityComparer { private Func GetField { get; } public Compare(Func getField) { GetField = getField; } public bool Equals(T x, T y) { return EqualityComparer.Default.Equals(GetField(x), GetField(y)); } public int GetHashCode(T obj) { return EqualityComparer.Default.GetHashCode(GetField(obj)); } } }