Implemented comparison of IEnumerables in RecursiveFieldsEqual.

This commit is contained in:
Filip Strajnar 2024-05-11 15:17:41 +02:00
parent f46b4284a8
commit 39645bfe29

View file

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@ -36,7 +37,7 @@ namespace AioNet.Reflection
// If type is IEnumerable, compare individual elements.
if (type.GetInterface("System.Collections.IEnumerable") != null)
{
throw new NotImplementedException();
return RecursiveEnumerableEquals(first, second);
}
IEnumerable<FieldInfo> fields = type.GetRuntimeFields();
@ -50,5 +51,53 @@ namespace AioNet.Reflection
);
});
}
private static bool RecursiveEnumerableEquals<T>(T first, T second)
{
// Throwing exceptions if casting to System.Collections.IEnumerable doesn't work.
IEnumerator firstEnumerator = ((IEnumerable)first).GetEnumerator();
IEnumerator secondEnumerator = ((IEnumerable)second).GetEnumerator();
while (firstEnumerator.MoveNext())
{
// If the second enumerator doesn't advance when first does,
// this means that first enumerator has more elements, so they
// can't be equal.
if (!secondEnumerator.MoveNext())
{
return false;
}
Type firstType = firstEnumerator.Current.GetType();
Type secondType = secondEnumerator.Current.GetType();
// If types don't match, they can't be equal.
if (!(firstType == secondType))
{
return false;
}
bool elementsEqual = RecursiveFieldsEqual(
firstEnumerator.Current,
secondEnumerator.Current,
firstType
);
// If the two elements are not equal, then the entire enumerable is not equal.
if (!elementsEqual)
{
return false;
}
}
// If second enumerator can move even though first can't, it's
// longer and therefore the two are not equal.
if (secondEnumerator.MoveNext())
{
return false;
}
// If there were no differences found, we return true.
return true;
}
}
}