17 using System.Linq.Expressions;
73 throw new InvalidOperationException($
"The specified ExpressionType '{type}' is not a binary comparison.");
80 public ExpressionType
Type {
get; }
91 => OfType<T>.GetFunc(
Type)(left, right);
97 => OfType<T>.GetFunc(
Type);
103 => OfType<T>.GetExpression(
Type);
113 case ExpressionType.
Equal:
return this;
114 case ExpressionType.
NotEqual:
return this;
121 "The skies are falling and the oceans are rising! " +
122 "If you've made it here then this exception is the least of your worries! " +
123 $
"ExpressionType: {Type}"
132 return Type.ToString();
140 private static class OfType<T>
142 private static readonly Expression<Func<T, T, bool>> EqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.Equal);
143 private static readonly Expression<Func<T, T, bool>> NotEqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.NotEqual);
144 private static readonly Expression<Func<T, T, bool>> LessThanExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.LessThan);
145 private static readonly Expression<Func<T, T, bool>> LessThanOrEqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.LessThanOrEqual);
146 private static readonly Expression<Func<T, T, bool>> GreaterThanExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.GreaterThan);
147 private static readonly Expression<Func<T, T, bool>> GreaterThanOrEqualExpr = MakeBinaryComparisonLambdaOrNull(ExpressionType.GreaterThanOrEqual);
149 public static Expression<Func<T, T, bool>> GetExpression(ExpressionType type)
153 case ExpressionType.Equal:
return EqualExpr;
154 case ExpressionType.NotEqual:
return NotEqualExpr;
155 case ExpressionType.LessThan:
return LessThanExpr;
156 case ExpressionType.LessThanOrEqual:
return LessThanOrEqualExpr;
157 case ExpressionType.GreaterThan:
return GreaterThanExpr;
158 case ExpressionType.GreaterThanOrEqual:
return GreaterThanOrEqualExpr;
160 throw new InvalidOperationException($
"The specified ExpressionType '{type}' is not a binary comparison.");
164 private static readonly Func<T, T, bool> EqualFunc = EqualExpr?.Compile();
165 private static readonly Func<T, T, bool> NotEqualFunc = NotEqualExpr?.Compile();
166 private static readonly Func<T, T, bool> LessThanFunc = LessThanExpr?.Compile();
167 private static readonly Func<T, T, bool> LessThanOrEqualFunc = LessThanOrEqualExpr?.Compile();
168 private static readonly Func<T, T, bool> GreaterThanFunc = GreaterThanExpr?.Compile();
169 private static readonly Func<T, T, bool> GreaterThanOrEqualFunc = GreaterThanOrEqualExpr?.Compile();
171 public static Func<T, T, bool> GetFunc(ExpressionType type)
175 case ExpressionType.Equal:
return EqualFunc;
176 case ExpressionType.NotEqual:
return NotEqualFunc;
177 case ExpressionType.LessThan:
return LessThanFunc;
178 case ExpressionType.LessThanOrEqual:
return LessThanOrEqualFunc;
179 case ExpressionType.GreaterThan:
return GreaterThanFunc;
180 case ExpressionType.GreaterThanOrEqual:
return GreaterThanOrEqualFunc;
182 throw new InvalidOperationException($
"The specified ExpressionType '{type}' is not a binary comparison.");
186 private static Expression<Func<T, T, bool>> MakeBinaryComparisonLambdaOrNull(ExpressionType type)
190 return MakeBinaryComparisonLambda<T>(type);