-3

I have scenario where -

  1. A C# WPF Model Object is Xml Serialized and stored in xml file.
  2. Later after some point of time, read xml file and de-serialize the object into a new variable
  3. Compare c# object in step 1 with c# object in step 2 In above scenario, these object equality returns false.

The object is complex one and hence, cannot compare each property for equality.

Note, - The WPF model object do not implement any equality related interfaces.

Var model = new TestViewModel();
XmlSerializer s = new XmlSerializer(typeOf(TestViewModel));
var xml = x.Serialize(model);
// Store xml in file
// some more code stuff
// some more code stuff
// some more code stuff
var newModel = x.Deserialize(xml);
If(model.Equals(newModel))
{
      // Do some stuff
}
else
{
    // do some other stuff
}
3
  • 3
    Can you show some code ? How are you Comparing the two objects ? Does your class implement any Interface for Equality ? IEquatable ? Commented May 4, 2016 at 6:22
  • 1
    Please include some more detail in your question and try to show what you have done so far. You tagged this xml, but does this have anything to do with XML? If so, and you are looking to compare XML files, see XNode.DeepEquals(). Or are you just looking for an automatic recursive equality comparer? If so, see maybe C# implementation of deep/recursive object comparison in .net 3.5 Commented May 4, 2016 at 6:30
  • Added the relevant code. Commented May 4, 2016 at 6:37

2 Answers 2

3

After the object is deserialized it has a new reference, so even if all properties are the same, the comparision in this case is made by reference and that's why you are getting false. You can do one of following:

  1. override Equals and GetHashCode and there perform your custom logic about when 2 objects with different references should be considered equal.

  2. implement IEquatable or IEqualityComparer base on your use cases

I'm not aware of your scenarios, but I consider the most straightforward way of solving your problem solution 1.

So basically you would do something like(inspired from MSDN):

class SerializingClass
{
    public readonly Complex1 a;
    public readonly Complex2 b;
    public readonly Complex3 c;

    public override bool Equals(System.Object obj)
    {

        if (obj == null || GetType() != obj.GetType()) 
            return false;
        SerializingClass p = obj as SerializingClass;

        // Return true if the fields match:
        return a.Id == p.A.Id && b.Id == p.B.Id && c.Id == p.C.Id;
    }

    public override int GetHashCode()
    {
        return a.Id ^ b.Id ^ c.Id;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Would be better if you added a sample for at least one of the suggestions. That would make the answer more complete.
1

ObjectsCompare framework could be useful in this case

It will allow you to avoid implementation of Equals and GetHashCode.

var comparer = new Comparer<TestViewModel>();
If(comparer.Compare(model, newModel))
{
      // Do some stuff
}
else
{
    // do some other stuff
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.