Check two object, of unknown type, for equality, comparing all their fields

后端 未结 2 1854
甜味超标
甜味超标 2021-01-13 22:58

I need to define a method to compare two different objects of a same type. The type of objects is not specific. The objects may be a DLL type, so I can\'t override Equ

相关标签:
2条回答
  • 2021-01-13 23:23

    I have recently been told about this lib that will do exactly what you are wanting

    http://comparenetobjects.codeplex.com/releases/view/47978

    0 讨论(0)
  • 2021-01-13 23:44

    I want the utility function to compare any 2 objects. All of the type I want to cover is

    1. Primitive Type
    2. Any class that Implement IEnumerable (Like Dict or List)
    3. Any Class

    so I use generic and reflection to do so. I code it like this.

            public static bool CompareObjects<T>(T expectInput, T actualInput)
        {
            // If T is primitive type.
            if (typeof(T).IsPrimitive)
            {
                if (expectInput.Equals(actualInput))
                {
                    return true;
                }
    
                return false;
            }
    
            if (expectInput is IEquatable<T>)
            {
                if (expectInput.Equals(actualInput))
                {
                    return true;
                }
    
                return false;
            }
    
            if (expectInput is IComparable)
            {
                if (((IComparable)expectInput).CompareTo(actualInput) == 0)
                {
                    return true;
                }
    
                return false;
            }
    
            // If T is implement IEnumerable.
            if (expectInput is IEnumerable)
            {
                var expectEnumerator = ((IEnumerable)expectInput).GetEnumerator();
                var actualEnumerator = ((IEnumerable)actualInput).GetEnumerator();
    
                var canGetExpectMember = expectEnumerator.MoveNext();
                var canGetActualMember = actualEnumerator.MoveNext();
    
                while (canGetExpectMember && canGetActualMember && true)
                {
                    var currentType = expectEnumerator.Current.GetType();
                    object isEqual = typeof(Utils).GetMethod("CompareObjects").MakeGenericMethod(currentType).Invoke(null, new object[] { expectEnumerator.Current, actualEnumerator.Current });
    
                    if ((bool)isEqual == false)
                    {
                        return false;
                    }
    
                    canGetExpectMember = expectEnumerator.MoveNext();
                    canGetActualMember = actualEnumerator.MoveNext();
                }
    
                if (canGetExpectMember != canGetActualMember)
                {
                    return false;
                }
    
                return true;
            }
    
            // If T is class.
            var properties = typeof(T).GetProperties();
            foreach (var property in properties)
            {
                var expectValue = typeof(T).GetProperty(property.Name).GetValue(expectInput);
                var actualValue = typeof(T).GetProperty(property.Name).GetValue(actualInput);
    
                if (expectValue == null || actualValue == null)
                {
                    if (expectValue == null && actualValue == null)
                    {
                        continue;
                    }
    
                    return false;
                }
    
                object isEqual = typeof(Utils).GetMethod("CompareObjects").MakeGenericMethod(property.PropertyType).Invoke(null, new object[] { expectValue, actualValue });
    
                if ((bool)isEqual == false)
                {
                    return false;
                }
            }
    
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题