WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
LinqExpressions.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
7 
8 namespace System.Linq.Expressions
9 {
10  public static class LinqExpressionsExtensions
11  {
12  public static MemberInfo GetMemberInfo<T, U>(this Expression<Func<T, U>> expression)
13  {
14  var member = expression.Body as MemberExpression;
15  if (member != null)
16  return member.Member;
17 
18  throw new ArgumentException("Expression is not a member access", "expression");
19  }
20 
21 
22  public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> a, Expression<Func<T, bool>> b)
23  {
24 
25  ParameterExpression p = a.Parameters[0];
26 
27  SubstExpressionVisitor visitor = new SubstExpressionVisitor();
28  visitor.subst[b.Parameters[0]] = p;
29 
30  Expression body = Expression.AndAlso(a.Body, visitor.Visit(b.Body));
31  return Expression.Lambda<Func<T, bool>>(body, p);
32  }
33 
34  public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> a, Expression<Func<T, bool>> b)
35  {
36 
37  ParameterExpression p = a.Parameters[0];
38 
39  SubstExpressionVisitor visitor = new SubstExpressionVisitor();
40  visitor.subst[b.Parameters[0]] = p;
41 
42  Expression body = Expression.OrElse(a.Body, visitor.Visit(b.Body));
43  return Expression.Lambda<Func<T, bool>>(body, p);
44  }
45 
46  internal class SubstExpressionVisitor : System.Linq.Expressions.ExpressionVisitor
47  {
48  public Dictionary<Expression, Expression> subst = new Dictionary<Expression, Expression>();
49 
50  protected override Expression VisitParameter(ParameterExpression node)
51  {
52  Expression newValue;
53  if (subst.TryGetValue(node, out newValue))
54  {
55  return newValue;
56  }
57  return node;
58  }
59  }
60 
61  }
62 }