protected virtual IEnumerable <Syntax> TranslateTryCatchStatement(TryCatchStatement tryCatchStatement, ILTranslationContext data) { if (tryCatchStatement.CatchClauses.Count > 1) { throw new ArgumentException("Too many Catch clauses"); } var s = new S.TryStatement(); s.TryBlock = GetStatements(tryCatchStatement.TryBlock, data); if (tryCatchStatement.CatchClauses.Count == 1) { var cc = tryCatchStatement.CatchClauses.First(); s.CatchParameter = new E.IdentifierExpression { Name = string.IsNullOrEmpty(cc.VariableName) ? "__ex" : cc.VariableName }; s.CatchBlock = GetStatements(cc.Body, data); } s.FinallyBlock = GetStatements(tryCatchStatement.FinallyBlock, data); yield return(s); }
protected virtual IEnumerable <Syntax> TranslateUsingStatement(UsingStatement usingStatement, ILTranslationContext data) { var b = new S.BlockStatement(); var rvd = usingStatement.ResourceAcquisition as VariableDeclarationStatement; var vds = new S.VariableDeclaration(); vds.Type = S.VariableDeclarationType.Let; if (rvd != null) { foreach (var v in rvd.Variables) { vds.Bindings.Add(new S.VariableBinding() { Variable = new E.IdentifierExpression() { Name = v.Name }, Type = ResolveType(v, rvd.Type), Initializer = GetExpression(v.Initializer, data) }); } } else { var ex = (ICSharpCode.NRefactory.CSharp.Expression)usingStatement.ResourceAcquisition; vds.Bindings.Add(new S.VariableBinding() { Variable = new E.IdentifierExpression() { Name = "__usingResource" }, Initializer = GetExpression(ex, data) }); } b.Statements.Add(vds); var tf = new S.TryStatement(); tf.TryBlock = GetStatements(usingStatement.EmbeddedStatement, data); foreach (var bd in vds.Bindings) { var ib = new S.IfStatement(); ib.Condition = bd.Variable; ib.TruePart.Add(new S.ExpressionStatement() { Expression = new E.CallExpression() { Target = new E.PropertyExpression() { Object = bd.Variable, Property = "Dispose" } } }); tf.FinallyBlock.Add(ib); } b.Statements.Add(tf); yield return(b); }