From 5078d2ef89ad965cdeb46c5781adbba8dfda42fe Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 7 Mar 2011 07:49:34 +0100 Subject: [PATCH] Handle calls to value type constructors. Closes #66. --- .../Ast/AstMethodBodyBuilder.cs | 10 ++++++++++ ICSharpCode.Decompiler/DecompilerSettings.cs | 2 -- ICSharpCode.Decompiler/Tests/ValueTypes.cs | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs index ee184461a..a85b773f9 100644 --- a/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs @@ -535,6 +535,16 @@ namespace ICSharpCode.Decompiler.Ast } } + if (cecilMethod.Name == ".ctor" && cecilMethod.DeclaringType.IsValueType) { + // On value types, the constructor can be called. + // This is equivalent to 'target = new ValueType(args);'. + ObjectCreateExpression oce = new ObjectCreateExpression(); + oce.Type = AstBuilder.ConvertType(cecilMethod.DeclaringType); + AdjustArgumentsForMethodCall(cecilMethod, methodArgs); + oce.Arguments.AddRange(methodArgs); + return new AssignmentExpression(target, oce); + } + if (cecilMethod.Name == "Get" && cecilMethod.DeclaringType is ArrayType && methodArgs.Count > 1) { return target.Indexer(methodArgs); } else if (cecilMethod.Name == "Set" && cecilMethod.DeclaringType is ArrayType && methodArgs.Count > 2) { diff --git a/ICSharpCode.Decompiler/DecompilerSettings.cs b/ICSharpCode.Decompiler/DecompilerSettings.cs index 53736311a..a1770d771 100644 --- a/ICSharpCode.Decompiler/DecompilerSettings.cs +++ b/ICSharpCode.Decompiler/DecompilerSettings.cs @@ -41,8 +41,6 @@ namespace ICSharpCode.Decompiler } } - public event EventHandler YieldReturnChanged; - public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) diff --git a/ICSharpCode.Decompiler/Tests/ValueTypes.cs b/ICSharpCode.Decompiler/Tests/ValueTypes.cs index ba863f302..e2e2d20fc 100644 --- a/ICSharpCode.Decompiler/Tests/ValueTypes.cs +++ b/ICSharpCode.Decompiler/Tests/ValueTypes.cs @@ -9,6 +9,11 @@ public static class ValueTypes { public int Field; + public S(int field) + { + this.Field = field; + } + public void SetField() { this.Field = 5; @@ -46,6 +51,17 @@ public static class ValueTypes p = default(S); } + public static S CallValueTypeCtor1() + { + return new S(10); + } + + public static S CallValueTypeCtor2() + { + S s = new S(10); + return s; + } + public static S Copy1(S p) { return p;