Mutating the expression tree of a predicate to target another type

It seems you’re generating the parameter expression twice, in VisitMember() here:

var converted = Expression.MakeMemberAccess(
    base.Visit(node.Expression),
    activeRecordType.GetProperty(node.Member.Name));

…since base.Visit() will end up in VisitParameter I imagine, and in GetMany() itself:

var lambda = Expression.Lambda<Func<ActiveRecord.Widget, bool>>(
    visitor.Visit(predicate.Body),
    predicate.Parameters.Select(p => visitor.Visit(p));

If you’re using a ParameterExpression in the body, it has to be the same instance (not just the same type and name) as the one declared for the Lambda.
I’ve had problems before with this kind of scenario, though I think the result was that I just wasn’t able to create the expression, it would just throw an exception. In any case you might try reusing the parameter instance see if it helps.

Leave a Comment