From baa05b2e01251788c513590112f5c8f1c79e5e0b Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 22 Apr 2011 19:38:17 +0200 Subject: [PATCH] Fixed unintended interaction between object initializers and anonymous methods. --- .../Ast/Transforms/DelegateConstruction.cs | 4 +++- .../ILAst/InitializerPeepholeTransforms.cs | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs b/ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs index 59c90c28c..871fc2546 100644 --- a/ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs +++ b/ICSharpCode.Decompiler/Ast/Transforms/DelegateConstruction.cs @@ -111,6 +111,8 @@ namespace ICSharpCode.Decompiler.Ast.Transforms { if (!context.Settings.AnonymousMethods) return false; // anonymous method decompilation is disabled + if (target != null && !(target is IdentifierExpression || target is ThisReferenceExpression || target is NullReferenceExpression)) + return false; // don't copy arbitrary expressions, deal with identifiers only // Anonymous methods are defined in the same assembly MethodDefinition method = methodRef.ResolveWithinSameModule(); @@ -175,7 +177,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms return true; } - static bool IsPotentialClosure(DecompilerContext context, TypeDefinition potentialDisplayClass) + internal static bool IsPotentialClosure(DecompilerContext context, TypeDefinition potentialDisplayClass) { if (potentialDisplayClass == null || !potentialDisplayClass.IsCompilerGeneratedOrIsInCompilerGeneratedClass()) return false; diff --git a/ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs b/ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs index 35853b830..f067e47f3 100644 --- a/ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs +++ b/ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs @@ -164,6 +164,11 @@ namespace ICSharpCode.Decompiler.ILAst return false; } else { // Object Initializer + + // don't use object initializer syntax for closures + if (Ast.Transforms.DelegateConstruction.IsPotentialClosure(context, ctor.DeclaringType.ResolveWithinSameModule())) + return false; + initializer = new ILExpression(ILCode.InitObject, null, newObjExpr); pos++; while (pos < body.Count) {