Пример #1
0
        public ComparisonResult CompareObjects(IComparisonContext context, object source, object destination)
        {
            PreparePropertyInfo(source, destination);

            foreach (var pair in propertyMap)
            {
                currentPair = pair;
                var sourceValue      = new Lazy <object>(() => currentPair.Source.Read(source));
                var destinationValue = new Lazy <object>(() => currentPair.Destination.Read(destination));

                if (IsPropertyIgnored())
                {
                    continue;
                }

                if (HandleMissingValues(context, destinationValue, sourceValue))
                {
                    continue;
                }

                var innerContext = context.VisitingProperty(currentPair.Name);
                results.Add(inner.Compare(innerContext, sourceValue.Value, destinationValue.Value));
            }

            return(results.ToResult());
        }
        public ComparisonResult CompareObjects(IComparisonContext context, object source, object destination)
        {
            PreparePropertyInfo(source, destination);

            foreach (var pair in propertyMap)
            {
                currentPair = pair;
                var sourceValue = new Lazy<object>(() => currentPair.Source.Read(source));
                var destinationValue = new Lazy<object>(() => currentPair.Destination.Read(destination));

                if (IsPropertyIgnored())
                {
                    continue;
                }

                if (HandleMissingValues(context, destinationValue, sourceValue))
                {
                    continue;
                }

                var innerContext = context.VisitingProperty(currentPair.Name);
                results.Add(inner.Compare(innerContext, sourceValue.Value, destinationValue.Value));
            }

            return results.ToResult();
        }
Пример #3
0
        public ComparisonResult Compare(IComparisonContext context, object value1, object value2)
        {
            var type1 = value1.GetType();
            var type2 = value2.GetType();

            var props1 = ReflectionCache.GetProperties(type1);
            var props2 = ReflectionCache.GetProperties(type2).ToDictionary(p => p.Name);

            var ignored = GetIgnoredPropertiesForTypes(type1, type2);

            var results = new List <ComparisonResult>();

            foreach (var propertyInfo1 in props1)
            {
                if (ignored.Contains(propertyInfo1.Name))
                {
                    props2.Remove(propertyInfo1.Name);
                    continue;
                }

                var propValue1 = propertyInfo1.GetValue(value1);

                if (!props2.ContainsKey(propertyInfo1.Name))
                {
                    if (!IgnoreUnmatchedProperties)
                    {
                        context.AddDifference(propValue1, "(missing)", propertyInfo1.Name);
                        results.Add(ComparisonResult.Fail);
                    }
                    continue;
                }

                var propertyInfo2 = props2[propertyInfo1.Name];
                var propValue2    = propertyInfo2.GetValue(value2);

                var innerContext = context.VisitingProperty(propertyInfo1.Name);
                results.Add(Inner.Compare(innerContext, propValue1, propValue2));

                props2.Remove(propertyInfo1.Name);
            }

            if (!IgnoreUnmatchedProperties && props2.Count > 0)
            {
                foreach (var p in props2)
                {
                    if (ignored.Contains(p.Key))
                    {
                        continue;
                    }

                    var v = p.Value.GetValue(value2);
                    context.AddDifference("(missing)", v, p.Key);
                    results.Add(ComparisonResult.Fail);
                }
            }

            return(results.ToResult());
        }
Пример #4
0
        public (ComparisonResult, IComparisonContext) CompareObjects(IComparisonContext context, object source, object destination)
        {
            PreparePropertyInfo(source, destination);

            var currentContext = context;

            foreach (var pair in propertyMap)
            {
                currentPair = pair;
                var sourceValue      = new Lazy <object>(() => currentPair.Source.Read(source));
                var destinationValue = new Lazy <object>(() => currentPair.Destination.Read(destination));

                if (IsPropertyIgnored())
                {
                    continue;
                }

                if (SourceAndDestinationPresent())
                {
                    var(result, innerContext) = inner.Compare(
                        context.VisitingProperty(currentPair.Name),
                        sourceValue.Value,
                        destinationValue.Value
                        );

                    results.Add(result);
                    currentContext = currentContext.MergeDifferencesFrom(innerContext);
                }
                else if (!ignoreUnmatchedProperties)
                {
                    if (currentPair.Source == null)
                    {
                        currentContext = currentContext.AddDifference("(missing)", destinationValue.Value, currentPair.Name);
                        results.Add(ComparisonResult.Fail);
                    }

                    if (currentPair.Destination == null)
                    {
                        currentContext = currentContext.AddDifference(sourceValue.Value, "(missing)", currentPair.Name);
                        results.Add(ComparisonResult.Fail);
                    }
                }
            }

            return(results.ToResult(), currentContext);
        }