AutoCompare is a simple library with the goal of making it effortless to compare two objects of the same type to generate the list of modified properties.
The main goal of AutoCompare is making it easy to get differences between two objects of the same type. AutoCompare builds an Expression Tree the first time you compare a type so the first call is always slower. Once the Expression Tree is built and compiled, comparing two objects becomes extremely fast.
This library is not yet available on NuGet. For now, clone this repository, compile it in release and use the library by adding the dll as a reference.
- Works on any object type, and does deep compare (compares child objects)
- Reflection is only used the first time a type is compared, successive calls are executing a compiled lambda and are extremely fast
- Detects circular references so it won't throw a StackOverflowException if you have
Parent.Child.Parent
- Strongly typed fluent configuration using lambdas
- Supports
IEnumerable<>
andIDictionary<,>
properties, although some configuration might be necessary
Compare two objects
var updatedProperties = AutoCompare.Comparer.Compare<MyObjectType>(objA, objB);
Configure properties that shouldn't be compared
AutoCompare.Comparer.Configure<MyObjectType>()
.Ignore(x => x.IgnoredProperty)
.Ignore(x => x.AnotherIgnoredProperty);
Configure a dictionary property to perform a deep compare. Objects are compared by matching keys. By default, dictionaries are not deeply compared and the values are simply compared using the default equality comparer, even if the value is a complex type.
AutoCompare.Comparer.Configure<MyObjectType>()
.DeepCompare(x => x.DictionaryProperty);
Configure a IEnumerable (array, list, hashmap, etc.) property to perform a deep compare. To deeply compare lists, you must specify the property to be used as a key or ID.
AutoCompare.Comparer.Configure<MyObjectType>()
.Enumerable(x => x.ListProperty, x => x.DeepCompare(y => y.ID));
Calling Configure<Type>()
is optional and AutoCompare will default to comparing every property, without doing deep compare for enumerables and dictionaries.
Please note that you must call Configure<Type>()
only once per type, and call it before any call to Compare<Type>()
is made or it will throw an exception.
More examples can be found in AutoCompare.Tests
AutoCompare is Copyright © 2015 Steven Gilligan and other contributors under the MIT license.