diff --git a/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs b/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs index a0bac08c8..bd4150537 100644 --- a/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs +++ b/ICSharpCode.Decompiler/Ast/DeclareVariableInSmallestScope.cs @@ -59,15 +59,19 @@ namespace Decompiler } node = node.NextSibling; } - if (withinPos != null && withinPos.Role == BlockStatement.StatementRole && (allowPassIntoLoops || !IsLoop(pos))) + if (withinPos != null && withinPos.Role == BlockStatement.StatementRole && AllowPassInto(pos, allowPassIntoLoops)) return withinPos; else return pos; } - static bool IsLoop(AstNode node) + static bool AllowPassInto(AstNode node, bool allowPassIntoLoops) { - return node is ForStatement || node is ForeachStatement || node is DoWhileStatement || node is WhileStatement; + if (node is AnonymousMethodExpression || node is LambdaExpression) + return false; + if (node is ForStatement || node is ForeachStatement || node is DoWhileStatement || node is WhileStatement) + return allowPassIntoLoops; + return true; } } } diff --git a/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs b/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs index 34657126e..3267ac149 100644 --- a/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs +++ b/ICSharpCode.Decompiler/Tests/DelegateConstruction.cs @@ -2,6 +2,7 @@ // This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; +using System.Collections.Generic; public static class DelegateConstruction { @@ -38,4 +39,24 @@ public static class DelegateConstruction { return new Func(((string)null).ToUpper); } + + public static List> AnonymousMethodStoreWithinLoop() + { + List> list = new List>(); + for (int i = 0; i < 10; i++) { + int counter; + list.Add(x => counter = x); + } + return list; + } + + public static List> AnonymousMethodStoreOutsideLoop() + { + List> list = new List>(); + int counter; + for (int i = 0; i < 10; i++) { + list.Add(x => counter = x); + } + return list; + } }