NUnitComparer
NUnitComparer encapsulates NUnit's default behavior
in comparing two objects.
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
public sealed class NUnitComparer : IComparer
{
[System.Runtime.CompilerServices.Nullable(1)]
public static NUnitComparer Default {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return new NUnitComparer();
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
public int Compare(object x, object y)
{
if (x == null) {
if (y != null)
return -1;
return 0;
}
if (y == null)
return 1;
if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y))
return Numerics.Compare(x, y);
Type type = x.GetType();
Type type2 = y.GetType();
MethodInfo method = type.GetMethod("CompareTo", new Type[1] {
type2
});
if ((object)method != null && !<Compare>g__IsIComparable|2_0(method))
return (int)method.Invoke(x, new object[1] {
y
});
method = type2.GetMethod("CompareTo", new Type[1] {
type
});
if ((object)method != null && !<Compare>g__IsIComparable|2_0(method))
return -(int)method.Invoke(y, new object[1] {
x
});
IComparable comparable = x as IComparable;
if (comparable != null)
return comparable.CompareTo(y);
IComparable comparable2 = y as IComparable;
if (comparable2 != null)
return -comparable2.CompareTo(x);
throw new ArgumentException("Neither value implements IComparable or IComparable<T>");
}
}
}