Пример #1
0
        // <summary>
        // Compare the value for of a property of LogicalTreeNode.
        // If the value are not of the same type, return false.
        // if the value can be convert to string and the result is not
        // the same just return false.
        // For logical tree nodes, call CompareLogicalTree to compare recursively.
        // Otherwise, use CompareAsGenericObject
        // to compare
        // </summary>
        // <param name="obj1">The first value</param>
        // <param name="obj2">The second value</param>
        // <returns>
        // true, if value is regarded as the same
        // false, otherwise use CompareAsGenericObject to compare
        // </returns>
        private static bool CompareObjects(object obj1, object obj2)
        {
            bool same = false;

            //Both are null
            if (null == obj1 && null == obj2)
            {
                return(true);
            }

            //Only one of them is null
            if (null == obj1)
            {
                TreeComparer.SendCompareMessage("Values is different: 'null' vs. '" + obj2.ToString() + "'.");
                TreeComparer.Break();
                return(false);
            }

            if (null == obj2)
            {
                TreeComparer.SendCompareMessage("Values are different: '" + obj1.ToString() + "' vs. 'null'.");
                TreeComparer.Break();
                return(false);
            }

            //Compare Type
            Type type1 = obj1.GetType();
            Type type2 = obj2.GetType();

            if (!type1.Equals(type2))
            {
                TreeComparer.SendCompareMessage("Type of value is different: '" + type1.FullName + "' vs. '" + type2.FullName + "'.");
                TreeComparer.Break();
                return(false);
            }


            if (type1.IsPrimitive)
            {
                same = TreeComparer.ComparePrimitive(obj1, obj2);
                return(same);
            }

            if (TreeComparer._objectsInTree[0].Contains(obj1.GetHashCode()) || TreeComparer._objectsInTree[1].Contains(obj2.GetHashCode()))
            {
                return(true);
            }

            TreeComparer._objectsInTree[0].Add(obj1.GetHashCode());
            TreeComparer._objectsInTree[1].Add(obj2.GetHashCode());

            return(TreeComparer.CompareGenericObject(obj1, obj2));;
        }
Пример #2
0
        // <summary>
        // Compare collections of properties
        // </summary>
        // <param name="properties1">The first property that is collection</param>
        // <param name="properties2">The second property that is collection</param>
        // <returns>
        // true, if they are the same
        // false, otherwise
        // </returns>
        private static bool ComparePropertyAsIEnumerable(
            object properties1,
            object properties2)
        {
            IEnumerable firstEnumerable  = properties1 as IEnumerable;
            IEnumerable secondEnumerable = properties2 as IEnumerable;

            if (firstEnumerable == null && secondEnumerable == null)
            {
                return(true);
            }

            if (firstEnumerable == null)
            {
                TreeComparer.SendCompareMessage("properties1 is not IEnumerable");
                TreeComparer.Break();
                return(false);
            }

            if (secondEnumerable == null)
            {
                TreeComparer.SendCompareMessage("properties2 is not IEnumerable");
                TreeComparer.Break();
                return(false);
            }

            IEnumerator firstEnumerator  = firstEnumerable.GetEnumerator();
            IEnumerator secondEnumerator = secondEnumerable.GetEnumerator();
            uint        firstNodeCount   = 0;
            uint        secondNodeCount  = 0;

            while (firstEnumerator.MoveNext())
            {
                firstNodeCount++;
                if (!secondEnumerator.MoveNext())
                {
                    break;
                }

                secondNodeCount++;

                if (!TreeComparer.CompareGenericObject(firstEnumerator.Current, secondEnumerator.Current))
                {
                    TreeComparer.SendCompareMessage("The first node and the second node have different values in collection");
                    TreeComparer.Break();
                    return(false);
                }
            }

            if (secondEnumerator.MoveNext())
            {
                secondNodeCount++;
            }

            if (firstNodeCount != secondNodeCount)
            {
                TreeComparer.SendCompareMessage("The first node and the second node have different lengths");
                TreeComparer.Break();
                return(false);
            }

            return(true);
        }