有没有更好的方式来implment等于与很多领域的对象?对象、领域、方式、implment

2023-09-03 04:21:57 作者:情到深处人孤独

又见Hows要快速检查,如果数据   传输两个对象具有相同   在C#中的属性?

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

我有很多数据传输对象(DTO),每包含的很多简单的领域。我需要实现equals对所有的人(这样我就可以写一些单元测试过运送他们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).

在code我现在用的就是:

The code I am using is:

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()来帮助他们进行克隆()的情况下,但我无法找到任何东西,以帮助等于。 我们将在充分信任的运行,这样的反映基础的解决方案是一个答案,但我宁愿不推倒重来。

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)

推荐答案

滑稽你应该问,我最近发表了一些code做这一点。看看我的 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-发光以产生在第一次运行整个Equals和GetHash code函数(一次为每种类型的使用)。它会比较每个字段使用默认的相等比较为该类型(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方法(例如,你不能用空的比较器自己的对象,除非你先盒装它的对象和很多的场外多个空有关的问题)。

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

我一直打算写篇博客文章,但还没有得到解决它呢。在code是一个有点无证,但如果你喜欢它,我可以清理一下。

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许可 meaining你可以做pretty的多少什么你想做的事情,包括专有解决方案使用它在不改变你授权了一下。

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.