Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an expression tree for dynamic if statements?

Tags:

c#

lambda

I have a situtaiton where I read the bussiness logic and replace the variables with actual values and then I need to evaluate it to get result. I am currently using bcParser to do it and it works just fine for all the logic which is written like excel format.

The curve ball thrown at me is that, the if condition will not be like excel if(cond, true, false) rather it will be like C# where the if (cond) { true; } else { false;}, this makes more sense and easy to maintain. Since I replace all the variables with value before hand, all I have to do is evaluate it. Currently I am solving this problem by exporting the logic to c# methods and using reflection I am evaluating it and it also works.

I am wondering is there any other option, I do not want to write code for each if condition and would like to evaluate it on the run time. I was wondering if I should able to create a token parser of some sort and call C# native expression evalution and perform the calculation. I haven't gone into understanding expresion trees, it seems it is possible with that approach. before I go there, I would like to know is it possible at all? Thanks,

like image 618
Nair Avatar asked Mar 23 '12 18:03

Nair


People also ask

Can we use dynamic in lambda expression?

In 2010, the Dynamic Type was introduced and that gave us the ability to create dynamic lambda expressions.

What .NET feature do you use to create an expression tree?

In . NET Framework 4 or later, the expression trees API also supports assignments and control flow expressions such as loops, conditional blocks, and try-catch blocks. By using the API, you can create expression trees that are more complex than those that can be created from lambda expressions by the C# compiler.


1 Answers

Yes!

The key is using the System.Linq.Expressions namespace. You can build up an expression tree programmatically, either in your code or by modifying your parser then compile it into a Delegate. This API compiles your Delegate inside of a DynamicAssembly which means that your compiled expressions can be unloaded from memory by the garbage collector when you completely dereference them.

Here is a very simple example:

var b = true;
Func<bool> condition = () => b;
Action trueExpression = () => { Console.WriteLine(true); };
Action falseExpression = () => { Console.WriteLine(false); };

var e = Expression.Condition(
    Expression.Invoke(Expression.Constant(condition)),
    Expression.Invoke(Expression.Constant(trueExpression)),
    Expression.Invoke(Expression.Constant(falseExpression)));

var λ = Expression.Lambda(e).Compile();

b = true;
λ.DynamicInvoke();

b = false;
λ.DynamicInvoke();

This produces the output:

True
False

The step where the expression is compiled into a Lambda can be a significant performance hit, you will want to come up with a caching strategy for your compiled lambdas. It's well worth it though, calling the compiled lambda using DynamicInvoke is very fast. Almost as fast as if you had pre-compiled it. This technique is significantly faster than using CodeDom code generation (which requires a whole another process to do the compilation) and it has the major benefit of producing unloadable assemblies.

The only limitation to this is that you cannot create Types with this API. You have to limit yourself to expressions and statements. It's quite powerful however, this is the magic guts of the DLR.

like image 177
justin.m.chase Avatar answered Nov 07 '22 17:11

justin.m.chase