Skip to content

Type Conversion

Michael Brown edited this page Dec 1, 2018 · 1 revision

AnyDiff supports using TypeConverters to perform differences on objects formatted differently than the type indicates. An example would be storing a TimeSpan value as a string and detecting the correct type of delta to compute:

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