比较两个复杂对象的最佳方式 [英] Best way to compare two complex object

查看:173
本文介绍了比较两个复杂对象的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 Object1 Object2的两种复杂的对象。 他们周围有5个级别的孩子的其他物品。

I have two complex objects like Object1 and Object2. They have around 5 levels of child's others objects.

我需要最快速的方法说,如果是相同的或没有。

I need the fastest method to say if are the same or not.

如何才能做到在C#4.0?

How it could be done in C# 4.0?

感谢您!

推荐答案

实施 IEquatable< T> (通常与覆盖继承对象一起.Equals 法)对所有的自定义类型。在复合类型的情况下,调用内包含的类型所包含的类型等于方法。这种做法显然需要你来扩展你的类型的定义,但其结果比系列化涉及任何通用的解决方案更快。

Implement IEquatable<T> (typically in conjunction with overriding the inherited Object.Equals method) on all your custom types. In the case of composite types, invoke the contained types’ Equals method within the containing types. This approach will obviously require you to extend your types’ definitions, but its results are faster than any generic solutions involving serialization.

修改:这是一个人为的例子有三个层次的嵌套。

Edit: Here is a contrived example with three levels of nesting.

有关值类型,通常可以只调用他们的等于方法。即使领域从未明确分配,他们仍然有一个默认值。

For value types, you can typically just call their Equals method. Even if the fields were never explicitly assigned, they would still have a default value.

有关引用类型,你应该先使用等于运算符, == ,通常检查引用相等 - 当你恰巧是这将成为一个效率提升引用同一对象。如果检查失败,请确认您的字段不为空(为了避免的NullReferenceException ),并调用它的等于方法。由于我们的字段类型正确,则 IEquatable&LT; T&GT; .Equals 方法被直接调用,绕过覆盖的Object.Equals 法(其执行会稍微慢由于类型转换)。

For reference types, you should first use the equality operator, ==, which typically checks for reference equality – this would serve as an efficiency boost when you happen to be referencing the same object. If that check fails, confirm that your field is not null (to avoid NullReferenceException) and call its Equals method. Since our fields are properly typed, the IEquatable<T>.Equals method gets called directly, bypassing the overridden Object.Equals method (whose execution would be marginally slower due to the type cast).

在覆盖的Object.Equals ,你也来覆盖 Object.GetHash code ;我没有为简明起见这样做以下。

When you override Object.Equals, you’re also expected to override Object.GetHashCode; I didn’t do so below for the sake of conciseness.

public class Person : IEquatable<Person>
{
    public int Age { get; set; }
    public string FirstName { get; set; }
    public Address Address { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Person);
    }

    public bool Equals(Person other)
    {
        if (other == null)
            return false;

        return this.Age.Equals(other.Age) &&
            (
                this.FirstName == other.FirstName ||
                this.FirstName != null &&
                this.FirstName.Equals(other.FirstName)
            ) &&
            (
                this.Address == other.Address ||
                this.Address != null &&
                this.Address.Equals(other.Address)
            );
    }
}

public class Address : IEquatable<Address>
{
    public int HouseNo { get; set; }
    public string Street { get; set; }
    public City City { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Address);
    }

    public bool Equals(Address other)
    {
        if (other == null)
            return false;

        return this.HouseNo.Equals(other.HouseNo) &&
            (
                this.Street == other.Street ||
                this.Street != null &&
                this.Street.Equals(other.Street)
            ) &&
            (
                this.City == other.City ||
                this.City != null &&
                this.City.Equals(other.City)
            );
    }
}

public class City : IEquatable<City>
{
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as City);
    }

    public bool Equals(City other)
    {
        if (other == null)
            return false;

        return
            this.Name == other.Name ||
            this.Name != null &&
            this.Name.Equals(other.Name);
    }
}

这篇关于比较两个复杂对象的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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