Exemplo n.º 1
0
        internal static bool IsSerializedEqual(object?a, object?b)
        {
            var type = a?.GetType();

            if (type != b?.GetType())
            {
                return(false);
            }

            if (a == null) // Also implies b is null since it'd have failed the type equality check otherwise.
            {
                return(true);
            }

            if (TryGenericListType(type !, out _))
            {
                var listA = (IList)a;
                var listB = (IList)b !;

                if (listA.Count != listB.Count)
                {
                    return(false);
                }

                for (var i = 0; i < listA.Count; i++)
                {
                    var elemA = listA[i];
                    var elemB = listB[i];

                    if (!IsSerializedEqual(elemA, elemB))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (typeof(IExposeData).IsAssignableFrom(type))
            {
                // Serialize both, see if output matches.
                var testA = new YamlMappingNode();
                var testB = new YamlMappingNode();
                var serA  = NewWriter(testA);
                var serB  = NewWriter(testB);

                var expA = (IExposeData)a;
                var expB = (IExposeData)b !;

                expA.ExposeData(serA);
                expB.ExposeData(serB);

                // Does deep equality.
                return(testA.Equals(testB));
            }

            return(a.Equals(b));
        }
Exemplo n.º 2
0
        internal static bool IsSerializedEqual(object?a, object?b)
        {
            var type = a?.GetType();

            if (type != b?.GetType())
            {
                return(false);
            }

            if (a == null) // Also implies b is null since it'd have failed the type equality check otherwise.
            {
                return(true);
            }

            if (TryGenericListType(type !, out _))
            {
                var listA = (IList)a;
                var listB = (IList)b !;

                if (listA.Count != listB.Count)
                {
                    return(false);
                }

                for (var i = 0; i < listA.Count; i++)
                {
                    var elemA = listA[i];
                    var elemB = listB[i];

                    if (!IsSerializedEqual(elemA, elemB))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (TryGenericDictType(type !, out _, out _))
            {
                var dictA = (IDictionary)a;
                var dictB = (IDictionary)b !;

                foreach (var entryMaybe in dictA)
                {
                    var entry = (DictionaryEntry)entryMaybe !;
                    var k     = entry.Key;
                    var v     = entry.Value;

                    if (!dictB.Contains(k))
                    {
                        return(false);
                    }

                    if (!IsSerializedEqual(v, dictB[k]))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            if (TryGenericHashSetType(type !, out _))
            {
                var setA = ((IEnumerable)a).GetEnumerator();
                var setB = ((IEnumerable)b !).GetEnumerator();

                while (setA.MoveNext())
                {
                    if (!setB.MoveNext())
                    {
                        return(false);
                    }

                    if (!IsSerializedEqual(setA.Current, setB.Current))
                    {
                        return(false);
                    }
                }

                if (setB.MoveNext())
                {
                    return(false);
                }

                return(true);
            }

            if (typeof(IExposeData).IsAssignableFrom(type))
            {
                // Serialize both, see if output matches.
                var testA = new YamlMappingNode();
                var testB = new YamlMappingNode();
                var serA  = NewWriter(testA);
                var serB  = NewWriter(testB);

                var expA = (IExposeData)a;
                var expB = (IExposeData)b !;

                expA.ExposeData(serA);
                expB.ExposeData(serB);

                // Does deep equality.
                return(testA.Equals(testB));
            }

            return(a.Equals(b));
        }