[ACCEPTED]-How to compare properties between two objects-reflection

Accepted answer
Score: 12

If you want to stick with comparison via 6 reflection you should not use != (reference 5 equality which will fail most of comparisons 4 for boxed results of GetProperty calls) but 3 instead use static Object.Equals method.

Sample how to use Equals method 2 to compare two object in your reflection 1 code.

 if (!Object.Equals(
     item.GetValue(person, null),
     dto.GetType().GetProperty(item.Name).GetValue(dto, null)))
 { 
   diffProperties.Add(item);
 } 
Score: 1

You may consider making the Person class implement 1 the IComparable interface and implementing the CompareTo(Object obj) method.

Score: 0

Implement IEquatable interface

0

More Related questions