Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two objects and find the differences [duplicate]

Tags:

c#

asp.net

what is the best way to compare two objects and find the differences?

Customer a = new Customer(); Customer b = new Customer(); 
like image 862
Nick Kahn Avatar asked Feb 09 '11 22:02

Nick Kahn


People also ask

How do you compare two objects?

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.

How do I find the difference between two objects in C#?

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.

How does compare-object compare two objects?

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.

What is the difference between reference and compare object?

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.

Is there a way to get the difference between two objects?

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.

How to compare the equality of two objects in Java?

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.


2 Answers

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; } } 
like image 74
deepee1 Avatar answered Oct 02 '22 17:10

deepee1


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, ...).

like image 26
Darin Dimitrov Avatar answered Oct 02 '22 16:10

Darin Dimitrov