Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve invocation support #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/LinqKit.Core/ExpressionExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ protected LambdaExpression EvaluateTarget(Expression target)
}
}

var lambda = target.EvaluateExpression() as LambdaExpression;

if (lambda == null)
{
throw new InvalidOperationException($"Invoke cannot evaluate LambdaExpression from '{target}'. Ensure that your function/property/member returns LambdaExpression");
}

return lambda;
return target.EvaluateExpression() as LambdaExpression;
}

/// <summary>
Expand All @@ -53,6 +46,10 @@ protected override Expression VisitInvocation(InvocationExpression iv)
var target = iv.Expression;

var lambda = EvaluateTarget(target);
if (lambda == null)
{
return base.VisitInvocation(iv);
}

var body = ExpressionReplacer.GetBody(lambda, iv.Arguments);

Expand Down Expand Up @@ -118,6 +115,11 @@ protected override Expression VisitMethodCall(MethodCallExpression m)
var target = m.Arguments[0];
var lambda = EvaluateTarget(target);

if (lambda == null)
{
throw new InvalidOperationException($"Invoke cannot evaluate LambdaExpression from '{target}'. Ensure that your function/property/member returns LambdaExpression");
}

var replaceVars = new Dictionary<Expression, Expression>();
for (int i = 0; i < lambda.Parameters.Count; i++)
{
Expand All @@ -129,6 +131,25 @@ protected override Expression VisitMethodCall(MethodCallExpression m)
return Visit(body);
}

if (m.Method.Name == nameof(Action.Invoke)
&& m.Method.DeclaringType.GetTypeInfo().IsSubclassOf(typeof(Delegate)))
{
var lambda = EvaluateTarget(m.Object);

if (lambda != null)
{
var replaceVars = new Dictionary<Expression, Expression>();
for (int i = 0; i < lambda.Parameters.Count; i++)
{
replaceVars.Add(lambda.Parameters[i], Visit(m.Arguments[i]));
}

var body = ExpressionReplacer.Replace(lambda.Body, replaceVars);

return Visit(body);
}
}

if (GetExpandLambda(m.Method, out var methodLambda))
{
var replaceVars = new Dictionary<Expression, Expression>();
Expand Down
62 changes: 62 additions & 0 deletions tests/LinqKit.Tests.Net452/ExpressionExpanderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,68 @@ public void ExpressionExpander_Expression_Block()
Assert.Equal(lambda.Invoke(42), expandedLambda.Invoke(42));
}

[Fact]
public void ExpressionExpander_Expression_InvokeExpressionRemoved()
{
Expression<Func<object, object>> lambda = o => o;

var expandedLambda = Linq.Expr((object o) => lambda.Invoke(o))
.Expand();
Assert.Equal(ExpressionType.Parameter, expandedLambda.Body.NodeType);
Assert.Equal(lambda.ToString(), expandedLambda.ToString());
Assert.Equal(lambda.Invoke(42), expandedLambda.Invoke(42));
}

[Fact]
public void ExpressionExpander_Expression_CompileAndInvokeExpressionRemoved()
{
Expression<Func<object, object>> lambda = o => o;

var expandedLambda = Linq.Expr((object o) => lambda.Compile()(o))
.Expand();
Assert.Equal(ExpressionType.Parameter, expandedLambda.Body.NodeType);
Assert.Equal(lambda.ToString(), expandedLambda.ToString());
Assert.Equal(lambda.Invoke(42), expandedLambda.Invoke(42));
}

[Fact]
public void ExpressionExpander_Expression_CompileAndInvokeOnExpressionRemoved()
{
Expression<Func<object, object>> lambda = o => o;

var expandedLambda = Linq.Expr((object o) => lambda.Compile().Invoke(o))
.Expand();
Assert.Equal(ExpressionType.Parameter, expandedLambda.Body.NodeType);
Assert.Equal(lambda.ToString(), expandedLambda.ToString());
Assert.Equal(lambda.Invoke(42), expandedLambda.Invoke(42));
}

[Fact]
public void ExpressionExpander_Expression_InvokeDelegate()
{
Func<object, string> func = o => o.ToString();

Expression<Func<object, string>> lambda = o => func(o);

var expandedLambda = Linq.Expr((object o) => lambda.Invoke(o))
.Expand();
Assert.Equal(lambda.ToString(), expandedLambda.ToString());
Assert.Equal(lambda.Invoke(42), expandedLambda.Invoke(42));
}

[Fact]
public void ExpressionExpander_Expression_InvokeOnDelegate()
{
Func<object, string> func = o => o.ToString();

Expression<Func<object, string>> lambda = o => func.Invoke(o);

var expandedLambda = Linq.Expr((object o) => lambda.Invoke(o))
.Expand();
Assert.Equal(lambda.ToString(), expandedLambda.ToString());
Assert.Equal(lambda.Invoke(42), expandedLambda.Invoke(42));
}

[Fact]
public void ExpressionExpander_Expression_Throw()
{
Expand Down