日期:2014-05-17  浏览次数:20722 次

|ZYCWPF| 一个Linq.Expressions的扩展方法在4.0是可以通过的,但如何把他转为3.5的 谢谢

using System.Linq;
using System.Linq.Expressions;
    public static class LambdaExpressionExtensions
    {
        public static Expression<TFunc> And<TFunc>(this Expression<TFunc> expr1, Expression<TFunc> expr2)
        {
            if (expr1.ReturnType != typeof(bool) || expr2.ReturnType != typeof(bool))
                throw new ArgumentException("both lambda expressions must return boolean type");
            if (expr1.Parameters.Zip(expr2.Parameters, (p1, p2) => p1.Type == p2.Type).Any(x => x == false))
                throw new ArgumentException("expr1 and expr2 must have exactly the same parameters");

            var p = expr1.Parameters;
            var left = Expression.Invoke(expr1, p);
            var right = Expression.Invoke(expr2, p);
            var expr = Expression.And(left, right);
            return Expression.Lambda<TFunc>(expr, p);
        }
    }


这里在3.5有三个地方出错
1:expr1.ReturnType
2:expr1.Parameters.Zip
3:Expression.Invoke(expr1, p)

前两个没有办法解决没什么,我可以注释掉
但第三个方法是必须的

谢谢
------最佳解决方案--------------------
var left = Expression.Invoke(expr1, p.ToArray());

那个是3.5不支持协变造成的,对于System.Collections.Generic.IEnumerable<Expression>接口,需要协变到ReadOnlyCollection<ParameterExpression>,该功能只有4.0以上才提供,不过转换为数组的话,就可以用第二种重载,不需要协变了。
------其他解决方案--------------------
        public static Expression<TFunc> And<TFunc>(this?Expression<TFunc> expr1, Expression<TFunc> expr2)
        {
            if (expr1.Body.Type != typeof(bool) 
------其他解决方案--------------------
更详细请看:
http://bbs.csdn.net/topics/390303038
谢谢
------其他解决方案--------------------
 expr2.Body.Type != typeof(bool))
                throw new ArgumentException("both?lambda?expressions?must?return?boolean?type");
            if (expr1.Parameters.Zip(expr2.Parameters, (p1, p2) => p1.Type == p2.Type).Any(x => x == false))
              &n