[ACCEPTED]-How to compare properties between two objects-reflection
Accepted answer
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);
}
You may consider making the Person
class implement 1 the IComparable
interface and implementing the CompareTo(Object obj)
method.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.