Recursive object comparer for .Net.
ObjectComparer.Equals(1, 2);
var x = new { Name = "John" };
var y = new { Name = "Jane" };
ObjectComparer.Equals(x, y);
var x = new[] { 1, 2 };
var y = new[] { 2, 3 };
ObjectComparer.Equals(x, y);
Each object pair will be compared according to the following order of execution.
-
Value type check
Objects are equal if the type is a value type and an equal check (==) returns true;
-
Null value check
Objects are equal if both references are null.
Objects are not equal if one of the references is null. -
ReferenceEquals check
Objects are equal if
Object.ReferenceEquals(x1, x2)
returns true. -
Visited objects check
Ensures that the object pair has not already been evaluated to prevent infinite loops which will lead to stack overflows.
class X { public Y Y { get; set; } } class Y { public X X { get; set; } }
The following code will work because the combination of x1 and x2 has already been evaluated when x.Y.X is reached.
var x1 = new X { Y = new Y() }; var x2 = new X { Y = new Y() }; x1.Y.X = x1; x2.Y.X = x2; ObjectComparer.Equals(x1, x2);
More complicated relationships will work the same way; x1.Y.X is the same object as x2.X.Y.X.Y.X.
var x1 = new X { Y = new Y() }; var x2 = new X { Y = new Y { X = new X { Y = new Y() } } }; x1.Y.X = x1; x2.Y.X.Y.X = x2; ObjectComparer.Equals(x1, x2);
-
IEquatable<T> check
Objects are equal if the type implements
IEquatable<T>
andx1.Equals(x2)
returns true. -
ICollection<T> check
Objects are not equal if the type implements
ICollection<T>
andx1.Count != x2.Count
. -
IEnumerable<T> check
Objects are equal if the type implements
IEbumerable<T>
, all the items are equal and the enumerables are of the same length. -
Member check
Compares public properties, protected properties and private fields according to specified settings.
Each member pair will be compared from the top of this list.
-
PrivateFields
Specify true if private fields should be included in the member check.
Default values is false. -
ProtectedProperties
Specify true if protected properties should be included in the member check.
Default values is false. -
PublicProperties
Specify true if public properties should be included in the member check.
Default values is true. -
UseEquatable
Specify true if IEquatable should be used if types implements it.
Default values is true.
Feel free to send a mail to objectcompare@ohnoes.eu with questions or comments.