public static void CompareRecursively(object originalData, object deserializedData, bool approxComparisonForFloatingPointAnd64BitValues = false) { ComparisionType cmpType = ComparisionType.DCS; SerializationMechanism att = ComparisonHelper.GetSerializationMechanism(originalData); if (att.Equals(SerializationMechanism.POCO)) { cmpType = ComparisionType.POCO; } ComparisonHelper.CompareData(originalData, deserializedData, att, cmpType); }
/// <summary> /// Iterates through the properties and invokes compare method /// </summary> /// <param name="originalData"></param> /// <param name="deserializedData"></param> /// <param name="containerTypeAttribute"></param> private static void CompareProperties(object originalData, object deserializedData, SerializationMechanism containerTypeAttribute, ComparisionType cmpType) { BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; //Include private fields for DCS types if (cmpType.Equals(ComparisionType.DCS)) { flag = flag | BindingFlags.NonPublic; } foreach (System.Reflection.PropertyInfo property in originalData.GetType().GetProperties(flag)) { object newData = property.GetValue(originalData, null); SerializationMechanism fieldAttribute = ComparisonHelper.GetSerializationMechanism(newData); if (cmpType.Equals(ComparisionType.DCS)) { if (containerTypeAttribute.Equals(SerializationMechanism.DataContractAttribute)) { if ( (property.GetCustomAttributes(typeof(DataMemberAttribute), false).Length > 0) || (property.GetCustomAttributes(typeof(EnumMemberAttribute), false).Length > 0) ) { //Pass attribute of the complex type for furthur evaluation if (IsComplexType(newData)) { CompareData(newData, property.GetValue(deserializedData, null), fieldAttribute, cmpType); } else //Is a simple type { CompareData(newData, property.GetValue(deserializedData, null), containerTypeAttribute, cmpType); } } } else if (containerTypeAttribute.Equals(SerializationMechanism.SerializableAttribute)) { if (property.GetCustomAttributes(typeof(NonSerializedAttribute), false).Length == 0) { //Pass attribute of the complex type for furthur evaluation if (IsComplexType(newData)) { CompareData(newData, property.GetValue(deserializedData, null), fieldAttribute, cmpType); } else //Is a simple type, so pass Parents attribute { CompareData(newData, property.GetValue(deserializedData, null), containerTypeAttribute, cmpType); } } } } else if (cmpType.Equals(ComparisionType.POCO)) { //Ignore member with [IgnoreDataMember] attribute on a POCO type if (property.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), false).Length == 0) { //On POCO types, Properties which have both getter and setter will be serialized otherwise ignored if (property.CanRead && property.CanWrite) { //Pass attribute of the complex type for furthur evaluation if (IsComplexType(newData)) { CompareData(newData, property.GetValue(deserializedData, null), fieldAttribute, cmpType); } else //Is a simple type, so pass Parents attribute { CompareData(newData, property.GetValue(deserializedData, null), containerTypeAttribute, cmpType); } } else if (property.CanRead && !property.CanWrite) //Get-Only collection { //Pass attribute of the complex type for furthur evaluation if (IsComplexType(newData)) { CompareData(newData, property.GetValue(deserializedData, null), fieldAttribute, cmpType); } else //Is a simple type, so pass Parents attribute { CompareData(newData, property.GetValue(deserializedData, null), containerTypeAttribute, cmpType); } } } } } }