public override Expression Apply(Expression expression, Random random)
        {
            var typeArgument = expression.Type.GetGenericArguments()[0];

            var isDescending = random.Next(3) == 0;
            var orderBy      = isDescending
                ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, typeArgument)
                : OrderByMethodInfo.MakeGenericMethod(typeArgument, typeArgument);

            var prm              = Expression.Parameter(typeArgument, "prm");
            var lambda           = Expression.Lambda(prm, prm);
            var resultExpression = Expression.Call(orderBy, expression, lambda);

            return(resultExpression);
        }
        public override Expression Apply(Expression expression, Random random)
        {
            var typeArgument = expression.Type.GetGenericArguments()[0];
            var properties   = typeArgument.GetProperties().Where(p => !p.GetMethod.IsStatic && IsOrderedableType(p.PropertyType)).ToList();

            properties = FilterPropertyInfos(typeArgument, properties);

            var i = random.Next(properties.Count);

            var isDescending = random.Next(3) == 0;
            var orderBy      = isDescending
                ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, properties[i].PropertyType)
                : OrderByMethodInfo.MakeGenericMethod(typeArgument, properties[i].PropertyType);

            var prm = Expression.Parameter(typeArgument, "prm");

            var lambdaBody = (Expression)Expression.Property(prm, properties[i]);

            if (properties[i].PropertyType.IsValueType &&
                !(properties[i].PropertyType.IsGenericType &&
                  properties[i].PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                var nullablePropertyType = typeof(Nullable <>).MakeGenericType(properties[i].PropertyType);

                orderBy = isDescending
                    ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType)
                    : OrderByMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType);

                lambdaBody = Expression.Convert(lambdaBody, nullablePropertyType);
            }

            if (typeArgument == typeof(string))
            {
                // string.Length - make it nullable in case we access optional argument
                orderBy    = OrderByMethodInfo.MakeGenericMethod(typeArgument, typeof(int?));
                lambdaBody = Expression.Convert(lambdaBody, typeof(int?));
            }

            var lambda           = Expression.Lambda(lambdaBody, prm);
            var resultExpression = Expression.Call(orderBy, expression, lambda);

            return(resultExpression);
        }
Esempio n. 3
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i = random.Next(_expressionFinder.FoundExpressions.Count);
            var expressionToInject = _expressionFinder.FoundExpressions.ToList()[i].Key;
            var j        = random.Next(_expressionFinder.FoundExpressions[expressionToInject].Count);
            var property = _expressionFinder.FoundExpressions[expressionToInject][j];

            var typeArgument = expressionToInject.Type.GetGenericArguments()[0];

            var isDescending = random.Next(3) == 0;
            var orderBy      = isDescending
                ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, property.PropertyType)
                : OrderByMethodInfo.MakeGenericMethod(typeArgument, property.PropertyType);

            var prm        = Expression.Parameter(typeArgument, "prm");
            var lambdaBody = (Expression)Expression.Property(prm, property);

            if (property.PropertyType.IsValueType &&
                !(property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                var nullablePropertyType = typeof(Nullable <>).MakeGenericType(property.PropertyType);

                orderBy = isDescending
                    ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType)
                    : OrderByMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType);

                lambdaBody = Expression.Convert(lambdaBody, nullablePropertyType);
            }

            if (typeArgument == typeof(string))
            {
                // string.Length - make it nullable in case we access optional argument
                orderBy    = OrderByMethodInfo.MakeGenericMethod(typeArgument, typeof(int?));
                lambdaBody = Expression.Convert(lambdaBody, typeof(int?));
            }

            var lambda   = Expression.Lambda(lambdaBody, prm);
            var injector = new ExpressionInjector(expressionToInject, e => Expression.Call(orderBy, e, lambda));

            return(injector.Visit(expression));
        }