转换表达式<func <fromtype>&gt;表达式<func <totype>&gt; </func <totype> </func <fromtype>

时间:2011-08-13 14:24:25

标签: .net linq expression-trees

如何制作通用辅助方法,将Func中使用的类型从表达式中的一种类型转换为另一种类型

我有Expression<Func<IEmployee, bool>>,我想将其转换为

Expression<Func<Employee, bool>>.

第二个Type始终实现第一个Type。通用解决方案是我想要实现的目标。

修改

我已将问题编辑得更清楚。

2 个答案:

答案 0 :(得分:4)

好吧,您可以创建一个表达式,然后将其参数转换为原始表达式:

Expression<Func<IEmployee, bool>> source = ...

var param = Expression.Parameter(typeof(Employee));

// Types the argument as the type expected by the source expression
// and then forwards it...
var invocationExpr = Expression.Invoke
                     (source, Expression.TypeAs(param, typeof(IEmployee))); 

var result = Expression.Lambda<Func<Employee, bool>>(invocationExpr, param);

如果提供程序不支持调用表达式,则可能需要更多 用于替换源表达式中的参数的复杂解决方案。

编辑:好的,既然你说你的提供者不喜欢结果表达式,这里有一个替代例子。这是真正粗略切割参数替换器的样子(我现在只是将其作为样本编写),但它应该可以正常用于您的目的。

public static class ParameterReplacer
{
    // Produces an expression identical to 'expression'
    // except with 'source' parameter replaced with 'target' parameter.     
    public static Expression<TOutput> Replace<TInput, TOutput>
                 (Expression<TInput> expression,
                  ParameterExpression source,
                  ParameterExpression target)
    {
        return new ParameterReplacerVisitor<TOutput>(source, target)
                  .VisitAndConvert(expression);
    }

    private class ParameterReplacerVisitor<TOutput> : ExpressionVisitor
    {
        private ParameterExpression _source;
        private ParameterExpression _target;

        public ParameterReplacerVisitor
              (ParameterExpression source, ParameterExpression target)
        {
            _source = source;
            _target = target;
        }

        internal Expression<TOutput> VisitAndConvert<T>(Expression<T> root)
        {
            return (Expression<TOutput>)VisitLambda(root);
        }

        protected override Expression VisitLambda<T>(Expression<T> node)
        {
            // Leave all parameters alone except the one we want to replace.
            var parameters = node.Parameters.Select
                             (p => p == _source ? _target : p);

            return Expression.Lambda<TOutput>(Visit(node.Body), parameters);
        }

        protected override Expression VisitParameter(ParameterExpression node)
        {
            // Replace the source with the target, visit other params as usual.
            return node == _source ? _target : base.VisitParameter(node);
        }
    }
}

然后将其用作:

Expression<Func<IEmployee, bool>> expression = ...

var result = ParameterReplacer.Replace
                <Func<IEmployee, bool>, Func<Employee, bool>>
                (expression,
                 expression.Parameters.Single(), 
                 Expression.Parameter(typeof(Employee));

答案 1 :(得分:3)

如果第二种类型总是继承或实现第一种类型,则可以使用

Func<TOut, bool> Convert<TIn, TOut>(Func<TIn, bool> f) where TOut : TIn
{
    return (TOut x) => f((TIn)x);
}

如果没有这种关系,你就不能使用泛型。

相关问题