Skip to content
A CSharp (C#) diff library that allows you to diff two objects and get a list of the differences back.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
AnyDiff Added support for using First() syntax in the expressions (not recomm… May 23, 2019
.gitignore Initial commit Nov 16, 2018
LICENSE Initial commit Nov 16, 2018
README.md Update README.md May 22, 2019

README.md

AnyDiff

nuget nuget Build status Codacy Badge Codacy Badge

A CSharp (C#) diff library that allows you to diff two objects and get a list of the differences back.

Description

AnyDiff works with complex objects of any type, and is great for performing changeset tracking, auditing, or anything else that might require comparing differences between complex objects. Great when combined with AnyClone, as it lets you take a snapshot of the current state of an object and then compare it in the future for changes to it.

It can even do this for objects that are of different types, though the results may vary depending on how different they are.

Installation

Install AnyDiff from the Package Manager Console:

PM> Install-Package AnyDiff

Usage

Comparing two objects with no differences:

using AnyDiff;

var object1 = new MyComplexObject(1, "A string");
var object2 = new MyComplexObject(1, "A string");
var diff = AnyDiff.Diff(object1, object2);
Assert.AreEqual(diff.Count, 0);

Alternate extension syntax is also available (we will use this in further examples):

using AnyDiff.Extensions;

var object1 = new MyComplexObject(1, "A string");
var object2 = new MyComplexObject(1, "A string");
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 0);

Comparing two objects with a single expected change:

var object1 = new MyComplexObject(1, "A string");
var object2 = new MyComplexObject(1, "A different string");
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 1);

Comparing objects using custom Type Converters for proper delta detection:

public class MyComplexObject
{
  public int Id { get; private set; }

  /// <summary>
  /// Convert a formatted string as a TimeSpan
  /// </summary>
  [TypeConverter(typeof(TimeSpanConverter))]
  public string StartTime { get; set; }

  public TypeConverterObject(int id, string startTime)
  {
    Id = id;
    StartTime = startTime;
  }
}

var object1 = new MyComplexObject(1, "04:00:00");
var object2 = new MyComplexObject(1, "04:05:00");
var diff = object1.Diff(object2);
Assert.AreEqual(diff.Count, 1);
Assert.AreEqual(TimeSpan.FromMinutes(5), diff.First().Delta); // difference of 5 minutes

Ignoring Properties

Anydiff will ignore fields and properties decorated using attributes: [IgnoreDataMember], [NonSerialized], and [JsonIgnore]. In addition, you can specify properties to ignore using expression syntax. See Ignoring Properties and Fields for more details.

Ignoring by properties explicitly:

var object1 = new MyComplexObject(1, "A string", true);
var object2 = new MyComplexObject(2, "A different string", true);
var diff = object1.Diff(object2, x => x.Id, x => x.Name);
Assert.AreEqual(diff.Count, 0);

Inclusion of Properties

AnyDiff also supports processing of specific properties if you don't want to diff the entire object. This works using the same syntax as ignoring properties but passing a different ComparisonOptions. In the example below, only the properties Id and Name will be compared.

var object1 = new MyComplexObject(1, "A string", true);
var object2 = new MyComplexObject(2, "A different string", true);
var diff = object1.Diff(object2, ComparisonOptions.All | ComparisonOptions.IncludeList, x => x.Id, x => x.Name);
Assert.AreEqual(diff.Count, 0);

Comparing Unordered lists

If you wish to perform a diff that ignores the ordering of data in a collection/list, you can specify that behavior with a ComparisonOptions flag as seen below.

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 3, 2 };
var diff = list1.Diff(list2, ComparisonOptions.All | ComparisonOptions.AllowCollectionsToBeOutOfOrder);
Assert.AreEqual(diff.Count, 0);

Analyzing results

Viewing the results of a diff:

var diff = object1.Diff(object2);

foreach(var difference in diff)
{
  Console.Write($"Index: {difference.ArrayIndex}"); // when array elements differ in value
  Console.Write($"Delta: {difference.Delta}"); // when numbers, Dates, Timespans, strings differ in value
  Console.Write($"Left: {difference.LeftValue}"); // the left value being compared
  Console.Write($"Right: {difference.RightValue}"); // the right value being compared
  Console.Write($"Property name: {difference.Property}"); // the name of the field/property
  Console.Write($"Property type: {difference.PropertyType}"); // the type of the field/property
}

Scenarios supported

  • Circular references
  • Using TypeConverters to understand data types
  • Deltas on strings, DateTimes, TimeSpans, numeric types
  • Comparing arrays, collections, dictionaries
  • Comparing collections with different ordering
  • Complex objects, deep type inspection
  • Entity Framework objects
  • IEquatable support

Using with other libraries

Comparing the difference between the same object at different states, using AnyClone

using AnyDiff.Extensions;
using AnyClone;

var object1 = new MyComplexObject(1, "A string");
var object1Snapshot = object1.Clone();

var diff = object1.Diff(object1Snapshot);
Assert.AreEqual(diff.Count, 0);

// change something anywhere in the object tree
object1.Name = "A different string";

diff = object1.Diff(object1Snapshot);
Assert.AreEqual(diff.Count, 1);
You can’t perform that action at this time.