// ReSharper disable once SuggestBaseTypeForParameter /// <code> /// context{i} => behavior.Invoke(context{i}, context{i+1} => previous) /// </code>> static Delegate CreateBehaviorCallDelegate(IBehavior currentBehavior, MethodInfo methodInfo, ParameterExpression outerContextParam, Delegate previous, List <Expression> expressions = null) { var body = ExpressionInfo.Call(ExpressionInfo.Constant(currentBehavior), methodInfo, outerContextParam, ExpressionInfo.Constant(previous)); var lambdaExpression = ExpressionInfo.Lambda(body, outerContextParam); expressions?.Add(lambdaExpression.ToExpression()); return(lambdaExpression.CompileFast()); }
public object CreateExpressionInfo_and_FastCompile() { //Expression<Func<Action<string>>> expr = () => a => s.SetValue(a); var aParam = Expression.Parameter(typeof(string), "a"); var expr = ExpressionInfo.Lambda( ExpressionInfo.Lambda( ExpressionInfo.Call(ExpressionInfo.Constant(_s), _setValueMethod, aParam), aParam ) ); return(expr.TryCompile <Func <Action <string> > >()); }
public void Nested_Func_using_outer_parameter() { // The same hoisted expression: //Expression<Func<string, string>> expr = a => GetS(() => a); var aParam = Expression.Parameter(typeof(string), "a"); var expr = ExpressionInfo.Lambda( ExpressionInfo.Call(GetType().GetTypeInfo().DeclaredMethods.First(m => m.Name == nameof(GetS)), ExpressionInfo.Lambda(aParam)), aParam); var f = ExpressionCompiler.TryCompile <Func <string, string> >(expr); Assert.AreEqual("a", f("a")); }
public void Can_compile_lambda_with_call_and_property() { var thisType = GetType().GetTypeInfo(); var funcExpr = ExpressionInfo.Lambda( ExpressionInfo.Call(thisType.GetMethod(nameof(GetX)), ExpressionInfo.Property(thisType.GetProperty(nameof(PropX))))); var func = ExpressionCompiler.TryCompile <Func <X> >(funcExpr); Assert.IsNotNull(func); var x = func(); Assert.IsInstanceOf <X>(x); }
public void Nested_Action_using_outer_parameter_and_closed_value() { var s = new S(); //Expression<Func<Action<string>>> expr = () => a => s.SetValue(a); var aParam = Expression.Parameter(typeof(string), "a"); var expr = ExpressionInfo.Lambda( ExpressionInfo.Lambda( ExpressionInfo.Call( ExpressionInfo.Constant(s), typeof(S).GetTypeInfo().DeclaredMethods.First(m => m.Name == nameof(S.SetValue)), aParam), aParam) ); var f = ExpressionCompiler.TryCompile <Func <Action <string> > >(expr); f()("a"); Assert.AreEqual("a", s.Value); }