2 using System.Collections.Generic;
8 namespace System.Linq.Expressions
10 public static class LinqExpressionsExtensions
12 public static MemberInfo GetMemberInfo<T, U>(
this Expression<Func<T, U>> expression)
14 var member = expression.Body as MemberExpression;
18 throw new ArgumentException(
"Expression is not a member access",
"expression");
22 public static Expression<Func<T, bool>> And<T>(
this Expression<Func<T, bool>> a, Expression<Func<T, bool>> b)
25 ParameterExpression p = a.Parameters[0];
27 SubstExpressionVisitor visitor =
new SubstExpressionVisitor();
28 visitor.subst[b.Parameters[0]] = p;
30 Expression body = Expression.AndAlso(a.Body, visitor.Visit(b.Body));
31 return Expression.Lambda<Func<T, bool>>(body, p);
34 public static Expression<Func<T, bool>> Or<T>(
this Expression<Func<T, bool>> a, Expression<Func<T, bool>> b)
37 ParameterExpression p = a.Parameters[0];
39 SubstExpressionVisitor visitor =
new SubstExpressionVisitor();
40 visitor.subst[b.Parameters[0]] = p;
42 Expression body = Expression.OrElse(a.Body, visitor.Visit(b.Body));
43 return Expression.Lambda<Func<T, bool>>(body, p);
46 internal class SubstExpressionVisitor :
System.Linq.Expressions.ExpressionVisitor
48 public Dictionary<Expression, Expression> subst =
new Dictionary<Expression, Expression>();
50 protected override Expression VisitParameter(ParameterExpression node)
53 if (subst.TryGetValue(node, out newValue))