有没有更好的方法来弥补具有很多字段的对象的Equals? [英] Is there a better way to implment Equals for object with lots of fields?

查看:106
本文介绍了有没有更好的方法来弥补具有很多字段的对象的Equals?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


另见如何快速检查数据
是否传输两个对象在C#中具有相等的
属性?

我有很多数据传输对象(DTO),每个都包含很多简单字段。我需要实现所有的Equal(所以我可以编写一些单元测试来运输它们var WCF)。

I have lot of Data Transfer Objects (DTO) that each contains lots of simple fields. I need to implement Equals on all of them (so I can write some unit tests off transporting them var WCF).

我使用的代码是:

public override bool Equals(object rhs)
{

    RequestArguments other = rhs as RequestArguments;

    return
       other != null && 
       other.m_RequestId.Equals(RequestId) && 
       other.m_Type.Equals(m_Type) && 
       other.m_Parameters.Equals(m_Parameters) && 
       other.m_user.Equals(m_user);
}

必须有更好的方式... (列出所有字段都是要求错误和维护问题)

例如我们有对象。 MemberwiseClone()来帮助克隆()的情况,但是我找不到任何有助于Equals的东西。
我们完全信任,所以基于反射的解决方案是一个答案,但我不是重塑轮子。

E.g. we have Object. MemberwiseClone() to help with the Cloning() case, but I cannot find anything to help with Equals. We are running in full trust so a reflection based solution is one answer, but I rather not reinvent the wheel.

(对不起,我们不会从特定于域的语言生成DTO,否则这样的事情会很容易!我也无法将构建系统更改为添加另一个步骤)

(Sorry we don’t generate the DTO from a domain-specific language otherwise this sort of thing would be easy! Also I am not able to change the build system to add another step)

推荐答案

有趣的是你应该问,最近我发布了一些代码来做这个。查看我的 MemberwiseEqualityComparer ,查看是否符合您的需求。

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

这真的很容易使用,效率也非常高。它使用IL-emit在第一次运行时生成整个Equals和GetHashCode函数(对于每个使用的类型一次)。它将使用该类型的默认等式比较器(EqualityComparer.Default)来比较给定对象的每个字段(私有或公开)。我们已经在生产中使用了一段时间,看起来很稳定,但是我不会保证= =

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 =)

它照顾所有那些pescy边缘案例你很少会想到当你滚动自己的equals方法(即,你不能用null来比较自己的对象,除非你先把它放在一个对象中,而且很多关于更多的null相关的问题)。

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);
}

MemberwiseEqualityComparer发布在 MIT许可证,你可以做任何你想要的任何事情,包括在专有解决方案中使用它,而不改变你的许可。

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.

这篇关于有没有更好的方法来弥补具有很多字段的对象的Equals?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆