VPet/VPet.Solution/Utils/CompareUtils.cs
2024-03-30 15:00:02 +08:00

43 lines
1.0 KiB
C#

namespace HKW.HKWUtils;
/// <summary>
/// 比较工具
/// </summary>
/// <typeparam name="T">比较类型</typeparam>
public class CompareUtils<T> : IEqualityComparer<T>, IEquatable<CompareUtils<T>>
{
/// <summary>
/// (Comparer, CompareUtils)
/// </summary>
public static Dictionary<Func<T, object>, CompareUtils<T>> Comparers = new();
public static CompareUtils<T> Create(Func<T, object> comparer)
{
if (Comparers.TryGetValue(comparer, out var value) is false)
value = Comparers[comparer] = new CompareUtils<T>(comparer);
return value;
}
public Func<T, object> Comparer { get; set; }
public CompareUtils(Func<T, object> comparer)
{
Comparer = comparer;
}
public bool Equals(T x, T y)
{
return Comparer(x).Equals(Comparer(y));
}
public int GetHashCode(T obj)
{
return Comparer(obj).GetHashCode();
}
public bool Equals(CompareUtils<T> other)
{
return Comparer.Equals(other.Comparer);
}
}