简体   繁体   English

转换表达式 <Func<T1,bool> &gt;表达 <Func<T2,bool> 动态

[英]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个表达式 - T2T1转换,而不是用给定的结果调用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.

相关问题 在表达式中转换类型<Func<T1, bool> &gt; 表达<Func<T2, bool> &gt; (LINQ to SQL) - Convert type in Expression<Func<T1, bool>> to Expression<Func<T2, bool>> (LINQ to SQL) 如何转换表达式 <Func<T1, bool> &gt;表达 <Func<T2, bool> &gt; - How to convert Expression<Func<T1, bool>> to Expression<Func<T2, bool>> 转换表达式<func<t1, bool> &gt; 到表达式<func<t2, bool> &gt; </func<t2,></func<t1,> - Convert Expression<Func<T1, bool>> to Expression<Func<T2, bool>> 如何转换 Linq 表达式<func<object,object,bool> &gt; 表达<func<t1,t2,bool> &gt; </func<t1,t2,bool></func<object,object,bool> - How to convert Linq Expression<Func<object,object,bool>> to Expression<Func<T1,T2,bool>> 如何组合表达式 Expression <Func<T1, T2, bool> &gt; 到单个表达式<Func<T2, bool> &gt; - How to combine expressions Expression<Func<T1, T2, bool>> to a single Expression<Func<T2, bool>> 我可以动态创建一个表达式 <Func<T, bool> &gt;谓词,但我如何创建表达式 <Func<T1,T2,bool> &gt; - I can dynamically create an Expression<Func<T, bool>> predicate ,but how do i create Expression<Func<T1,T2,bool>> 转换功能 <T1,bool> 到功能 <T2,bool> - Convert Func<T1,bool> to Func<T2,bool> 如何投射表达<Func<T1,bool> &gt; 表达<Func<T2,bool> &gt;? - How cast Expression<Func<T1,bool>> to Expression<Func<T2,bool>>? 转换表达式 <Func<T,bool> &gt;到表达式 <Func<T1,bool> &gt;使T是T1的成员 - Convert an Expression<Func<T,bool>> to an Expression<Func<T1,bool>> so that T is a member of T1 通过为T引入一个常量,将表达式<Func <T,T2,bool >>转换为Expression <Func <T2,bool >> - Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM