ValeraT1982 / ObjectsComparer

C# Framework provides mechanism to compare complex objects, allows to override comparison rules for specific properties and types.
MIT License
352 stars 86 forks source link

Problem with list of dynamic object #1

Closed c4ntin closed 6 years ago

c4ntin commented 6 years ago

I try to compare two Json. They contain some list of dynamic object. I write a sample here: https://dotnetfiddle.net/ad0YCC What did I do wrong?

Thank you for your work.

ValeraT1982 commented 6 years ago

Sorry for the later answer. I was on vacation.

You don't need to use 'items[0]' and 'items[1]' to change field. I think it what you need.

var myObject = new { id = 123, name = "bob", items = new List<dynamic>() { new { pro1 = 123, pro2= "hello"}, new { pro1 = 123, pro2 = "world" } } };
dynamic myObjectBis1 = JsonConvert.DeserializeObject<ExpandoObject>(JsonConvert.SerializeObject(myObject));
dynamic myObjectBis2 = JsonConvert.DeserializeObject<ExpandoObject>(JsonConvert.SerializeObject(myObject));

myObjectBis2.pro2 = "Bye";
myObjectBis2.pro1 = "666";

var comparer = new Comparer(new ComparisonSettings { UseDefaultIfMemberNotExist = true });
IEnumerable<Difference> differences;
var isEqual = comparer.Compare(myObjectBis1, myObjectBis2, out differences);

Console.WriteLine(isEqual?"they are identical but it should not...":"they are different! Yeah!!!");