mirror of
https://onedev.fprog.nl/AioNet
synced 2026-02-25 22:43:43 +01:00
Implemented comparison of IEnumerables in RecursiveFieldsEqual.
This commit is contained in:
parent
f46b4284a8
commit
39645bfe29
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
@ -36,7 +37,7 @@ namespace AioNet.Reflection
|
||||||
// If type is IEnumerable, compare individual elements.
|
// If type is IEnumerable, compare individual elements.
|
||||||
if (type.GetInterface("System.Collections.IEnumerable") != null)
|
if (type.GetInterface("System.Collections.IEnumerable") != null)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
return RecursiveEnumerableEquals(first, second);
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerable<FieldInfo> fields = type.GetRuntimeFields();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue