PropertiesComparer
Comparator for two instances of the same type, comparing each property.
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints.Comparers
{
internal static class PropertiesComparer
{
[System.Runtime.CompilerServices.NullableContext(1)]
public static EqualMethodResult Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
{
Type type = x.GetType();
Type type2 = y.GetType();
if (type != type2)
return EqualMethodResult.TypesNotSupported;
if (type.IsPrimitive)
return EqualMethodResult.TypesNotSupported;
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length == 0 || properties.Length >= 32 || properties.Any((PropertyInfo p) => p.GetIndexParameters().Length != 0))
return EqualMethodResult.TypesNotSupported;
ComparisonState state2 = state.PushComparison(x, y);
uint num = 0;
if (tolerance.HasVariance) {
for (int i = 0; i < properties.Length; i++) {
PropertyInfo propertyInfo = properties[i];
object value = propertyInfo.GetValue(x, null);
object value2 = propertyInfo.GetValue(y, null);
EqualMethodResult equalMethodResult = equalityComparer.AreEqual(value, value2, ref tolerance, state2);
switch (equalMethodResult) {
case EqualMethodResult.ComparedNotEqual:
return equalMethodResult;
case EqualMethodResult.ToleranceNotSupported:
num = (uint)((int)num | (1 << i));
break;
}
}
if (num == (uint)((1 << properties.Length) - 1))
return EqualMethodResult.ToleranceNotSupported;
} else
num = (uint)((1 << properties.Length) - 1);
if (num != 0) {
Tolerance tolerance2 = Tolerance.Exact;
for (int j = 0; j < properties.Length; j++) {
if (((int)num & (1 << j)) != 0) {
PropertyInfo propertyInfo2 = properties[j];
object value3 = propertyInfo2.GetValue(x, null);
object value4 = propertyInfo2.GetValue(y, null);
EqualMethodResult equalMethodResult2 = equalityComparer.AreEqual(value3, value4, ref tolerance2, state2);
if (equalMethodResult2 == EqualMethodResult.ComparedNotEqual)
return equalMethodResult2;
}
}
}
return EqualMethodResult.ComparedEqual;
}
}
}