Lean  $LEAN_TAG$
ExpressionBuilder.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using System.Linq.Expressions;
20 
21 namespace QuantConnect.Util
22 {
23  /// <summary>
24  /// Provides methods for constructing expressions at runtime
25  /// </summary>
26  public static class ExpressionBuilder
27  {
28  /// <summary>
29  /// Constructs a selector of the form: x => x.propertyOrField where x is an instance of 'type'
30  /// </summary>
31  /// <param name="type">The type of the parameter in the expression</param>
32  /// <param name="propertyOrField">The name of the property or field to bind to</param>
33  /// <returns>A new lambda expression that represents accessing the property or field on 'type'</returns>
34  public static LambdaExpression MakePropertyOrFieldSelector(Type type, string propertyOrField)
35  {
36  var parameter = Expression.Parameter(type);
37  var property = Expression.PropertyOrField(parameter, propertyOrField);
38  var lambda = Expression.Lambda(property, parameter);
39  return lambda;
40  }
41 
42  /// <summary>
43  /// Constructs a selector of the form: x => x.propertyOrField where x is an instance of 'type'
44  /// </summary>
45  /// <typeparam name="T">The type of the parameter in the expression</typeparam>
46  /// <typeparam name="TProperty">The type of the property or field being accessed in the expression</typeparam>
47  /// <param name="propertyOrField">The name of the property or field to bind to</param>
48  /// <returns>A new lambda expression that represents accessing the property or field on 'type'</returns>
49  public static Expression<Func<T, TProperty>> MakePropertyOrFieldSelector<T, TProperty>(string propertyOrField)
50  {
51  return (Expression<Func<T, TProperty>>) MakePropertyOrFieldSelector(typeof (T), propertyOrField);
52  }
53 
54  /// <summary>
55  /// Constructs a lambda expression that accepts two parameters of type <typeparamref name="T"/> and applies
56  /// the specified binary comparison and returns the boolean result.
57  /// </summary>
58  public static Expression<Func<T, T, bool>> MakeBinaryComparisonLambda<T>(ExpressionType type)
59  {
60  if (!type.IsBinaryComparison())
61  {
62  throw new ArgumentException($"Provided ExpressionType '{type}' is not a binary comparison.");
63  }
64 
65  var left = Expression.Parameter(typeof(T), "left");
66  var right = Expression.Parameter(typeof(T), "right");
67  var body = Expression.MakeBinary(type, left, right);
68  var lambda = Expression.Lambda<Func<T, T, bool>>(body, left, right);
69  return lambda;
70  }
71 
72  /// <summary>
73  /// Determines whether or not the specified <paramref name="type"/> is a binary comparison.
74  /// </summary>
75  public static bool IsBinaryComparison(this ExpressionType type)
76  {
77  switch (type)
78  {
79  case ExpressionType.Equal:
80  case ExpressionType.NotEqual:
81  case ExpressionType.LessThan:
82  case ExpressionType.LessThanOrEqual:
83  case ExpressionType.GreaterThan:
84  case ExpressionType.GreaterThanOrEqual:
85  return true;
86 
87  default:
88  return false;
89  }
90  }
91 
92  /// <summary>
93  /// Converts the specified expression into an enumerable of expressions by walking the expression tree
94  /// </summary>
95  /// <param name="expression">The expression to enumerate</param>
96  /// <returns>An enumerable containing all expressions in the input expression</returns>
97  public static IEnumerable<Expression> AsEnumerable(this Expression expression)
98  {
99  var walker = new ExpressionWalker();
100  walker.Visit(expression);
101  return walker.Expressions;
102  }
103 
104  /// <summary>
105  /// Returns all the expressions of the specified type in the given expression tree
106  /// </summary>
107  /// <typeparam name="T">The type of expression to search for</typeparam>
108  /// <param name="expression">The expression to search</param>
109  /// <returns>All expressions of the given type in the specified expression</returns>
110  public static IEnumerable<T> OfType<T>(this Expression expression)
111  where T : Expression
112  {
113  return expression.AsEnumerable().OfType<T>();
114  }
115 
116  /// <summary>
117  /// Returns the single expression of the specified type or throws if none or more than one expression
118  /// of the specified type is contained within the expression.
119  /// </summary>
120  /// <typeparam name="T">The type of expression to search for</typeparam>
121  /// <param name="expression">The expression to search</param>
122  /// <returns>Expression of the specified type</returns>
123  public static T Single<T>(this Expression expression)
124  where T : Expression
125  {
126  return expression.AsEnumerable().OfType<T>().Single();
127  }
128 
129  /// <summary>
130  /// Returns the single expression of the specified type or throws if none or more than one expression
131  /// of the specified type is contained within the expression.
132  /// </summary>
133  /// <typeparam name="T">The type of expression to search for</typeparam>
134  /// <param name="expressions">The expressions to search</param>
135  /// <returns>Expression of the specified type</returns>
136  public static T Single<T>(this IEnumerable<Expression> expressions)
137  where T : Expression
138  {
139  return expressions.OfType<T>().Single();
140  }
141 
142  private class ExpressionWalker : ExpressionVisitor
143  {
144  public readonly HashSet<Expression> Expressions = new HashSet<Expression>();
145  public override Expression Visit(Expression node)
146  {
147  Expressions.Add(node);
148  return base.Visit(node);
149  }
150  }
151  }
152 }