我有一个Expression<Func<Entity, string>>
,可以是属性或嵌套属性访问器
y => y.SearchColumn
或
y => y.SubItem.SubColumn
我正在动态构建一个表达式树,并希望像这样得到一个InvokeExpression
x => x.SearchColumn.Contains("foo");
x => x.Sub.SearchColumn.Contains("foo");
我很确定我可以解开Expression主体,然后将x ParameterExpression
部分应用到它上面,但我无法弄清楚实现这一点的神奇咒语
所以我有类似
的东西Expression<Func<Entity, string>> createContains(Expression<Func<Entity, string>> accessor) {
var stringContains = typeof(String).GetMethod("Contains", new [] { typeof(String) });
var pe = Expression.Parameter(typeof(T), "__x4326");
return Expression.Lambda<Func<Entity, bool>>(
Expression.Call(
curryExpression(accessor.Body, pe),
stringContains,
Expression.Constant("foo")
)
, pe
);
}
static Expression curryExpression(Expression from, ParameterExpression parameter) {
// this doesn't handle the sub-property scenario
return Expression.Property(parameter, ((MemberExpression) from).Member.Name);
//I thought this would work but it does not
//return Expression.Lambda<Func<Entity,string>>(from, parameter).Body;
}
修改 Here is the full thing I'm trying to do along with the error
答案 0 :(得分:3)
你需要做两件事 - 首先你可以使用相同的accessor.Body
,但是当你创建一个新参数时,它会引用不正确的参数。第二个你需要编写自定义ExpressionVisitor
,它将原始y
ParameterExpression
的所有用法替换为新创建的,因此结果表达式将被编译好。
如果您不需要创建新的ParameterExpression
,则可以使用相同的accessor.Body
并重新发送原始ParameterExpression
到新树。
所以这是我的测试工作副本,其中包含新的ParameterExpression
- https://dotnetfiddle.net/uuPVAl
代码本身
public class Program
{
public static void Main(string[] args)
{
Expression<Func<Entity, string>> parent = (y) => y.SearchColumn;
Expression<Func<Entity, string>> sub = (y) => y.Sub.SearchColumn;
var result = Wrap(parent);
Console.WriteLine(result);
result.Compile();
result = Wrap(sub);
Console.WriteLine(result);
result.Compile();
result = Wrap<Entity>((y) => y.Sub.Sub.Sub.SearchColumn);
Console.WriteLine(result);
result.Compile();
}
private static Expression<Func<T, bool>> Wrap<T>(Expression<Func<T, string>> accessor)
{
var stringContains = typeof (String).GetMethod("Contains", new[] {typeof (String)});
var pe = Expression.Parameter(typeof (T), "__x4326");
var newBody = new ParameterReplacer(pe).Visit(accessor.Body);
var call = Expression.Call(
newBody,
stringContains,
Expression.Constant("foo")
);
return Expression.Lambda<Func<T, bool>>(call, pe);
}
}
public class ParameterReplacer : ExpressionVisitor
{
private ParameterExpression _target;
public ParameterReplacer(ParameterExpression target)
{
_target = target;
}
protected override Expression VisitParameter(ParameterExpression node)
{
// here we are replacing original to a new one
return _target;
}
}
public class Entity
{
public string SearchColumn { get; set; }
public Entity Sub { get; set; }
}
PS:此示例仅在原始查询中只有一个ParameterExpression
时才有效,否则访问者应区分它们
<强>更新强>
以下是我在更新中的完整示例的工作答案 - https://dotnetfiddle.net/MXP7wE
答案 1 :(得分:1)
你只需修复一些事情:
Expression<Func<T, bool>>
。Expression.Call()
的第一个参数应为accessor.Body
。ParameterExpression
方法调用的Expression.Lambda<Func<T, bool>>()
参数应该只根据accessor
参数设置。方式:强>
Expression<Func<T, bool>> CreateContains<T>(Expression<Func<T, string>> accessor)
{
var stringContains = typeof(String).GetMethod("Contains", new[] { typeof(String) });
return Expression.Lambda<Func<T, bool>>(
Expression.Call(
accessor.Body,
stringContains,
Expression.Constant("foo")
)
, accessor.Parameters[0]
);
}