Available on NuGet.
Install with the dotnet CLI: dotnet add package Indiko.Object.Comparison
, or through the NuGet Package Manager in Visual Studio.
This library is designed for comparing two objects of the same or different types. The main functionality is encapsulated in the following four classes:
ObjectExtensions
: Provides a static method that extendsobject
type for comparison.ComparisonReport
: Holds the result of the comparison between two objects.Difference
: Details the difference in a particular property between two objects.ObjectComparer
: Handles the core logic of comparing objects.
This static class contains a single extension method CompareTo
, which takes two objects, and an optional list of properties to ignore during comparison. The method returns a ComparisonReport
.
var report = obj1.CompareTo(obj2);
or
var report = obj1.CompareTo(obj2, new List<string>{"PropertyToIgnore"});
A class that contains the result of a comparison operation. It has two properties:
AreEqual
: A boolean indicating if the objects are equal.Differences
: A list ofDifference
objects indicating which properties differ between the two objects.
Represents a single difference between the compared objects. Holds information about:
PropertyName
: The property that differs.SourceValue
: The value of the property in the source object.DestinationValue
: The value of the property in the destination object.SourceType
: The type of the property in the source object.DestinationType
: The type of the property in the destination object.
This static class does the heavy lifting of object comparison. It has a static method Compare
that takes two generic objects and an optional list of properties to ignore.
The method returns a ComparisonReport
that can be further examined to understand how the two objects differ.
- Initialize a
ComparisonReport
object, settingAreEqual
totrue
. - Compare each property of the source object with the destination object.
- If a property is only present in one object or the values are different, update
ComparisonReport
accordingly.
This is mainly used internally by ObjectExtensions
, but can also be used directly.
var report = ObjectComparer.Compare(obj1, obj2);
or
var report = ObjectComparer.Compare(obj1, obj2, new List<string>{"PropertyToIgnore"});