differences between two objects in C#

前端 未结 6 472
攒了一身酷
攒了一身酷 2021-01-02 18:29

I was wondering how I would find the difference between two objects of the same class. So if I had a Person class with the only difference being Age it will return the field

6条回答
  •  渐次进展
    2021-01-02 19:08

    You will need to recursively go through all private and public properties and fields on the entire object graph. Use a HashSet to keep track of objects you have already checked so you don't return duplicate results or get into a stack overflow.

    If the property type is IComparable, you can cast the values of that property to IComparable and use IComparable.CompareTo. If not, you'll have to recursively call the your differential method on the sub-objects.

提交回复
热议问题