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

后端 未结 4 2044
醉梦人生
醉梦人生 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:25

    Edit: sorry, I didn't notice that you are asking for serialization testing. So this approach definitely doesn't work for you.


    There is another "dirty" way. If your object is serializable anyway, you can serialize them and compare the resulting streams.

    This is rather slow, but should be quite reliable and easy to implement.

    We are doing this sometimes to check if someone changed any data in an editor.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-15 09:38

    Funny you should ask, I recently published some code for doing exactly that. Check out my MemberwiseEqualityComparer to see if it fits your needs.

    It's really easy to use and quite efficient too. It uses IL-emit to generate the entire Equals and GetHashCode function on the first run (once for each type used). It will compare each field (private or public) of the given object using the default equality comparer for that type (EqualityComparer.Default). We've been using it in production for a while and it seems stable but I'll leave no guarantees =)

    It takes care of all those pescy edge-cases that you rarely think of when you're rolling your own equals method (ie, you can't comparer your own object with null unless you've boxed it in an object first and lot's off more null-related issues).

    I've been meaning to write a blog post about it but haven't gotten around to it yet. The code is a bit undocumented but if you like it I could clean it up a bit.

    public override int GetHashCode()
    {
        return MemberwiseEqualityComparer<Foo>.Default.GetHashCode(this);
    }
    
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
    
        return Equals(obj as Foo);
    }
    
    public override bool Equals(Foo other)
    {
        return MemberwiseEqualityComparer<Foo>.Default.Equals(this, other);
    }
    

    The MemberwiseEqualityComparer is released under the MIT license meaining you can do pretty much whatever you want with it, including using it in proprietary solutions without changing you licensing a bit.

    0 讨论(0)
  • 2021-01-15 09:40

    You can have a concept of an object hash - whenever an object changes, you pay the price of updating the hash (where the hash is literally a hash of all concatenated properties). Then, if you have a bunch of objects that rarely change, it's really cheap to compare them. The price, of course, is then paid at object editing time.

    0 讨论(0)
提交回复
热议问题