what is the best way to compare two objects and find the differences?
Customer a = new Customer(); Customer b = new Customer();
In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.
The Equals method and the IEquatable<T> interface could be used to know if two objects are equal but they won't allow you to know the differences between the objects. You could use reflection by comparing each property values.
One set of objects is the reference , and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method, it calls the ToString () methods of the input objects and compares the string results. You can provide one or more properties to be used for comparison.
One set of objects is the reference , and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method, it calls the ToString () methods of the input objects and compares the string results.
But, if you're looking for a simple solution without accessing the Reflection API, there is a library called java-object-diff. It can be used to get the differences between two objects even if the class is nested.
The equals () method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. The method parses a reference object as a parameter. It returns true if the objects are equal, else returns false.
One Flexible solution: You could use reflection to enumerate through all of the properties and determine which are and are not equal, then return some list of properties and both differing values.
Here's an example of some code that is a good start for what you are asking. It only looks at Field values right now, but you could add any number of other components for it to check through reflection. It's implemented using an extension method so all of your objects could use it.
TO USE
SomeCustomClass a = new SomeCustomClass(); SomeCustomClass b = new SomeCustomClass(); a.x = 100; List<Variance> rt = a.DetailedCompare(b);
My sample class to compare against
class SomeCustomClass { public int x = 12; public int y = 13; }
AND THE MEAT AND POTATOES
using System.Collections.Generic; using System.Reflection; static class extentions { public static List<Variance> DetailedCompare<T>(this T val1, T val2) { List<Variance> variances = new List<Variance>(); FieldInfo[] fi = val1.GetType().GetFields(); foreach (FieldInfo f in fi) { Variance v = new Variance(); v.Prop = f.Name; v.valA = f.GetValue(val1); v.valB = f.GetValue(val2); if (!Equals(v.valA, v.valB)) variances.Add(v); } return variances; } } class Variance { public string Prop { get; set; } public object valA { get; set; } public object valB { get; set; } }
The Equals
method and the IEquatable<T>
interface could be used to know if two objects are equal but they won't allow you to know the differences between the objects. You could use reflection by comparing each property values.
Yet another approach might consist into serializing those instances into some text format and compare the differences inside the resulting strings (XML, JSON, ...).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With