Compiler dynamic Linq Func cache divider <> * without * params?

I use the Dynamic.ParseLambda method from the Dynamic LINQ library to create expressions, compile each of them into Func <> and the cache in the dictionary:

// parse some dynamic expression using this ParseLambda sig:
Expression<Func<TArgument,TResult>> funcExpr = 
 System.Linq.Dynamic.ParseLambda<TArgument, TResult>(
  expressionString, // string for dyn lambda expression
  parameters);  // object[] params

// then compile & cache the output of this as a delegate:
Func<TArgument,TResult> func = funcExpr.Compile(); //<-cache this

// then to execute, use:
return func(entityInstance);

The problem is that it forces me to cache a different instance of the delegate for each individual set of parameter values. It seems wasteful; all overhead using Dynamic LINQ is parsed and compiled; after creation, delegates are next to directly encoded lambdas in performance. Is there a way to move the parameters outside the expression, so I can pass different values ​​to the shared cached delegate when I call it (instead of creating it)?

// e.g. with params...
return func(entityInstance,parameters);

// or if params are the issue, multiple signatures are ok:
return func(entityInstance, intValue, stringValue);

System.Linq.Dynamic - .ParseLambda .Compile-, . - ?

!

+3
1

, ; - Expression<Func<object[], object>> fetch-by-index . , , . , .

"", , - , , , , :

Expression<Func<object[], object>> func = arr => ((string)arr[0]) + (int)arr[1];

( , , )

+3

Source: https://habr.com/ru/post/1756638/


All Articles