网上演练
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4 KiB

// Needed for Workaround
#if !NET_4_6
using System;
using System.Collections.Generic;
using LinqInternal.Core;
namespace LinqInternal.Collections.Specialized
{
[System.Diagnostics.DebuggerNonUserCode]
internal class CustomEqualityComparer<T> : IEqualityComparer<T>
{
private readonly Func<T, T, bool> _comparison;
private readonly Func<T, int> _getHashCode;
public CustomEqualityComparer(Func<T, T, bool> comparison, Func<T, int> getHashCode)
{
if (comparison == null)
{
throw new ArgumentNullException("comparison");
}
if (getHashCode == null)
{
throw new ArgumentNullException("getHashCode");
}
_comparison = comparison;
_getHashCode = getHashCode;
}
public CustomEqualityComparer(IComparer<T> comparer, Func<T, int> getHashCode)
{
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
if (getHashCode == null)
{
throw new ArgumentNullException("getHashCode");
}
_comparison = (x, y) => comparer.Compare(x, y) == 0;
_getHashCode = getHashCode;
}
public CustomEqualityComparer(Func<T, T, int> comparison, Func<T, int> getHashCode)
{
if (comparison == null)
{
throw new ArgumentNullException("comparison");
}
if (getHashCode == null)
{
throw new ArgumentNullException("getHashCode");
}
_comparison = (x, y) => comparison.Invoke(x, y) == 0;
_getHashCode = getHashCode;
}
public CustomEqualityComparer(Comparison<T> comparison, Func<T, int> getHashCode)
{
if (comparison == null)
{
throw new ArgumentNullException("comparison");
}
if (getHashCode == null)
{
throw new ArgumentNullException("getHashCode");
}
_comparison = (x, y) => comparison.Invoke(x, y) == 0;
_getHashCode = getHashCode;
}
public bool Equals(T x, T y)
{
return _comparison.Invoke(x, y);
}
public int GetHashCode(T obj)
{
return _getHashCode.Invoke(obj);
}
}
}
#endif