Is it possible to create an expression tree for dy

2020-06-17 06:19发布

问题:

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,

回答1:

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.



标签: c# lambda