Closed
Description
Now that #1089 is solved, we can include the suffix visitor I was talking about in that issue.
This is safer and more clear than using the current SuffixExtensions.Suffix
because it only applies to Expression<Func<T, object>>
instead of any object
.
It also allows me to build a list of fields (of type Expression<Func<T, object>>
) and apply a suffix to that whole list (useful for mapping multiple string fields with multiple analysers).
// put this anywhere you want
public static Expression<Func<T, object>> AppendSuffix<T>(this Expression<Func<T, object>> property, string suffix)
{
var newBody = new SuffixExprVisitor(suffix).Visit(property.Body);
return Expression.Lambda<Func<T, object>>(newBody, property.Parameters[0]);
}
/// <summary>
/// Calls <see cref="SuffixExtensions.Suffix"/> on a member expression.
/// </summary>
private class SuffixExprVisitor : ExpressionVisitor
{
readonly string Suffix;
public SuffixExprVisitor(string suffix)
{
Suffix = suffix;
}
protected override Expression VisitMember(MemberExpression node)
{
return Expression.Call(typeof(SuffixExtensions), "Suffix", null, node, Expression.Constant(Suffix));
}
protected override Expression VisitUnary(UnaryExpression node)
{
// allow rewriting child expressions
return node;
}
}
Metadata
Metadata
Assignees
Labels
No labels