public void VisitGreaterThan(BinaryExpression expr_) { Type expType = expr_.GetType(); Type type_ = expr_.GetType().BaseType; ExpressionType ndType = expr_.NodeType; Visit(expr_.Left); cb.GreaterThan(); Visit(expr_.Right); }
public void VisitEqual(BinaryExpression expr_) { Type expType = expr_.GetType(); Type type_ = expr_.GetType().BaseType; ExpressionType ndType = expr_.NodeType; Visit(expr_.Left); cb.EqualSign(); Visit(expr_.Right); }
public Expression WithLog(BinaryExpression exp) { var subLambda = Expression.Lambda( Expression.Convert(exp, typeof(object)), _parameters ); var subFunc = subLambda.Compile(); var result = subFunc.DynamicInvoke(_parameterValues); return(Expression.Block( Expression.Call( typeof(Debug).GetMethod("Print", new Type[] { typeof(string) }), new[] { Expression.Call( typeof(string).GetMethod("Format", new [] { typeof(string), typeof(object), typeof(object) }), Expression.Constant("Executing Rule: {0} --> {1}"), Expression.Call(Expression.Constant(exp), exp.GetType().GetMethod("ToString")), Expression.Convert(Expression.Constant(result), typeof(object)) ) } ), base.VisitBinary(exp) )); }
static void Main(string[] args) { BinaryExpression expr = Expression.Add(Expression.Constant(10), Expression.Constant(20)); Console.WriteLine(expr.GetType().FullName); Console.WriteLine(expr.Type); Console.WriteLine(expr.Left.Type); Console.WriteLine(expr.Right.Type); Console.WriteLine(expr.NodeType); Console.WriteLine(expr.Left.NodeType); Console.WriteLine(expr.Right.NodeType); // double result = (3d + 2d) / 7d; // Console.WriteLine(result); BinaryExpression expr2 = Expression.Divide( Expression.Add(Expression.Constant(3d), Expression.Constant(2d)), Expression.Constant(7d) ); Func <double> binaryDelegate = Expression.Lambda <Func <double> >(expr2).Compile(); Console.WriteLine(binaryDelegate.Invoke()); }
// // Summary: // Visits the children of the System.Linq.Expressions.BinaryExpression. // // Parameters: // node: // The expression to visit. // // Returns: // The modified expression, if it or any subexpression was modified; otherwise, // returns the original expression. protected override Expression VisitBinary(BinaryExpression node) { Console.WriteLine("VisitBinary:"); Console.WriteLine('\t' + node.GetType().ToString()); Console.WriteLine('\t' + node.ToString()); return(base.VisitBinary(node)); }
protected override void Visit(BinaryExpression node) { InsideTag("BinaryExpression", () => { this.InsideTag("Left", () => { this.VisitNode(node.Left); }); this.InsideTag("Op", () => { Out.WriteLine(node.GetType().ToString()); }); this.InsideTag("Right", () => { this.VisitNode(node.Right); }); }); }
private static QueryFilter ParseComparison <T>(BinaryExpression binaryExpression, ParameterExpression parameter) { var leftIsConstant = ExpressionHelper.TryConvertToConstant(binaryExpression.Left, out var leftConstant); var rightIsConstant = ExpressionHelper.TryConvertToConstant(binaryExpression.Right, out var rightConstant); var leftIsProperty = TryConvertToMember(binaryExpression.Left, out var leftMember); var rightIsProperty = TryConvertToMember(binaryExpression.Right, out var rightMember); if (!((leftIsConstant ^ rightIsConstant) || (leftIsProperty || rightIsProperty))) { throw new Exception($"{nameof(Parse)}: Expressions of type {binaryExpression.GetType()} with {nameof(binaryExpression.NodeType)}={binaryExpression.NodeType} must contain an expression that compiles to a constant value and a member expression. Found (sub-)expression: '{binaryExpression}'."); } var constantValue = leftIsConstant ? leftConstant : rightConstant; var memberExpression = leftIsConstant ? rightMember : leftMember; var property = QueryInfo.EncodeMemberExpression(memberExpression, parameter); var constant = EncodeConstant(constantValue, constantValue.GetType()); switch (binaryExpression.NodeType) { case ExpressionType.GreaterThan: return(GreaterThan(property, constant)); case ExpressionType.GreaterThanOrEqual: return(GreaterThanEqual(property, constant)); case ExpressionType.LessThan: return(LessThan(property, constant)); case ExpressionType.LessThanOrEqual: return(LessThanEqual(property, constant)); case ExpressionType.Equal: return(Exact(property, constant)); default: throw new Exception($"{nameof(Parse)}: Expressions of type {binaryExpression.GetType()} with {nameof(binaryExpression.NodeType)}={binaryExpression.NodeType} are not supported yet. Found (sub-)expression: {binaryExpression}"); } }
private static int PrintBinary(BinaryExpression expression) { Write("ToString: " + expression); Write("GetType: " + expression.GetType()); Write("Conversion: " + expression.Conversion); Write("IsLifted: " + expression.IsLifted); Write("IsLiftedToNull: " + expression.IsLiftedToNull); Write("Left: " + expression.Left); PrintUnkwnown(expression.Left); Write("Method: " + expression.Method); Write("NodeType: " + expression.NodeType); Write("Right: " + expression.Right); PrintUnkwnown(expression.Right); Write("Type: " + expression.Type); return(0); }
internal static bool IsStringConcat(BinaryExpression binaryExpression) { var methodProperty = binaryExpression.GetType().GetProperty("Method"); if (methodProperty == null) { return false; } var method = methodProperty.GetValue(binaryExpression) as MethodInfo; if (method == null) { return false; } return method.DeclaringType == typeof(string) && method.Name == nameof(string.Concat); }
public Expression WithLog(BinaryExpression exp) { return(Expression.Block( Expression.Call( typeof(Debug).GetMethod("Print", new Type[] { typeof(string) }), new[] { Expression.Call( typeof(string).GetMethod("Format", new [] { typeof(string), typeof(object), typeof(object) }), Expression.Constant("Executing Rule: {0} --> {1}"), Expression.Call(Expression.Constant(exp), exp.GetType().GetMethod("ToString")), Expression.Convert( exp, typeof(object) ) ) } ), base.VisitBinary(exp) )); }
protected override Expression VisitBinary(BinaryExpression node) { bool isChecked = false; switch (node.NodeType) { case ExpressionType.AddAssignChecked: case ExpressionType.AddChecked: case ExpressionType.ConvertChecked: case ExpressionType.MultiplyAssignChecked: case ExpressionType.MultiplyChecked: case ExpressionType.SubtractAssignChecked: case ExpressionType.SubtractChecked: { isChecked = true; break; } } if (isChecked) { this._writer.Write("checked( "); } this.Visit(node.Left); switch (node.NodeType) { case ExpressionType.Add: case ExpressionType.AddChecked: { this._writer.Write(" + "); break; } case ExpressionType.AddAssign: case ExpressionType.AddAssignChecked: { this._writer.Write(" += "); break; } case ExpressionType.And: { this._writer.Write(" & "); break; } case ExpressionType.AndAlso: { this._writer.Write(" && "); break; } case ExpressionType.AndAssign: { this._writer.Write(" &= "); break; } case ExpressionType.Assign: { this._writer.Write(" = "); break; } case ExpressionType.ArrayIndex: { this._writer.Write("[ "); break; } case ExpressionType.Coalesce: { this._writer.Write(" ?? "); break; } case ExpressionType.Divide: { this._writer.Write(" / "); break; } case ExpressionType.DivideAssign: { this._writer.Write(" /= "); break; } case ExpressionType.Equal: { this._writer.Write(" == "); break; } case ExpressionType.ExclusiveOr: { this._writer.Write(" ^ "); break; } case ExpressionType.ExclusiveOrAssign: { this._writer.Write(" ^= "); break; } case ExpressionType.GreaterThan: { this._writer.Write(" > "); break; } case ExpressionType.GreaterThanOrEqual: { this._writer.Write(" >= "); break; } case ExpressionType.LeftShift: { this._writer.Write(" << "); break; } case ExpressionType.LeftShiftAssign: { this._writer.Write(" <<= "); break; } case ExpressionType.LessThan: { this._writer.Write(" < "); break; } case ExpressionType.LessThanOrEqual: { this._writer.Write(" <= "); break; } case ExpressionType.Modulo: { this._writer.Write(" % "); break; } case ExpressionType.ModuloAssign: { this._writer.Write(" %= "); break; } case ExpressionType.Multiply: case ExpressionType.MultiplyChecked: { this._writer.Write(" * "); break; } case ExpressionType.MultiplyAssign: case ExpressionType.MultiplyAssignChecked: { this._writer.Write(" *= "); break; } case ExpressionType.NotEqual: { this._writer.Write(" != "); break; } case ExpressionType.Or: { this._writer.Write(" | "); break; } case ExpressionType.OrAssign: { this._writer.Write(" |= "); break; } case ExpressionType.OrElse: { this._writer.Write(" || "); break; } case ExpressionType.Power: { this._writer.Write(" `pow` "); break; } case ExpressionType.PowerAssign: { this._writer.Write(" `pow`= "); break; } case ExpressionType.RightShift: { this._writer.Write(" >> "); break; } case ExpressionType.RightShiftAssign: { this._writer.Write(" >>= "); break; } case ExpressionType.Subtract: case ExpressionType.SubtractChecked: { this._writer.Write(" - "); break; } case ExpressionType.SubtractAssign: case ExpressionType.SubtractAssignChecked: { this._writer.Write(" -= "); break; } default: { throw new NotSupportedException(String.Format(CultureInfo.CurrentCulture, "Binary operation {0}(NodeType:{1}) is not supported. Expression tree:{2}", node.GetType().Name, node.NodeType, node)); } } this.Visit(node.Right); if (isChecked) { this._writer.Write(" )"); } else if (node.NodeType == ExpressionType.ArrayIndex) { this._writer.Write(" ]"); } return(node); }
public void Visit(BinaryExpression expression) { expression.Left.Accept(this); _aggregate.Append($" {expression.GetType().GetCustomAttribute<OperatorAttribute>().OperatorSign} "); expression.Right.Accept(this); }
private void VisitBinaryArithmetic(BinaryExpression n) { var lhs = CheckSubTree(n.Left); var rhs = CheckSubTree(n.Right); if (lhs.IsNumeric && rhs.IsNumeric) { _lastSeenType = n.InternalType = Supertype(lhs, rhs); } else if (lhs.CilType == typeof(DateTime) && rhs.CilType == typeof(DateTime)) { _lastSeenType = n.InternalType = new TypeSpan(); } else { ReportError(n.Location, "Invalid operands for operation {0}. Got types '{1}' and '{2}'.", n.GetType().ToString(), TypeToFriendlyName(lhs), TypeToFriendlyName(rhs)); } }