[英]Convert Expression<Func<T1,bool>> to Expression<Func<T2,bool> dynamically
I cannot find the way to convert from Expression<Func<T1,bool>> to Expression<Func<T2,bool>>. 我找不到从Expression <Func <T1,bool >>转换为Expression <Func <T2,bool >>的方法。 Since Im using a lot of reflection, in fact, what I really need is a method which takes a type parameter and performs the conversion.
因为我使用了大量的反射,实际上,我真正需要的是一种采用类型参数并执行转换的方法。
public object Convert(Expression<Func<T1,bool>> expr, Type t);
T2 is derived from T1 T2来自T1
public class T1 {
int FamilyId {get; set;}
}
public class T2 : T1 {
... other properties
}
I am defining a filter expression on the base class 我在基类上定义了一个过滤器表达式
Expression<Func<T1,bool>> filter = p => p.FamilyId == [some value]
that i want to apply to a List<T2> 我想申请List <T2>
Is this what you're looking for? 这是你在找什么? There are two flavors of the method: the first lets you pass in the new input type as an argument;
该方法有两种形式:第一种允许您将新输入类型作为参数传递; the second lets you pass in the input type as a generic parameter and get a strongly typed LambdaExpression.
第二个允许您将输入类型作为泛型参数传递,并获得强类型的LambdaExpression。
public static LambdaExpression ChangeInputType<T, TResult>(Expression<Func<T, TResult>> expression, Type newInputType)
{
if (!typeof(T).IsAssignableFrom(newInputType))
throw new Exception(string.Format("{0} is not assignable from {1}.", typeof(T), newInputType));
var beforeParameter = expression.Parameters.Single();
var afterParameter = Expression.Parameter(newInputType, beforeParameter.Name);
var visitor = new SubstitutionExpressionVisitor(beforeParameter, afterParameter);
return Expression.Lambda(visitor.Visit(expression.Body), afterParameter);
}
public static Expression<Func<T2, TResult>> ChangeInputType<T1, T2, TResult>(Expression<Func<T1, TResult>> expression)
{
if (!typeof(T1).IsAssignableFrom(typeof(T2)))
throw new Exception(string.Format("{0} is not assignable from {1}.", typeof(T1), typeof(T2)));
var beforeParameter = expression.Parameters.Single();
var afterParameter = Expression.Parameter(typeof(T2), beforeParameter.Name);
var visitor = new SubstitutionExpressionVisitor(beforeParameter, afterParameter);
return Expression.Lambda<Func<T2, TResult>>(visitor.Visit(expression.Body), afterParameter);
}
public class SubstitutionExpressionVisitor : ExpressionVisitor
{
private Expression before, after;
public SubstitutionExpressionVisitor(Expression before, Expression after)
{
this.before = before;
this.after = after;
}
public override Expression Visit(Expression node)
{
return node == before ? after : base.Visit(node);
}
}
What your asking for is very unwise. 你的要求是非常不明智的。 How would the compiler ever know if T1 is can be converted to T2?
编译器如何知道T1是否可以转换为T2? Seems like asking for awful run-time errors even if it is possible.*
似乎即使有可能也要求可怕的运行时错误。*
(*I don't think its possible since you are trying to combine refection with nested generic types.) (*我认为它不可能,因为你试图将refection与嵌套泛型类型结合起来。)
It looks like you want to combine 2 expressions - T2
to T1
conversion and than call to expr
with given result. 看起来你想要组合2个表达式 -
T2
到T1
转换,而不是用给定的结果调用expr
。
This question discusses Combining two expressions (Expression<Func<T, bool>>) in general. 这个问题一般讨论组合两个表达式(Expression <Func <T,bool >>) 。 For your case I think you need Expression.Call to construct conversion expression and than again to call original expression with result of conversion.
对于您的情况,我认为您需要Expression.Call来构建转换表达式,而不是再次使用转换结果调用原始表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.