How to "unquote" when creating an expression tree from lambda?

Suppose I have some function cthat returns Expression:

Func<int, Expression<Func<int>>> c = (int a) => () => a + 3;

Now I want to create another one Expression, but during its creation I would like to call a function cand insert its result as part of a new expression:

Expression<Func<int>> d = () => 2 + c(3);

I cannot do this because it will interpret c(3)as a function call that will be converted to an expression, and I will get an error that I cannot add intandExpression<Func<int>>

I would like to dmake a difference:

(Expression<Func<int>>)( () => 2 + 3 + 3 )

I am also interested in having this work on more complex expressions, not just this toy example.

How would you do this in C #?

, CLR, # ?


:

Func<int, Expression<Func<int>>> c = (int a) => () => a*(a + 3);
Expression<Func<int, int>> d = (x) => 2 + c(3 + x);

3+x , c .


, #, lambda Expression const . , "test" string literal "test ${a+b} other", # .

, :

CLR , , , ?

- , , , , .


, F # "" "unquote" () :

https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/code-quotations

+4
3

( ):

static class Extensions {
    public static TResult FakeInvoke<TResult>(this Delegate instance, params object[] parameters)
    {
        // this is not intended to be called directly
        throw new NotImplementedException();
    }

    public static TExpression Unwrap<TExpression>(this TExpression exp) where TExpression : Expression {
        return (TExpression) new FakeInvokeVisitor().Visit(exp);
    }

    class FakeInvokeVisitor : ExpressionVisitor {
        protected override Expression VisitMethodCall(MethodCallExpression node) {
            // replace FakeInvoke call
            if (node.Method.Name == "FakeInvoke") {
                // first obtain reference to method being called (so, for c.FakeInvoke(...) that will be "c")
                var func = (Delegate)Expression.Lambda(node.Arguments[0]).Compile().DynamicInvoke();
                // explore method argument names and types
                var argumentNames = new List<string>();
                var dummyArguments = new List<object>();
                foreach (var arg in func.Method.GetParameters()) {
                    argumentNames.Add(arg.Name);
                    // create default value for each argument
                    dummyArguments.Add(arg.ParameterType.IsValueType ? Activator.CreateInstance(arg.ParameterType) : null);
                }
                // now, invoke function with default arguments to obtain expression (for example, this one () => a*(a + 3)).
                // all arguments will have default value (0 in this case), but they are not literal "0" but a reference to "a" member with value 0
                var exp = (Expression) func.DynamicInvoke(dummyArguments.ToArray());
                // this is expressions representing what we passed to FakeInvoke (for example expression (x + 3))
                var argumentExpressions = (NewArrayExpression)node.Arguments[1];
                // now invoke second visitor
                exp = new InnerFakeInvokeVisitor(argumentExpressions, argumentNames.ToArray()).Visit(exp);
                return ((LambdaExpression)exp).Body;
            }
            return base.VisitMethodCall(node);
        }
    }

    class InnerFakeInvokeVisitor : ExpressionVisitor {
        private readonly NewArrayExpression _args;
        private readonly string[] _argumentNames;
        public InnerFakeInvokeVisitor(NewArrayExpression args, string[] argumentNames) {
            _args =  args;
            _argumentNames = argumentNames;
        }
        protected override Expression VisitMember(MemberExpression node) {
            // if that is a reference to one of our arguments (for example, reference to "a")
            if (_argumentNames.Contains(node.Member.Name)) {
                // find related expression
                var idx = Array.IndexOf(_argumentNames, node.Member.Name);
                var argument = _args.Expressions[idx];
                var unary = argument as UnaryExpression;
                // and replace it. So "a" is replaced with expression "x + 3"
                return unary?.Operand ?? argument;
            }
            return base.VisitMember(node);
        }
    }
}

:

Func<int, Expression<Func<int>>> c = (int a) => () => a * (a + 3);
Expression<Func<int, int>> d = (x) => 2 + c.FakeInvoke<int>(3 + x);
d = d.Unwrap(); // this is now "x => (2 + ((3 + x) * ((3 + x) + 3)))"

:

Func<int, Expression<Func<int>>> c = (int a) => () => a + 3;
Expression<Func<int>> d = () => 2 + c.FakeInvoke<int>(3);
d = d.Unwrap(); // this is now "() => 2 + (3 + 3)

:

Func<int, int, Expression<Func<int>>> c = (int a, int b) => () => a * (a + 3) + b;
Expression<Func<int, int>> d = (x) => 2 + c.FakeInvoke<int>(3 + x, x + 5);
d = d.Unwrap(); // "x => (2 + (((3 + x) * ((3 + x) + 3)) + (x + 5)))"

, FakeInvoke ( ). , , , FakeInvoke, :

public static TResult FakeInvoke<TArg, TResult>(this Func<TArg, Expression<Func<TResult>>> instance, TArg argument) {
        // this is not intended to be called directly
    throw new NotImplementedException();
}

( NewArrayExpression), . :

Expression<Func<int, int>> d = (x) => 2 + c.FakeInvoke(3 + x); // this is type-safe now, you cannot pass non-integer as "3+x", nor you can pass more or less arguments than required.
+1

, lambdas, , , non-public (System.Runtime.CompilerServices.Closure?), , lambda . .


Evk :

Expression<Func<int, int>> c = (int a) => a * (a + 3);
var d = Extensions.Splice<Func<int, int>>((x) => 2 + c.Embed(3 + x));

// d is now x => (2 + ((3 + x) * ((3 + x) + 3))) expression

public static class Extensions
{
    public static T Embed<T>(this Expression<Func<T>> exp) { throw new Exception("Should not be executed"); }
    public static T Embed<A, T>(this Expression<Func<A, T>> exp, A a) { throw new Exception("Should not be executed"); }
    public static T Embed<A, B, T>(this Expression<Func<A, B, T>> exp, A a, B b) { throw new Exception("Should not be executed"); }
    public static T Embed<A, B, C, T>(this Expression<Func<A, B, C, T>> exp, A a, B b, C c) { throw new Exception("Should not be executed"); }
    public static T Embed<A, B, C, D, T>(this Expression<Func<A, B, C, D, T>> exp, A a, B b, C c) { throw new Exception("Should not be executed"); }

    public static Expression<T> Splice<T>(Expression<T> exp)
    {
        return new SplicingVisitor().Visit(exp) as Expression<T>;
    }
    class SplicingVisitor : ExpressionVisitor
    {
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            if (node.Method.Name == "Embed")
            {
                var mem = node.Arguments[0] as MemberExpression;

                var getterLambda = Expression.Lambda<Func<object>>(mem, new ParameterExpression[0]);
                var lam = getterLambda.Compile().DynamicInvoke() as LambdaExpression;

                var parameterMapping = lam.Parameters.Select((p, index) => new
                {
                    FormalParameter = p,
                    ActualParameter = node.Arguments[index+1]
                }).ToDictionary(o => o.FormalParameter, o => o.ActualParameter);

                return new ParameterReplacerVisitor(parameterMapping).Visit(lam.Body);
            }
            return base.VisitMethodCall(node);
        }
    }
    public class ParameterReplacerVisitor : ExpressionVisitor
    {
        private Dictionary<ParameterExpression, Expression> parameterMapping;
        public ParameterReplacerVisitor(Dictionary<ParameterExpression, Expression> parameterMapping)
        {
            this.parameterMapping = parameterMapping;
        }

        protected override Expression VisitParameter(ParameterExpression node)
        {
            if(parameterMapping.ContainsKey(node))
            {
                return parameterMapping[node];
            }
            return base.VisitParameter(node);
        }
    }
}
0

LinqKit, , AsExpandable() . , , EF.

(Person - EF Code) -

var ctx = new Test();

Expression<Func<Person, bool>> ageFilter = p => p.Age < 30;

var filtered = ctx.People.AsExpandable()
    .Where(p => ageFilter.Invoke(p) && p.Name.StartsWith("J"));
Console.WriteLine( $"{filtered.Count()} people meet the criteria." );
-1

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


All Articles