I cannot find the way to convert from Expression<Func<T1,bool>> to 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
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>
It looks like you want to combine 2 expressions -
T2
toT1
conversion and than call toexpr
with given result.This question discusses Combining two expressions (Expression<Func<T, bool>>) in general. For your case I think you need Expression.Call to construct conversion expression and than again to call original expression with result of conversion.
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.
What your asking for is very unwise. How would the compiler ever know if T1 is can be converted to 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.)