Beispiel #1
0
        private static void ReflectionAppend(
            object lhs,
            object rhs,
            Type clazz,
            EqualsBuilder builder,
            bool useTransients)
        {
            var fields = clazz.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
                                         BindingFlags.DeclaredOnly);

            for (var i = 0; i < fields.Length && builder._isEqual; i++)
            {
                var f = fields[i];
                //TODO:atrosin Revise:f.getName().indexOf('$')
                if (f.Name.IndexOf('$') == -1 &&
                    (useTransients || !IsTransient(f)) &&
                    !f.IsStatic)
                {
                    try
                    {
                        builder.Append(f.GetValue(lhs), f.GetValue(rhs));
                    }

                    catch (TargetException te)
                    {
                        throw new Exception("Unexpected TargetException", te);
                    }
                }
Beispiel #2
0
        public static bool ReflectionEquals(object lhs, object rhs, bool testTransients, Type reflectUpToClass)
        {
            if (lhs == rhs)
            {
                return(true);
            }
            if (lhs == null || rhs == null)
            {
                return(false);
            }

            var  lhsClass = lhs.GetType();
            var  rhsClass = rhs.GetType();
            Type testClass;

            if (lhsClass.IsInstanceOfType(rhs))
            {
                testClass = lhsClass;
                if (!rhsClass.IsInstanceOfType(lhs))
                {
                    testClass = rhsClass;
                }
            }
            else if (rhsClass.IsInstanceOfType(lhs))
            {
                testClass = rhsClass;
                if (!lhsClass.IsInstanceOfType(rhs))
                {
                    testClass = lhsClass;
                }
            }
            else
            {
                return(false);
            }
            var equalsBuilder = new EqualsBuilder();

            try
            {
                ReflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients);
                while (testClass.BaseType != null && testClass != reflectUpToClass)
                {
                    testClass = testClass.BaseType;
                    ReflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients);
                }
            }
            catch (ArgumentException e)
            {
                return(false);
            }
            return(equalsBuilder.IsEquals());
        }