예제 #1
0
        private static bool TryGenerateForEnumerable(Type type, ref EqualityFunction <T> result)
        {
            if (type.CustomAttributes.OfType <NotEnumerableComparisonAttribute>().Any())
            {
                return(false);
            }

            Type enumerableInterface =
                type.GetInterfaces()
                .Concat(new[] { type })//this one handles the case where type is IEnumerable itself
                .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                .FirstOrDefault();

            if (enumerableInterface == null)
            {
                return(false);
            }

            Type itemType = enumerableInterface.GetGenericArguments().Single();

            ParameterExpression left  = Expression.Parameter(type, "x");
            ParameterExpression right = Expression.Parameter(type, "y");

            result = Expression.Lambda <EqualityFunction <T> >(
                Expression.Call(
                    instance: null,
                    method: NotEquals.GetSequenceNotEqualMethod(itemType, type),
                    arguments: new[] { left, right }
                    ),
                left,
                right
                ).Compile();
            return(true);
        }
예제 #2
0
        private static bool TryGenerateForIEquatable(Type type, ref EqualityFunction <T> result)
        {
            Type equatableInterface = typeof(IEquatable <>).MakeGenericType(type);
            bool implements         = type.GetInterfaces().Any(x => x == equatableInterface);

            if (!implements)
            {
                return(false);
            }

            if (type.GetCustomAttributes <EquatableDependsOnLazyComparisonAttribute>().Any())
            {
                return(false);
            }

            ParameterExpression left  = Expression.Parameter(type, "x");
            ParameterExpression right = Expression.Parameter(type, "y");

            MethodInfo compareMethod = NotEquals.GetEquatableCompareMethod(type);

            result = Expression.Lambda <EqualityFunction <T> >(
                Expression.Call(
                    instance: null,
                    method: NotEquals.GetEquatableCompareMethod(type),
                    arguments: new[] { left, right }
                    ),
                parameters: new[] { left, right }
                ).Compile();

            return(true);
        }