/// <summary> /// Copies the object by value, only on the designated property or field names /// </summary> /// <param name="source">The source object.</param> /// <param name="dest">The destination object.</param> /// <param name="property_names">The property or field names to copy from source to destination.</param> /// <param name="onlyCopyValueTypes">if set to <c>true</c> [only copy value types].</param> /// <param name="doRecursiveCopy">if set to <c>true</c> [do recursive copy].</param> public virtual void CopyByValue(object source, object dest, IEnumerable <string> property_names, bool onlyCopyValueTypes = false, bool doRecursiveCopy = true) { Contract.Requires(source != null, "source is null."); Contract.Requires(dest != null, "dest is null."); Contract.Requires(property_names != null, "property_names is null."); Contract.Requires(source.IsInstanceOfType(ObjectType), "Reflected type does not match source object type"); Contract.Requires(dest.IsInstanceOfType(ObjectType), "Reflected type does not match dest object type"); foreach (string name in property_names) { object currentSourceValue = this.Getters[name](source); if (!onlyCopyValueTypes || currentSourceValue is ValueType) { object currentDestValue = this.Getters[name](dest); if (!(currentSourceValue is ValueType || currentSourceValue is string || currentSourceValue is DateTime) && doRecursiveCopy && currentSourceValue != null && currentDestValue != null) { var reflect = new ObjectFasterFlection(currentDestValue.GetType()); reflect.CopyByValue(currentDestValue, currentSourceValue, onlyCopyValueTypes, doRecursiveCopy); } else { this.Setters[name](dest, currentSourceValue); } } } }
/// <summary> /// Compares the the objects by value. /// </summary> /// <param name="comparedA">The compared A.</param> /// <param name="comparedB">The compared B.</param> /// <param name="property_names">The property names to compare, null to compare all.</param> /// <param name="onlyCompareValueTypes">if set to <c>true</c> [only compare value types].</param> /// <param name="dorecursiveComparison">if set to <c>true</c> [do recursive comparison].</param> /// <returns> /// true if equal, false otherwise /// </returns> public virtual bool CompareByValue(object comparedA, object comparedB, bool onlyCompareValueTypes = false, bool dorecursiveComparison = true, IEnumerable <string> property_names = null) { Contract.Requires(comparedA != null, "source is null."); Contract.Requires(comparedB != null, "dest is null."); Contract.Requires(comparedB.IsInstanceOfType(ObjectType)); Contract.Requires(comparedA.IsInstanceOfType(ObjectType)); bool equal = false; if (comparedA.Equals(comparedB)) { equal = true; } else { ICollection <MemberGetter> currentGetters = null; if (property_names == null) { currentGetters = this.Getters.Values; } else { // SMELL: Va tirar una excepci�n si se provee un nombre de propiedad inexistente currentGetters = property_names.Select(name => this.Getters[name]).ToArray(); } foreach (MemberGetter getter in currentGetters) { object valueA = getter(comparedA); object valueB = getter(comparedB); if (valueA != null && valueB != null && dorecursiveComparison && !(valueA is ValueType)) { var reflection = new ObjectFasterFlection(valueA.GetType()); if (!reflection.CompareByValue(valueA, valueB, onlyCompareValueTypes, dorecursiveComparison)) { equal = false; break; } } else if (!(valueA == null && valueB == null) && valueA != valueB) { equal = false; break; } } } return(equal); }