mardi 28 novembre 2017

Fastest way to compute an expression func result given parameters

I receive an Expression<Func<...,...,...,TResult>> (random, but fixed number of parameters) instance in a form of a general Expression type.

For instance:

Expression<Func<int,int,int>> adderExpression = (a,b) => a+b;
Expression receivedExpression = adderExpression;
//Later on, I get only the receivedExpression variable and an array of values for which I have to compute:
var values = new object[] {3, 4};
var result = receivedExpression.Compile().DynamicInvoke(values);

The only thing I have access to is the receivedExpression variable and an array of object values for which I have to compute the result of applying the compiled expression.

The receivedExpression does not change: once I get it, it remains the same.

What is the fastest way I can compute the result variable given my circumstances? DynamicInvoke is super slow in this case, and I was wondering if there is a faster way to compute the result.





Aucun commentaire:

Enregistrer un commentaire