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

Solution of Compute - Include - Decompile problem #53

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 44 additions & 0 deletions src/DelegateDecompiler/DecompileExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,50 @@ public static Expression Decompile(Expression expression)
return new DecompileExpressionVisitor().Visit(expression);
}

private static readonly object NULL = new object(); // for use as a dictionary key
private readonly Dictionary<object, Expression> visitedConstants;

private bool hasAnyChanges = false;
public override Expression Visit(Expression node)
{
var result = base.Visit(node);
if (result != node)
hasAnyChanges = true;
return result;
}

private DecompileExpressionVisitor(Dictionary<object, Expression> sharedVisitedConstants = null)
{
this.visitedConstants = sharedVisitedConstants ?? new Dictionary<object, Expression>();
}

protected override Expression VisitConstant(ConstantExpression node)
{
Expression result;
if (visitedConstants.TryGetValue(node.Value ?? NULL, out result))
{
return result; // avoid infinite recursion
}

if (typeof(IQueryable).IsAssignableFrom(node.Type))
{
visitedConstants.Add(node.Value ?? NULL, node);

var value = (IQueryable)node.Value;
var childVisitor = new DecompileExpressionVisitor(visitedConstants);
result = childVisitor.Visit(value.Expression);

if (childVisitor.hasAnyChanges)
{
result = Expression.Constant(value.Provider.CreateQuery(result), node.Type);
visitedConstants[node.Value ?? NULL] = result;
return result;
}
}

return node;
}

protected override Expression VisitMember(MemberExpression node)
{
if (ShouldDecompile(node.Member))
Expand Down