z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.

Home Page:https://z4kn4fein.github.io/stashbox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion: Add Func<> to compile expressions to configuration

schuettecarsten opened this issue · comments

I heavily use a forked version of FastExpressionCompiler in my project, and so I think it would be great if Stashbox also use this library when compiling expressions. For that, I have changed the ExpressionExtensions like this:

 @@ -36,15 +34,18 @@ namespace System.Linq.Expressions
                resolutionContext.SingleInstructions.Add(expression);
                expression = Expression.Block(resolutionContext.DefinedVariables.WalkOnValues(), resolutionContext.SingleInstructions);
            }

            if (containerConfiguration.ForceUseMicrosoftExpressionCompiler)
                return Expression.Lambda<Func<IResolutionScope, object>>(expression, resolutionContext.CurrentScopeParameter).Compile();

#if IL_EMIT
            if (!expression.TryEmit(out Delegate factory, typeof(Func<IResolutionScope, object>), typeof(object),
                resolutionContext.CurrentScopeParameter))
                factory = Expression.Lambda(expression, resolutionContext.CurrentScopeParameter).CompileFast();

            return (Func<IResolutionScope, object>)factory;
#else
            return Expression.Lambda<Func<IResolutionScope, object>>(expression, resolutionContext.CurrentScopeParameter).CompileFast();
#endif
        }

Take a look at schuettecarsten@85023aa for details.

Of course, I cannot expect that you will include this library into the project, but maybe you can add a Func<LambdaExpression, Delegate> parameter to the configuration to allow usage of external expression compilers?

Sure, I have added the option in the latest pre-release package. What do you think?

Great, thank you! 👍