[Dev Tip] Compare two objects

Sometime, you need to compare objects. Beside of override the Equal(), GetHashCode() methods, which are for some domain rules.

If you must compare all properties of the object, then I suggest another way to get it done: using JSON.

You can serialize those objects and compare JSON results.

public static void AreEqualByJson(object expected, object actual, string message = "")
var settings = new JsonSerializerSettings
PreserveReferencesHandling = PreserveReferencesHandling.All

var expectedJson = JsonConvert.SerializeObject(expected, settings);
var actualJson = JsonConvert.SerializeObject(actual, settings);
Assert.AreEqual(expectedJson, actualJson, message);