Is there a better way to implment Equals for object with lots of fields?

后端 未结 4 2042
醉梦人生
醉梦人生 2021-01-15 09:15

see also Hows to quick check if data transfer two objects have equal properties in C#?

I have lot of Data Transfer Objects (DTO) tha

4条回答
  •  时光说笑
    2021-01-15 09:35

    An option is to use reflection to get all the available fields and then get and compare their values on the desired objects. This would give you a generic solution but you would have quite a work to do, probably using hashes as Alex suggests is a cleaner solution.

    EDIT: Here is a simple example of comparing objects using reflection, it looks at properties instead of fields but you get the idea: http://www.willasrari.com/blog/use-systemreflection-for-comparing-custom-objects/000257.aspx

提交回复
热议问题