/// <summary> /// 通过委托实现<see cref="IEqualityComparer{Obj}"/>, /// 它从<typeparamref name="Obj"/>对象中提取键, /// 然后逐个比较它们,作为判断相等的标准 /// </summary> /// <typeparam name="Obj">要比较的对象类型</typeparam> /// <param name="getKey">从<typeparamref name="Obj"/>对象中提取键的委托</param> /// <returns></returns> public static IEqualityComparer <Obj> EqualityComparer <Obj>(params Func <Obj, object>[] getKey) { getKey.AnyCheck("用来获取键的委托"); return(EqualityComparer <Obj>( (x, y) => getKey.All(del => Equals(del(x), del(y))), x => ToolEqual.CreateHash(getKey.Select(del => del(x)).ToArray()))); }
/// <summary> /// 使用指定的参数初始化对象 /// </summary> /// <param name="equals">这个委托用于执行相等比较</param> /// <param name="getHashCode">这个委托用于计算哈希值</param> public EqualityComparer(Func <Obj, Obj, bool> equals, Func <Obj, int> getHashCode) { this.EqualsDelegate = (x, y) => ToolEqual.JudgeNull(x, y) ?? equals(x !, y !); this.GetHashCodeDelegate = x => x is null ? 0 : getHashCode(x); }