Comparing dynamic objects in C#

前端 未结 6 2302
不知归路
不知归路 2021-02-13 04:08

What is the best way to compare two arbitrary dynamic objects for equality? For example these two objects.

I.e.

dynamic obj1 = new ExpandoObject();
obj1.         


        
6条回答
  •  失恋的感觉
    2021-02-13 04:22

    You can also use the ObjectsComparer library available on GitHub : ObjectsComparer

    This library is an object-to-object comparer that allows us to compare objects recursively member by member and to define custom comparison rules for certain properties, fields or types. It supports enumerables (arrays, collections, lists), multidimensional arrays, enumerations, flags and dynamic objects (ExpandoObject, DynamicObject and compiler generated dynamic objects).

    Go to Valerii Tereshchenko excellent paper for more details.

提交回复
热议问题