/// <summary>
        /// Recursively loops through the customization object hierarchy and creates a CustomizationComparison hierarchy.
        /// </summary>
        /// <param name="parent">The parent CustomizationComparison.</param>
        /// <param name="prop">The property that source and target are values of.</param>
        /// <param name="source">The source value.</param>
        /// <param name="target">The target value.</param>
        private void BuildComparisons(CustomizationComparison parent, PropertyInfo prop, object source, object target)
        {
            // Make sure at least one value is not null
            if (source != null || target != null)
            {
                // Extract the value types
                Type type = GetCommonType(source, target);

                // Don't continue if the types differ
                if (type == null)
                {
                    parent.IsDifferent = true;
                }
                else
                {
                    CustomizationComparison originalParent = parent;

                    // Determine if a new CustomizationComparison node should be created
                    if (type != typeof(ImportExportXml) && ComparisonTypeMap.IsTypeComparisonType(type))
                    {
                        string name = ComparisonTypeMap.GetComparisonTypeName(source, target);

                        parent = new CustomizationComparison(name, source, target);
                        parent.ParentProperty = prop;
                        originalParent.Children.Add(parent);
                    }

                    if (IsSimpleType(type))
                    {
                        // for simple types just compare values
                        if (!Object.Equals(source, target))
                        {
                            originalParent.IsDifferent = true;
                            parent.IsDifferent         = true;
                        }
                    }
                    else if (typeof(IEnumerable).IsAssignableFrom(type))
                    {
                        // Several arrays need to be sorted by a specific property (for example: Entity name)
                        originalParent.IsDifferent |= BuildArrayComparisonTypes(parent, prop, (IEnumerable)source, (IEnumerable)target);
                    }
                    else
                    {
                        // for classes, just compare each property
                        foreach (PropertyInfo p in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                        {
                            if (p.CanRead)
                            {
                                object sourceValue = source != null?p.GetValue(source, null) : null;

                                object targetValue = target != null?p.GetValue(target, null) : null;

                                BuildComparisons(parent, p, sourceValue, targetValue);
                                originalParent.IsDifferent |= parent.IsDifferent;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private string GetTypeName(CustomizationComparison self, string typeString)
        {
            object selfValue = self.SourceValue ?? self.TargetValue;

            if (selfValue is IIdentifiable[])
            {
                typeString = ComparisonTypeMap.GetComparisonTypeName(selfValue);
            }

            return(typeString);
        }
        public IEnumerable <String> SerializeObjectToLines(CustomizationComparison comparison, object item)
        {
            if (item != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.OmitXmlDeclaration = true;
                    settings.Indent             = true;

                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");

                    XmlAttributeOverrides overrides = ComparisonTypeMap.GetComparisonTypeXmlOverrides(item.GetType());

                    XmlSerializer serializer = new XmlSerializer(item.GetType(), overrides);

                    XmlFilter filter = new XmlFilter();
                    BuildXmlFilter(filter, item, comparison.ParentProperty);

                    using (FilteringXmlWriter writer = new FilteringXmlWriter(XmlWriter.Create(stream, settings)))
                    {
                        writer.Filter = filter;
                        serializer.Serialize(writer, item, ns);
                    }
                    stream.Seek(0, SeekOrigin.Begin);

                    using (StreamReader sourceReader = new StreamReader(stream))
                    {
                        string line;

                        while ((line = sourceReader.ReadLine()) != null)
                        {
                            yield return(line);
                        }
                    }
                }
            }
        }