none
Changing expression tree at run time RRS feed

  • Question


  • Hello,

    I'm new to C# expressions trees. I've an expression tree being passed to a method as below: private void MyMethod(Expression<MyClass> exp, string param) I need to be able to tweak this exp and add a AND clause based on the param value at runtime. How do I achieve this please? Thanks.


    sdfsdf

    I'm new to C# expressions trees.

    I've an expression tree being passed to a method as below:

    private void MyMethod(Expression<MyClass> exp, string param)

    I need to be able to tweak this exp and add a AND clause based on the param value at runtime. How do I achieve this please?

    Thanks.

    I'm new to C# expressions trees.

    I've an expression tree being passed to a method as below:

    private void MyMethod(Expression<MyClass> exp, string param)

    I need to be able to tweak this exp and add a AND clause based on the param value at runtime. How do I achieve this please?

    Thanks.


    • Edited by ANi2000 Saturday, December 16, 2017 3:33 AM
    Saturday, December 16, 2017 3:32 AM

Answers

  • Hello ANi2000,

    >>I need to be able to tweak thisexp andadd a AND clause based on the param value at runtime

    The emphasis is how to convert string to expressions. Because the CLR can't recognize expression type . it always just take the only string type to handle. If you want to convert the string to expression, you need to recompile it. You could try the below example.

      private void MyMethod(Expression<Func<int,Boolean>> expression1, string param)
            {
                CSharpCodeProvider provider = new CSharpCodeProvider();
    
                CompilerResults results = provider.CompileAssemblyFromSource
                (
                    new CompilerParameters(new[] { "System.Core.dll" }),
                       @"using System;
                       using System.Linq.Expressions;
                             class foo{
                                public static Expression<Func<int, Boolean>> bar(){
                                    return " + param+";" +
                                    "}" +
                                 "}");
    
                Expression<Func<int,Boolean>> expression2 = (Expression<Func<int, Boolean>>)results.CompiledAssembly.GetType("foo").GetMethod("bar").Invoke(null, null);
    
                 //Expression.AndAlso(expression.);
                 Expression andAlsoExpr = Expression.AndAlso(
                    Expression.Constant(expression1.Compile()(7)),
                    Expression.Constant(expression2.Compile()(8))
                );
    
                // Print out the expression.
                Console.WriteLine(andAlsoExpr.ToString());
    
                // The following statement first creates an expression tree,
                // then compiles it, and then executes it. 
                Console.WriteLine(Expression.Lambda<Func<bool>>(andAlsoExpr).Compile()());
    
                // LambdaExpression lambda = Expression.Lambda<Func<UserInfo, bool>>(whereExp, parExp);
    
                // Console.WriteLine(lambda);
            }

    How to use

          Expression<Func<int, bool>> lambda = num => num> 5;
    
               // Expression<Func<int,Boolean>> expression = Expression.Lambda<Func<int,Boolean>>(Expression.Call(func.Method));
    
                MyMethod(lambda,"x=>x<7");

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 17, 2017 9:24 AM
    Moderator

All replies

  • Check the Expression.AndAlso function, which allows you to build a conditional And expression having two operands. You can define the second operand using the members of Expression.

    If you want to calculate the expression immediately, then maybe evaluate the result of exp, then use ‘&&’ to get the final result.

    Saturday, December 16, 2017 8:46 AM
  • Hello ANi2000,

    >>I need to be able to tweak thisexp andadd a AND clause based on the param value at runtime

    The emphasis is how to convert string to expressions. Because the CLR can't recognize expression type . it always just take the only string type to handle. If you want to convert the string to expression, you need to recompile it. You could try the below example.

      private void MyMethod(Expression<Func<int,Boolean>> expression1, string param)
            {
                CSharpCodeProvider provider = new CSharpCodeProvider();
    
                CompilerResults results = provider.CompileAssemblyFromSource
                (
                    new CompilerParameters(new[] { "System.Core.dll" }),
                       @"using System;
                       using System.Linq.Expressions;
                             class foo{
                                public static Expression<Func<int, Boolean>> bar(){
                                    return " + param+";" +
                                    "}" +
                                 "}");
    
                Expression<Func<int,Boolean>> expression2 = (Expression<Func<int, Boolean>>)results.CompiledAssembly.GetType("foo").GetMethod("bar").Invoke(null, null);
    
                 //Expression.AndAlso(expression.);
                 Expression andAlsoExpr = Expression.AndAlso(
                    Expression.Constant(expression1.Compile()(7)),
                    Expression.Constant(expression2.Compile()(8))
                );
    
                // Print out the expression.
                Console.WriteLine(andAlsoExpr.ToString());
    
                // The following statement first creates an expression tree,
                // then compiles it, and then executes it. 
                Console.WriteLine(Expression.Lambda<Func<bool>>(andAlsoExpr).Compile()());
    
                // LambdaExpression lambda = Expression.Lambda<Func<UserInfo, bool>>(whereExp, parExp);
    
                // Console.WriteLine(lambda);
            }

    How to use

          Expression<Func<int, bool>> lambda = num => num> 5;
    
               // Expression<Func<int,Boolean>> expression = Expression.Lambda<Func<int,Boolean>>(Expression.Call(func.Method));
    
                MyMethod(lambda,"x=>x<7");

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 17, 2017 9:24 AM
    Moderator
  • Hello ANi2000,

    Did you try the above code? Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 24, 2017 9:09 AM
    Moderator