/// <summary> /// Initializes an ObjectComparer that compares the two objects and defined mapped values supplied in /// the configuration. /// </summary> public ObjectComparer(Action <ObjectComparerConfiguration> configuration) { var config = new ObjectComparerConfiguration { Object1 = (T1)(object)null, Object2 = (T2)(object)null, Mappings = new List <ValuePair>() }; configuration.Invoke(config); Object1 = config.Object1; Object2 = config.Object2; Pairs.AddRange(config.Mappings.Select(pair => ValuePair.Create(pair.Prop1, pair.Prop2))); }
/// <summary> /// Initializes an ObjectComparer that compares all the field values of the two supplied objects /// </summary> /// <param name="object1"></param> /// <param name="object2"></param> public ObjectComparer(T1 object1, T2 object2) { Object1 = object1; Object2 = object2; var obj1Fields = object1.GetType().GetProperties().ToList(); var obj2Fields = object2.GetType().GetProperties().ToList(); foreach (var obj1Prop in obj1Fields) { var obj2Prop = obj2Fields.Where(p => p.Name == obj1Prop.Name).FirstOrDefault(); if (obj1Prop != null && obj2Prop != null) { Pairs.Add( ValuePair.Create( Object1.GetType().GetProperty(obj1Prop.Name).GetValue(Object1), Object2.GetType().GetProperty(obj2Prop.Name).GetValue(Object2) )); } } }