コード例 #1
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());
        }
コード例 #2
0
 public int Compare(T x, T y) =>
 Inner.Compare(y, x);