public override object VisitUsingStatement(UsingStatement usingStatement, object data)
        {
            // using (new expr) { stmts; }
            //
            // emulate with
            //      object _dispose;
            //      try
            //      {
            //          _dispose = new expr;
            //
            //          stmts;
            //      }
            //      finally
            //      {
            //          if (((_dispose != null)
            //              && (typeof(System.IDisposable).IsInstanceOfType(_dispose) == true)))
            //          {
            //              ((System.IDisposable)(_dispose)).Dispose();
            //          }
            //      }
            //

            usingId++; // in case nested using() statements
            string name = "_dispose" + usingId.ToString();

            CodeVariableDeclarationStatement disposable = new CodeVariableDeclarationStatement("System.Object", name, new CodePrimitiveExpression(null));

            AddStmt(disposable);

            CodeTryCatchFinallyStatement tryStmt = new CodeTryCatchFinallyStatement();

            CodeVariableReferenceExpression left1 = new CodeVariableReferenceExpression(name);

            codeStack.Push(NullStmtCollection); // send statements to nul Statement collection
            CodeExpression right1 = (CodeExpression)usingStatement.ResourceAcquisition.AcceptVisitor(this, data);
            codeStack.Pop();

            CodeAssignStatement assign1 = new CodeAssignStatement(left1, right1);

            tryStmt.TryStatements.Add(assign1);
            tryStmt.TryStatements.Add(new CodeSnippetStatement());

            codeStack.Push(tryStmt.TryStatements);
            usingStatement.EmbeddedStatement.AcceptChildren(this, data);
            codeStack.Pop();

            CodeMethodInvokeExpression isInstanceOfType = new CodeMethodInvokeExpression(new CodeTypeOfExpression(typeof(IDisposable)), "IsInstanceOfType", new CodeExpression[] { left1 });

            CodeConditionStatement if1 = new CodeConditionStatement();
            if1.Condition = new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(left1, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)),
                                                             CodeBinaryOperatorType.BooleanAnd,
                                                             new CodeBinaryOperatorExpression(isInstanceOfType, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(true)));
            if1.TrueStatements.Add(new CodeMethodInvokeExpression(new CodeCastExpression(typeof(IDisposable),left1), "Dispose", new CodeExpression[] { }));

            tryStmt.FinallyStatements.Add(if1);

            // Add Statement to Current Statement Collection
            AddStmt(tryStmt);

            return null;
        }
 public override sealed object VisitUsingStatement(UsingStatement usingStatement, object data)
 {
     this.BeginVisit(usingStatement);
     object result = this.TrackedVisitUsingStatement(usingStatement, data);
     this.EndVisit(usingStatement);
     return result;
 }
 public virtual object TrackedVisitUsingStatement(UsingStatement usingStatement, object data)
 {
     return base.VisitUsingStatement(usingStatement, data);
 }
 public override object VisitUsingStatement(UsingStatement usingStatement, object data)
 {
     // uses LocalVariableDeclaration, we just have to put the end location on the stack
     if (usingStatement.EmbeddedStatement.EndLocation.IsEmpty) {
         return base.VisitUsingStatement(usingStatement, data);
     } else {
         endLocationStack.Push(usingStatement.EmbeddedStatement.EndLocation);
         base.VisitUsingStatement(usingStatement, data);
         endLocationStack.Pop();
         return null;
     }
 }
 public virtual object VisitUsingStatement(UsingStatement usingStatement, object data)
 {
     Debug.Assert((usingStatement != null));
     Debug.Assert((usingStatement.ResourceAcquisition != null));
     Debug.Assert((usingStatement.EmbeddedStatement != null));
     usingStatement.ResourceAcquisition.AcceptVisitor(this, data);
     return usingStatement.EmbeddedStatement.AcceptVisitor(this, data);
 }
 public virtual object VisitUsingStatement(UsingStatement usingStatement, object data)
 {
     throw new global::System.NotImplementedException("UsingStatement");
 }
示例#7
0
 void UsingStatement(out Statement statement)
 {
     Expression expr = null; Statement block; statement = null;
     Expect(226);
     if (Peek(1).kind == Tokens.As) {
     LocalVariableDeclaration resourceAquisition =
     new LocalVariableDeclaration(Modifiers.None);
     VariableDeclarator(resourceAquisition.Variables);
     while (la.kind == 22) {
         Get();
         VariableDeclarator(resourceAquisition.Variables);
     }
     EndOfStmt();
     Block(out block);
     statement = new UsingStatement(resourceAquisition, block);
     } else if (StartOf(24)) {
     Expr(out expr);
     EndOfStmt();
     Block(out block);
     statement = new UsingStatement(new ExpressionStatement(expr), block);
     } else SynErr(313);
     Expect(113);
     Expect(226);
 }
 public virtual object VisitUsingStatement(UsingStatement usingStatement, object data)
 {
     Debug.Assert((usingStatement != null));
     Debug.Assert((usingStatement.ResourceAcquisition != null));
     Debug.Assert((usingStatement.EmbeddedStatement != null));
     nodeStack.Push(usingStatement.ResourceAcquisition);
     usingStatement.ResourceAcquisition.AcceptVisitor(this, data);
     usingStatement.ResourceAcquisition = ((Statement)(nodeStack.Pop()));
     nodeStack.Push(usingStatement.EmbeddedStatement);
     usingStatement.EmbeddedStatement.AcceptVisitor(this, data);
     usingStatement.EmbeddedStatement = ((Statement)(nodeStack.Pop()));
     return null;
 }