public static void Assert(Expression<Func<bool>> predicate) { Visitor visitor = new Visitor(); Expression<Func<bool>> wrappedExpr = (Expression<Func<bool>>)visitor.Visit(predicate); Func<bool> wrappedFunc = wrappedExpr.Compile(); if (!wrappedFunc()) throw ExceptionFactory.Make(visitor.Captures); }
protected override Expression VisitBinary(BinaryExpression node) { Expression left = node.Left; Expression right = node.Right; Visitor leftVisitor = new Visitor(); Visitor rightVisitor = new Visitor(); BinaryExpression binaryExpression = Expression.MakeBinary(node.NodeType, leftVisitor.Visit(left), rightVisitor.Visit(right)); _captureNode.AddChild(leftVisitor.CaptureNode); _captureNode.AddChild(rightVisitor.CaptureNode); return WrapWithCapture(binaryExpression, node); }
protected override Expression VisitMethodCall(MethodCallExpression node) { Expression instance = node.Object; Visitor instanceVisitor = new Visitor(); List<Visitor> argumentVisitors = node.Arguments.Select(_ => new Visitor()).ToList(); Expression[] argumentExpressions = argumentVisitors.Zip(node.Arguments, (v, a) => v.Visit(a)).ToArray(); MethodCallExpression callExpression = node.Object == null // static ? Expression.Call(node.Method, argumentExpressions) // instance : Expression.Call(instanceVisitor.Visit(instance), node.Method, argumentExpressions); if (node.Object != null) _captureNode.AddChild(instanceVisitor.CaptureNode); foreach (Visitor visitor in argumentVisitors) _captureNode.AddChild(visitor.CaptureNode); return WrapWithCapture(callExpression, node); }
protected override Expression VisitUnary(UnaryExpression node) { Expression operand = node.Operand; Visitor visitor = new Visitor(); UnaryExpression unaryExpression = Expression.MakeUnary(node.NodeType, visitor.Visit(operand), operand.Type); _captureNode.AddChild(visitor.CaptureNode); return WrapWithCapture(unaryExpression, node); }