Exemple #1
0
        private static bool IsMemberMarkedWithEnumMember(object data, ComparisonType cmpType)
        {
            bool isEnumMember = false;
            //Non-public members are not serialized for POCO types
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            if (cmpType.Equals(ComparisonType.DCS))
            {
                flag = flag | BindingFlags.NonPublic;
            }

            FieldInfo info = data.GetType().GetField(data.ToString(), flag);

            if (null != info)
            {
                isEnumMember = info.GetCustomAttributes(typeof(EnumMemberAttribute), false).Length > 0;
            }
            return(isEnumMember);
        }
Exemple #2
0
        private static void CompareFields(object originalData, object deserializedData, SerializationMechanism containerTypeAttribute, ComparisonType cmpType)
        {
            //Compare fields
            //Non-public members are not serialized for POCO types
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            if (cmpType.Equals(ComparisonType.DCS))
            {
                flag = flag | BindingFlags.NonPublic;
            }

            foreach (System.Reflection.FieldInfo field in originalData.GetType().GetFields(flag))
            {
                object newData = field.GetValue(originalData);
                SerializationMechanism fieldAttribute = GetSerializationMechanism(newData);
                if (cmpType.Equals(ComparisonType.DCS))
                {
                    if (containerTypeAttribute.Equals(SerializationMechanism.DataContractAttribute))
                    {
                        if (
                            (field.GetCustomAttributes(typeof(DataMemberAttribute), false).Length > 0)
                            ||
                            (field.GetCustomAttributes(typeof(EnumMemberAttribute), false).Length > 0)
                            )
                        {
                            //Pass attribute of the complex type for furthur evaluation
                            if (ComparisonHelper.IsComplexType(newData))
                            {
                                ComparisonHelper.CompareData(field.GetValue(originalData), field.GetValue(deserializedData), fieldAttribute, cmpType);
                            }
                            else //Is a simple type
                            {
                                ComparisonHelper.CompareData(field.GetValue(originalData), field.GetValue(deserializedData), containerTypeAttribute, cmpType);
                            }
                        }
                    }
                    else if (containerTypeAttribute.Equals(SerializationMechanism.SerializableAttribute))
                    {
                        //Do not compare [NonSerialized] members
                        if (!field.IsNotSerialized)
                        {
                            if (ComparisonHelper.IsComplexType(newData))
                            {
                                ComparisonHelper.CompareData(field.GetValue(originalData), field.GetValue(deserializedData), fieldAttribute, cmpType);
                            }
                            else //Is a simple type
                            {
                                ComparisonHelper.CompareData(field.GetValue(originalData), field.GetValue(deserializedData), containerTypeAttribute, cmpType);
                            }
                        }
                    }
                }
                else if (cmpType.Equals(ComparisonType.POCO))
                {
                    //ReadOnly fields should be ignored for POCO type
                    //Ignore member with [IgnoreDataMember] attribute on a POCO type
                    if ((!field.IsInitOnly) && (field.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), false).Length == 0))
                    {
                        if (ComparisonHelper.IsComplexType(newData))
                        {
                            ComparisonHelper.CompareData(field.GetValue(originalData), field.GetValue(deserializedData), fieldAttribute, cmpType);
                        }
                        else //Is a simple type
                        {
                            ComparisonHelper.CompareData(field.GetValue(originalData), field.GetValue(deserializedData), containerTypeAttribute, cmpType);
                        }
                    }
                }
            }
        }
Exemple #3
0
        /// <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, ComparisonType cmpType)
        {
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            //Include private fields for DCS types
            if (cmpType.Equals(ComparisonType.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(ComparisonType.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(ComparisonType.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);
                            }
                        }
                    }
                }
            }
        }