在.NET 3.5中实现C#的深度/递归对象比较

48
我正在寻找一个特定于C#的,开源(或提供源代码)的递归或深度对象比较实现。
我目前有两个包含活动对象的图形,我希望将它们相互比较,并获得在图形中不一致的一组差异结果。这些对象是一组在运行时间已知的类的实例化(但不一定在编译时间已知)。
有一个具体要求,需要能够从图形中的差异映射回包含差异的对象。

我知道这对你没什么帮助,但我写了一个相当复杂的东西。我想把它开源,但必须先问问我的老板。我还有一些其他很酷的东西,比如一个对象创建器。它可以直接使用,但也可以通过流畅的语法进行详细配置。但我在说什么呢...恐怕你在接下来的几周内无法得到它。 - Stefan Steinegger
6个回答

45

我在www.kellermansoftware.com发现了一个非常好用且免费的实现,名为 Compare .NET Objects,可以在这里找到。强烈推荐。


该工具似乎已经迁移到 GitHub,最新版本可在此处获取。


这些解决方案中是否有任何检查循环引用的方法? - digitguy
是的。比较.NET对象可以做到。 - Greg Finzer
CompareNETObjects看起来是这样的,但它没有利用表达式树(或其类似物)来使比较更快。在某些情况下,仅使用反射可能不可接受。 - nawfal

18

这是一个复杂的领域;我曾经在运行时做过类似的事情,但很快就变得混乱了。如果可能的话,您会发现最简单的方法是将对象序列化并比较序列化形式(例如使用xml-diff和XmlSerializer)。由于类型直到运行时才知道,这使事情有点复杂,但不是特别困难(您总可以使用new XmlSerializer(obj.GetType())等)。

无论如何,这将是我的默认方法。


1
这真的很聪明;我之前从未考虑过在比较之前对它们进行序列化。无论如何,我并不指望这会变得美观。根据我的需求,我将不得不构建一个包含所有冲突对象的结构。我认为从 XML-Diff 输出中实现这一点并不容易。 - blueberryfields
2
这是 C#(和 Java)中最让我感到沮丧的部分。当在 C++ 中进行简单的深度比较时,只需要自然地完成,而在 C# 中则需要使用这些繁琐复杂的系统。 - v.oddou
@v.oddou,考虑到多个指针链接的对象,C++在哪些方面使这更容易? - Marc Gravell
@MarcGravell:我认为由于编译器提供的复制构造函数和赋值运算符,这变得更加容易。使用原始指针的情况越来越少,现在通常使用定义明确的语义指针,如unique_ptr、shared_ptr、value_ptr。我使用零规则来最大化编译器的利用并最小化编写的代码。http://flamingdangerzone.com/cxx11/2012/08/15/rule-of-zero.html - v.oddou
3
@v.oddou的赋值不是比较。为什么你指责我心胸狭窄,而我却积极地要求你解释呢? - Marc Gravell
显示剩余7条评论

9
这是我们用于比较复杂图形的NUnit 2.4.6自定义约束条件。它支持嵌套集合,父引用,为数字比较设置公差,识别要忽略的字段名称(即使在层次结构深处),以及装饰类型始终被忽略。
我相信这段代码可以适应NUnit之外的使用场景,因为大部分代码不依赖于NUnit。
我们在数千个单元测试中使用它。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.Constraints;

namespace Tests
{
    public class ContentsEqualConstraint : Constraint
    {
        private readonly object expected;
        private Constraint failedEquality;
        private string expectedDescription;
        private string actualDescription;

        private readonly Stack<string> typePath = new Stack<string>();
        private string typePathExpanded;

        private readonly HashSet<string> _ignoredNames = new HashSet<string>();
        private readonly HashSet<Type> _ignoredTypes = new HashSet<Type>();
        private readonly LinkedList<Type> _ignoredInterfaces = new LinkedList<Type>();
        private readonly LinkedList<string> _ignoredSuffixes = new LinkedList<string>();
        private readonly IDictionary<Type, Func<object, object, bool>> _predicates = new Dictionary<Type, Func<object, object, bool>>();

        private bool _withoutSort;
        private int _maxRecursion = int.MaxValue;

        private readonly HashSet<VisitedComparison> _visitedObjects = new HashSet<VisitedComparison>();

        private static readonly HashSet<string> _globallyIgnoredNames = new HashSet<string>();
        private static readonly HashSet<Type> _globallyIgnoredTypes = new HashSet<Type>();
        private static readonly LinkedList<Type> _globallyIgnoredInterfaces = new LinkedList<Type>();

        private static object _regionalTolerance;

        public ContentsEqualConstraint(object expectedValue)
        {
            expected = expectedValue;
        }

        public ContentsEqualConstraint Comparing<T>(Func<T, T, bool> predicate)
        {
            Type t = typeof (T);

            if (predicate == null)
            {
                _predicates.Remove(t);
            }
            else
            {
                _predicates[t] = (x, y) => predicate((T) x, (T) y);
            }
            return this;
        }

        public ContentsEqualConstraint Ignoring(string fieldName)
        {
            _ignoredNames.Add(fieldName);
            return this;
        }

        public ContentsEqualConstraint Ignoring(Type fieldType)
        {
            if (fieldType.IsInterface)
            {
                _ignoredInterfaces.AddFirst(fieldType);
            }
            else
            {
                _ignoredTypes.Add(fieldType);
            }
            return this;
        }

        public ContentsEqualConstraint IgnoringSuffix(string suffix)
        {
            if (string.IsNullOrEmpty(suffix))
            {
                throw new ArgumentNullException("suffix");
            }
            _ignoredSuffixes.AddLast(suffix);
            return this;
        }

        public ContentsEqualConstraint WithoutSort()
        {
            _withoutSort = true;
            return this;
        }

        public ContentsEqualConstraint RecursingOnly(int levels)
        {
            _maxRecursion = levels;
            return this;
        }

        public static void GlobalIgnore(string fieldName)
        {
            _globallyIgnoredNames.Add(fieldName);
        }

        public static void GlobalIgnore(Type fieldType)
        {
            if (fieldType.IsInterface)
            {
                _globallyIgnoredInterfaces.AddFirst(fieldType);
            }
            else
            {
                _globallyIgnoredTypes.Add(fieldType);
            }
        }

        public static IDisposable RegionalIgnore(string fieldName)
        {
            return new RegionalIgnoreTracker(fieldName);
        }

        public static IDisposable RegionalIgnore(Type fieldType)
        {
            return new RegionalIgnoreTracker(fieldType);
        }

        public static IDisposable RegionalWithin(object tolerance)
        {
            return new RegionalWithinTracker(tolerance);
        }

        public override bool Matches(object actualValue)
        {
            typePathExpanded = null;
            actual = actualValue;
            return Matches(expected, actualValue);
        }

        private bool Matches(object expectedValue, object actualValue)
        {

            bool matches = true;

            if (!MatchesNull(expectedValue, actualValue, ref matches))
            {
                return matches;
            }
            // DatesEqualConstraint supports tolerance in dates but works as equal constraint for everything else
            Constraint eq = new DatesEqualConstraint(expectedValue).Within(tolerance ?? _regionalTolerance);
            if (eq.Matches(actualValue))
            {
                return true;
            }

            if (MatchesVisited(expectedValue, actualValue, ref matches))
            {
                if (MatchesDictionary(expectedValue, actualValue, ref matches) &&
                    MatchesList(expectedValue, actualValue, ref matches) &&
                    MatchesType(expectedValue, actualValue, ref matches) &&
                    MatchesPredicate(expectedValue, actualValue, ref matches))
                {
                    MatchesFields(expectedValue, actualValue, eq, ref matches);
                }
            }

            return matches;
        }

        private bool MatchesNull(object expectedValue, object actualValue, ref bool matches)
        {
            if (IsNullEquivalent(expectedValue))
            {
                expectedValue = null;
            }

            if (IsNullEquivalent(actualValue))
            {
                actualValue = null;
            }

            if (expectedValue == null && actualValue == null)
            {
                matches = true;
                return false;
            }

            if (expectedValue == null)
            {
                expectedDescription = "null";
                actualDescription = "NOT null";
                matches = Failure;
                return false;
            }

            if (actualValue == null)
            {
                expectedDescription = "not null";
                actualDescription = "null";
                matches = Failure;
                return false;
            }

            return true;
        }

        private bool MatchesType(object expectedValue, object actualValue, ref bool matches)
        {
            Type expectedType = expectedValue.GetType();
            Type actualType = actualValue.GetType();

            if (expectedType != actualType)
            {
                try
                {
                    Convert.ChangeType(actualValue, expectedType);
                }
                catch(InvalidCastException)             
                {
                    expectedDescription = expectedType.FullName;
                    actualDescription = actualType.FullName;
                    matches = Failure;
                    return false;
                }

            }
            return true;
        }

        private bool MatchesPredicate(object expectedValue, object actualValue, ref bool matches)
        {
            Type t = expectedValue.GetType();
            Func<object, object, bool> predicate;

            if (_predicates.TryGetValue(t, out predicate))
            {
                matches = predicate(expectedValue, actualValue);
                return false;
            }
            return true;
        }

        private bool MatchesVisited(object expectedValue, object actualValue, ref bool matches)
        {
            var c = new VisitedComparison(expectedValue, actualValue);

            if (_visitedObjects.Contains(c))
            {
                matches = true;
                return false;
            }

            _visitedObjects.Add(c);

            return true;
        }

        private bool MatchesDictionary(object expectedValue, object actualValue, ref bool matches)
        {
            if (expectedValue is IDictionary && actualValue is IDictionary)
            {
                var expectedDictionary = (IDictionary)expectedValue;
                var actualDictionary = (IDictionary)actualValue;

                if (expectedDictionary.Count != actualDictionary.Count)
                {
                    expectedDescription = expectedDictionary.Count + " item dictionary";
                    actualDescription = actualDictionary.Count + " item dictionary";
                    matches = Failure;
                    return false;
                }

                foreach (DictionaryEntry expectedEntry in expectedDictionary)
                {
                    if (!actualDictionary.Contains(expectedEntry.Key))
                    {
                        expectedDescription = expectedEntry.Key + " exists";
                        actualDescription = expectedEntry.Key + " does not exist";
                        matches = Failure;
                        return false;
                    }
                    if (CanRecurseFurther)
                    {
                        typePath.Push(expectedEntry.Key.ToString());
                        if (!Matches(expectedEntry.Value, actualDictionary[expectedEntry.Key]))
                        {
                            matches = Failure;
                            return false;
                        }
                        typePath.Pop();
                    }
                }
                matches = true;
                return false;
            }
            return true;
        }

        private bool MatchesList(object expectedValue, object actualValue, ref bool matches)
        {
            if (!(expectedValue is IList && actualValue is IList))
            {
                return true;
            }

            var expectedList = (IList) expectedValue;
            var actualList = (IList) actualValue;

            if (!Matches(expectedList.Count, actualList.Count))
            {
                matches = false;
            }
            else
            {
                if (CanRecurseFurther)
                {
                    int max = expectedList.Count;

                    if (max != 0 && !_withoutSort)
                    {
                        SafeSort(expectedList);
                        SafeSort(actualList);
                    }

                    for (int i = 0; i < max; i++)
                    {
                        typePath.Push(i.ToString());

                        if (!Matches(expectedList[i], actualList[i]))
                        {
                            matches = false;
                            return false;
                        }
                        typePath.Pop();
                    }
                }
                matches = true;
            }
            return false;
        }

        private void MatchesFields(object expectedValue, object actualValue, Constraint equalConstraint, ref bool matches)
        {
            Type expectedType = expectedValue.GetType();

            FieldInfo[] fields = expectedType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);

            // should have passed the EqualConstraint check
            if (expectedType.IsPrimitive ||
                expectedType == typeof(string) ||
                expectedType == typeof(Guid) ||
                fields.Length == 0)
            {
                failedEquality = equalConstraint;
                matches = Failure;
                return;
            }

            if (expectedType == typeof(DateTime))
            {
                var expectedDate = (DateTime)expectedValue;
                var actualDate = (DateTime)actualValue;

                if (Math.Abs((expectedDate - actualDate).TotalSeconds) > 3.0)
                {
                    failedEquality = equalConstraint;
                    matches = Failure;
                    return;
                }
                matches = true;
                return;
            }

            if (CanRecurseFurther)
            {
                while(true)
                {
                    foreach (FieldInfo field in fields)
                    {
                        if (!Ignore(field))
                        {
                            typePath.Push(field.Name);
                            if (!Matches(GetValue(field, expectedValue), GetValue(field, actualValue)))
                            {
                                matches = Failure;
                                return;
                            }
                            typePath.Pop();
                        }
                    }
                    expectedType = expectedType.BaseType;
                    if (expectedType == null)
                    {
                        break;
                    }
                    fields = expectedType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
                }
            }
            matches = true;
            return;
        }

        private bool Ignore(FieldInfo field)
        {
            if (_ignoredNames.Contains(field.Name) ||
                _ignoredTypes.Contains(field.FieldType) ||
                _globallyIgnoredNames.Contains(field.Name) ||
                _globallyIgnoredTypes.Contains(field.FieldType) ||
                field.GetCustomAttributes(typeof (IgnoreContentsAttribute), false).Length != 0)
            {
                return true;
            }

            foreach(string ignoreSuffix in _ignoredSuffixes)
            {
                if (field.Name.EndsWith(ignoreSuffix))
                {
                    return true;
                }
            }

            foreach (Type ignoredInterface in _ignoredInterfaces)
            {
                if (ignoredInterface.IsAssignableFrom(field.FieldType))
                {
                    return true;
                }
            }
            return false;
        }

        private static bool Failure
        {
            get
            {
                return false;
            }
        }

        private static bool IsNullEquivalent(object value)
        {
            return value == null ||
                    value == DBNull.Value ||
                   (value is int && (int) value == int.MinValue) ||
                   (value is double && (double) value == double.MinValue) ||
                   (value is DateTime && (DateTime) value == DateTime.MinValue) ||
                   (value is Guid && (Guid) value == Guid.Empty) ||
                   (value is IList && ((IList)value).Count == 0);
        }

        private static object GetValue(FieldInfo field, object source)
        {
            try
            {
                return field.GetValue(source);
            }
            catch(Exception ex)
            {
                return ex;
            }
        }

        public override void WriteMessageTo(MessageWriter writer)
        {
            if (TypePath.Length != 0)
            {
                writer.WriteLine("Failure on " + TypePath);
            }

            if (failedEquality != null)
            {
                failedEquality.WriteMessageTo(writer);
            }
            else
            {
                base.WriteMessageTo(writer);
            }
        }
        public override void WriteDescriptionTo(MessageWriter writer)
        {
            writer.Write(expectedDescription);
        }

        public override void WriteActualValueTo(MessageWriter writer)
        {
            writer.Write(actualDescription);
        }

        private string TypePath
        {
            get
            {
                if (typePathExpanded == null)
                {
                    string[] p = typePath.ToArray();
                    Array.Reverse(p);
                    var text = new StringBuilder(128);
                    bool isFirst = true;
                    foreach(string part in p)
                    {
                        if (isFirst)
                        {
                            text.Append(part);
                            isFirst = false;
                        }
                        else
                        {
                            int i;
                            if (int.TryParse(part, out i))
                            {
                                text.Append("[" + part + "]");
                            }
                            else
                            {
                                text.Append("." + part);
                            }
                        }
                    }
                    typePathExpanded = text.ToString();
                }
                return typePathExpanded;
            }
        }

        private bool CanRecurseFurther
        {
            get
            {
                return typePath.Count < _maxRecursion;
            }
        }

        private static bool SafeSort(IList list)
        {
            if (list == null)
            {
                return false;
            }

            if (list.Count < 2)
            {
                return true;
            }

            try
            {
                object first = FirstNonNull(list) as IComparable;
                if (first == null)
                {
                    return false;
                }

                if (list is Array)
                {
                    Array.Sort((Array)list);
                    return true;
                }
                return CallIfExists(list, "Sort");
            }
            catch
            {
                return false;
            }
        }

        private static object FirstNonNull(IEnumerable enumerable)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException("enumerable");
            }
            foreach (object item in enumerable)
            {
                if (item != null)
                {
                    return item;
                }
            }
            return null;
        }

        private static bool CallIfExists(object instance, string method)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (String.IsNullOrEmpty(method))
            {
                throw new ArgumentNullException("method");
            }
            Type target = instance.GetType();
            MethodInfo m = target.GetMethod(method, new Type[0]);
            if (m != null)
            {
                m.Invoke(instance, null);
                return true;
            }
            return false;
        }

        #region VisitedComparison Helper

        private class VisitedComparison
        {
            private readonly object _expected;
            private readonly object _actual;

            public VisitedComparison(object expected, object actual)
            {
                _expected = expected;
                _actual = actual;
            }

            public override int GetHashCode()
            {
                return GetHashCode(_expected) ^ GetHashCode(_actual);
            }

            private static int GetHashCode(object o)
            {
                if (o == null)
                {
                    return 0;
                }
                return o.GetHashCode();
            }

            public override bool Equals(object obj)
            {
                if (obj == null)
                {
                    return false;
                }

                if (obj.GetType() != typeof(VisitedComparison))
                {
                    return false;
                }

                var other = (VisitedComparison) obj;
                return _expected == other._expected &&
                       _actual == other._actual;
            }
        }

        #endregion

        #region RegionalIgnoreTracker Helper

        private class RegionalIgnoreTracker : IDisposable
        {
            private readonly string _fieldName;
            private readonly Type _fieldType;

            public RegionalIgnoreTracker(string fieldName)
            {
                if (!_globallyIgnoredNames.Add(fieldName))
                {
                    _globallyIgnoredNames.Add(fieldName);
                    _fieldName = fieldName;
                }
            }

            public RegionalIgnoreTracker(Type fieldType)
            {
                if (!_globallyIgnoredTypes.Add(fieldType))
                {
                    _globallyIgnoredTypes.Add(fieldType);
                    _fieldType = fieldType;
                }
            }

            public void Dispose()
            {
                if (_fieldName != null)
                {
                    _globallyIgnoredNames.Remove(_fieldName);
                }
                if (_fieldType != null)
                {
                    _globallyIgnoredTypes.Remove(_fieldType);
                }
            }
        }

        #endregion

        #region RegionalWithinTracker Helper

        private class RegionalWithinTracker : IDisposable
        {
            public RegionalWithinTracker(object tolerance)
            {
                _regionalTolerance = tolerance;
            }

            public void Dispose()
            {
                _regionalTolerance = null;
            }
        }

        #endregion

        #region IgnoreContentsAttribute

        [AttributeUsage(AttributeTargets.Field)]
        public sealed class IgnoreContentsAttribute : Attribute
        {
        }

        #endregion
    }
    public class DatesEqualConstraint : EqualConstraint
    {
        private readonly object _expected;

        public DatesEqualConstraint(object expectedValue) : base(expectedValue)
        {
            _expected = expectedValue;
        }

        public override bool Matches(object actualValue)
        {
            if (tolerance != null && tolerance is TimeSpan)
            {
                if (_expected is DateTime && actualValue is DateTime)
                {
                    var expectedDate = (DateTime) _expected;
                    var actualDate = (DateTime) actualValue;
                    var toleranceSpan = (TimeSpan) tolerance;

                    if ((actualDate - expectedDate).Duration() <= toleranceSpan)
                    {
                        return true;
                    }
                }
                tolerance = null;
            }
            return base.Matches(actualValue);
        }
    }
}

您提供的代码缺少 DatesEqualConstraint() 方法以及所有相关方法(例如 .Within())。 - Daniel T.
@Daniel T.,当时我没有评论,但在你的评论后我添加了DatesEqualConstraint到答案中。 - Samuel Neff
代码中的“tolerance”变量不存在。已更新,但不是很准确。 - Tom Mayfield
@Thomas G. Mayfield,tolerance 在 NUnit 框架的父类 Constraint 中声明。 - Samuel Neff
@Thomas G. Mayfield,看起来tolerance在NUnit 2.5中被移除了。上面的代码是针对NUnit 2.4.6编写的。它需要稍微修改一下才能与NUnit 2.5一起使用(他们确实经常改变API)。 - Samuel Neff

3

这实际上是一个简单的过程。使用反射,你可以比较对象上的每个字段。

public static Boolean ObjectMatches(Object x, Object y)
{
    if (x == null && y == null)
        return true;
    else if ((x == null && y != null) || (x != null && y == null))
        return false;       

    Type tx = x.GetType();
    Type ty = y.GetType();

    if (tx != ty)
        return false;

    foreach(FieldInfo field in tx.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
    {
        if (field.FieldType.IsValueType && (field.GetValue(x).ToString() != field.GetValue(y).ToString()))
            return false;
        else if (field.FieldType.IsClass && !ObjectMatches(field.GetValue(x), field.GetValue(y)))
            return false;               
    }

    return true;
}

9
经验表明,处理深层对象时可能会变得非常棘手,特别是在比较子集合时。 - Marc Gravell
这并不意味着这是一个糟糕的解决方案。对于大多数情况,这种方法运作得相当不错。 - ChaosPandion
2
没错,当我运行这个程序时,我会遇到集合方面的问题。此外,它没有内置的循环检测 - 这种方法适用于单向树,而不适用于通用图形。 - blueberryfields
1
您还需要定义对象如何进行递归比较,可以通过调用Equal、IComparable或自定义比较方式来实现。列表和字典的比较并不是简单的。而且在实际情况中,总会有一些例外,您希望忽略某些字段。 - Stefan Steinegger
1
是的,你说得对 - Jesse上面链接的www.kellermansoftware.com上的代码似乎可以正确处理所有事情。 - blueberryfields
是的,这个答案至少应该附带一个关于哪些用例没有被处理的警告。此外,在具有“NotImplementedExceptions”的属性中,这段代码会发生什么?(无法记住使用此“BindingFlags”设置包含什么和不包含什么。) - Johny Skovdal

2

使用Jesse建议的Nuget和这段代码,我成功地比较了两个对象,并取得了很好的结果。

using KellermanSoftware.CompareNetObjects;
using System;

namespace MyProgram.UnitTestHelper
{
  public class ObjectComparer
  {

    public static bool ObjectsHaveSameValues(object first, object second)
    {
      CompareLogic cl = new CompareLogic();
      ComparisonResult result = cl.Compare(first, second);

      if (!result.AreEqual)
        Console.WriteLine(result.DifferencesString);

      return result.AreEqual;
    }
  }
}

0

这里是一个简单的比较器,我们在单元测试中使用它来断言两个对象具有相等的属性。它是从各种文章中找到的想法的混合,并处理循环引用。

public static class TestHelper
{
    public static void AssertAreEqual(Object expected, Object actual, String name)
    {
        // Start a new check with an empty list of actual objects checked
        // The list of actual objects checked is used to ensure that circular references don't result in infinite recursion
        List<Object> actualObjectsChecked = new List<Object>();
        AssertAreEqual(expected, actual, name, actualObjectsChecked);
    }

    private static void AssertAreEqual(Object expected, Object actual, String name, List<Object> actualObjectsChecked)
    {
        // Just return if already checked the actual object
        if (actualObjectsChecked.Contains(actual))
        {
            return;
        }

        actualObjectsChecked.Add(actual);

        // If both expected and actual are null, they are considered equal
        if (expected == null && actual == null)
        {
            return;
        }

        if (expected == null && actual != null)
        {
            Assert.Fail(String.Format("The actual value of {0} was not null when null was expected.", name));
        }

        if (expected != null && actual == null)
        {
            Assert.Fail(String.Format("The actual value of {0} was null when an instance was expected.", name));
        }

        // Get / check type info
        // Note: GetType always returns instantiated (i.e. most derived) type
        Type expectedType = expected.GetType();
        Type actualType = actual.GetType();
        if (expectedType != actualType)
        {
            Assert.Fail(String.Format("The actual type of {0} was not the same as the expected type.", name));
        }

        // If expected is a Primitive, Value, or IEquatable type, assume Equals is sufficient to check
        // Note: Every IEquatable type should have also overridden Object.Equals
        if (expectedType.IsPrimitive || expectedType.IsValueType || expectedType.IsIEquatable())
        {
            Assert.IsTrue(expected.Equals(actual), "The actual {0} is not equal to the expected.", name);
            return;
        }

        // If expected is an IEnumerable type, assume comparing enumerated items is sufficient to check
        IEnumerable<Object> expectedEnumerable = expected as IEnumerable<Object>;
        IEnumerable<Object> actualEnumerable = actual as IEnumerable<Object>;
        if ((expectedEnumerable != null) && (actualEnumerable != null))
        {
            Int32 actualEnumerableCount = actualEnumerable.Count();
            Int32 expectedEnumerableCount = expectedEnumerable.Count();

            // Check size first
            if (actualEnumerableCount != expectedEnumerableCount)
            {
                Assert.Fail(String.Format("The actual number of enumerable items in {0} did not match the expected number.", name));
            }

            // Check items in order, assuming order is the same
            for (int i = 0; i < actualEnumerableCount; ++i)
            {
                AssertAreEqual(expectedEnumerable.ElementAt(i), actualEnumerable.ElementAt(i), String.Format("{0}[{1}]", name, i), actualObjectsChecked);
            }

            return;
        }

        // If expected is not a Primitive, Value, IEquatable, or Ienumerable type, assume comparing properties is sufficient to check
        // Iterate through properties
        foreach (PropertyInfo propertyInfo in actualType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
        {
            // Skip properties that can't be read or require parameters
            if ((!propertyInfo.CanRead) || (propertyInfo.GetIndexParameters().Length != 0))
            {
                continue;
            }

            // Get properties from both
            Object actualProperty = propertyInfo.GetValue(actual, null);
            Object expectedProperty = propertyInfo.GetValue(expected, null);

            AssertAreEqual(expectedProperty, actualProperty, String.Format("{0}.{1}", name, propertyInfo.Name), actualObjectsChecked);
        }
    }

    public static Boolean IsIEquatable(this Type type)
    {
        return type.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEquatable<>));
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接