比较对象的值相等

时间:2014-11-07 13:24:08

标签: vb.net oop object equality

我想知道比较2个复杂对象的最佳方法,以确定它们是否相等,即它们的属性是否相同?我尝试了序列化方法,但不确定为什么它们不等于值

    Dim stream As New MemoryStream()
    Dim bstream As New MemoryStream()

    Dim clientOne As Jewellery.ClientInfo = New Jewellery.ClientInfo(New Jewellery.Company("a", "", "", "b", "", "e"), New Jewellery.Customer("a", "b", "c", "d", "", "", "", "f"))
    Dim clientTwo As Jewellery.ClientInfo = New Jewellery.ClientInfo(New Jewellery.Company("a", "", "", "b", "", "e"), New Jewellery.Customer("a", "b", "c", "d", "", "", "", "f"))

    formatter.Serialize(stream, clientOne)
    formatter.Serialize(bstream, clientTwo)

    Dim streamOneBytes As Byte() = stream.ToArray()
    Dim streamTwoBytes As Byte() = bstream.ToArray()
    Dim streamToCompareBytes As Byte() = streamToCompare.ToArray()
    Dim i As Int16 = 0
    Dim flag As Boolean

    If streamOneBytes.Length <> streamTwoBytes.Length Then

        MsgBox("False")
        flag = False
    Else
        While i < streamOneBytes.Count

            If streamOneBytes(i) <> streamTwoBytes(i) Then
                flag = "False"                   
            Else : flag = "True"                    
            End If

            i = i + 1

        End While

    End If

正如您在上面的代码中看到的,当我初始化2个相同类型的对象并进行比较时,它可以正常工作。但是当我添加一个对象来说出一个List然后检索并与一个类似类型的对象进行比较时,它无法说两者的二进制宽度不同。有什么建议?感谢

2 个答案:

答案 0 :(得分:0)

我不确定我明白你想做什么。 所以我选择IComparable<T>

答案 1 :(得分:0)

public class IntegerPropertyEqualityCompare : IEqualityComparer<Main>
{
    #region IEqualityComparer<Main> Members

    public bool Equals( Main x, Main y )
    {
        return x.IntegerProperty == y.IntegerProperty;
    }

    public int GetHashCode( Main obj )
    {
        return obj.IntegerProperty.GetHashCode() ^ obj.StringProperty.GetHashCode();
    }

    #endregion
}

public class StringPropertyEqualityCompare : IEqualityComparer<Main>
{
    #region IEqualityComparer<Main> Members

    public bool Equals( Main x, Main y )
    {
        return x.StringProperty == y.StringProperty;
    }

    public int GetHashCode( Main obj )
    {
        return obj.IntegerProperty.GetHashCode() ^ obj.StringProperty.GetHashCode();
    }

    #endregion
}

public class AllPropertiesEqualityCompare : IEqualityComparer<Main>
{
    #region IEqualityComparer<Main> Members

    public bool Equals( Main x, Main y )
    {
        return ( x.IntegerProperty == y.IntegerProperty ) && ( x.StringProperty == y.StringProperty );
    }

    public int GetHashCode( Main obj )
    {
        return obj.IntegerProperty.GetHashCode() ^ obj.StringProperty.GetHashCode();
    }

    #endregion
}



public abstract class Main
{
    public int IntegerProperty { get; set; }
    public string StringProperty { get; set; }

    public bool Equals( IEqualityComparer<Main> comparer, Main other )
    {
        return comparer.Equals( this, other );
    }
}

public class ConcreteA : Main
{ }

public class ConcreteB : Main
{ }




class TemporaryTest
{
    public static void Run()
    {
        var a = new ConcreteA();

        a.IntegerProperty = 1;
        a.StringProperty = "some";

        var b = new ConcreteB();
        b.IntegerProperty = 1;
        a.StringProperty = "other";

        Console.WriteLine( a.Equals( new IntegerPropertyEqualityCompare(), b ) );
        Console.WriteLine( a.Equals( new StringPropertyEqualityCompare(), b ) );
        Console.WriteLine( a.Equals( new AllPropertiesEqualityCompare(), b ) );
    }
}

也许这样的事情呢?