public void ParameterExpressionRenamer_Rename_OldNameInNewName(string oldName, string newName, string resultString)
        {
            // Assign
            var expression = Expression.Add(Expression.Parameter(typeof(int), oldName), Expression.Constant(42));
            var sut        = new ParameterExpressionRenamer(oldName, newName);

            // Act
            string result = sut.Rename(expression, out ParameterExpression parameterExpression).ToString();

            // Assert
            Check.That(result).IsEqualTo(resultString);
            Check.That(parameterExpression.Name).IsEqualTo(newName);
        }
        public void ParameterExpressionRenamer_Rename_NoParameterExpressionPresent()
        {
            // Assign
            var expression = Expression.Add(Expression.Constant(1), Expression.Constant(2));
            var sut        = new ParameterExpressionRenamer("test");

            // Act
            string result = sut.Rename(expression, out ParameterExpression parameterExpression).ToString();

            // Assert
            Check.That(result).IsEqualTo("(1 + 2)");
            Check.That(parameterExpression).IsNull();
        }
        /// <summary>
        /// Parses an expression into a LambdaExpression.
        /// </summary>
        /// <param name="delegateType">The delegate type.</param>
        /// <param name="parsingConfig">The Configuration for the parsing.</param>
        /// <param name="createParameterCtor">if set to <c>true</c> then also create a constructor for all the parameters. Note that this doesn't work for Linq-to-Database entities.</param>
        /// <param name="parameters">A array from ParameterExpressions.</param>
        /// <param name="resultType">Type of the result. If not specified, it will be generated dynamically.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="values">An object array that contains zero or more objects which are used as replacement values.</param>
        /// <returns>The generated <see cref="LambdaExpression"/></returns>
        //[PublicAPI]
        public static LambdaExpression ParseLambda(Type?delegateType, ParsingConfig?parsingConfig, bool createParameterCtor, ParameterExpression[] parameters, Type?resultType, string expression, params object?[] values)
        {
            LambdaExpression?lambdaExpression = null;

            // Check.NotNull(parameters, nameof(parameters));
            Check.HasNoNulls(parameters, nameof(parameters));
            Check.NotEmpty(expression, nameof(expression));

            var parser = new ExpressionParser(parameters, expression, values, parsingConfig);

            var parsedExpression = parser.Parse(resultType, createParameterCtor);

            if (parsingConfig is not null && parsingConfig.RenameParameterExpression && parameters.Length == 1)
            {
                var renamer = new ParameterExpressionRenamer(parser.LastLambdaItName);
                parsedExpression = renamer.Rename(parsedExpression, out ParameterExpression? newParameterExpression);

                if (delegateType is null)
                {
                    lambdaExpression = Expression.Lambda(parsedExpression, new[] { newParameterExpression ! });
Ejemplo n.º 4
0
        public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
        {
            LambdaExpression lambdaExpression = null;

            Check.NotNull(parameters, nameof(parameters));
            Check.HasNoNulls(parameters, nameof(parameters));
            Check.NotEmpty(expression, nameof(expression));

            var parser = new ExpressionParser(parameters, expression, values, parsingConfig);

            var parsedExpression = parser.Parse(resultType, createParameterCtor);

            if (parsingConfig != null && parsingConfig.RenameParameterExpression && parameters.Length == 1)
            {
                var renamer = new ParameterExpressionRenamer(parser.LastLambdaItName);
                parsedExpression = renamer.Rename(parsedExpression, out ParameterExpression newParameterExpression);

                if (delegateType == null)
                {
                    lambdaExpression = Expression.Lambda(parsedExpression, new[] { newParameterExpression });
                }
                else
                {
                    lambdaExpression = Expression.Lambda(delegateType, parsedExpression, new[] { newParameterExpression });
                }
            }
            else
            {
                if (delegateType == null)
                {
                    lambdaExpression = Expression.Lambda(parsedExpression, parameters);
                }
                else
                {
                    lambdaExpression = Expression.Lambda(delegateType, parsedExpression, parameters);
                }
            }

            return(lambdaExpression);
        }