diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index 2c3e9f93f..161d8d38a 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -100,6 +100,7 @@ + @@ -142,6 +143,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs index 57fa18c7c..2fc5bbc87 100644 --- a/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs @@ -231,6 +231,12 @@ namespace ICSharpCode.Decompiler.Tests await Run(); } + [Test] + public async Task Issue3524() + { + await Run(); + } + [Test] public async Task Issue2260SwitchString() { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.cs b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.cs new file mode 100644 index 000000000..3ba73156c --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.cs @@ -0,0 +1,14 @@ +public class C +{ + public static int X { + get { + return 32; + } + set { + } + } + static C() + { + X = 1; + } +} \ No newline at end of file diff --git a/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.il b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.il new file mode 100644 index 000000000..1d3d04f65 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue3524.il @@ -0,0 +1,77 @@ +.assembly _ +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( + 01 00 08 00 00 00 00 00 + ) + .custom instance void [System.Runtime]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( + 01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78 + 63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01 + ) + .custom instance void [System.Runtime]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( + 01 00 07 01 00 00 00 00 + ) + .hash algorithm 0x00008004 // SHA1 + .ver 0:0:0:0 +} + +.class public auto ansi beforefieldinit C + extends [System.Runtime]System.Object +{ + // Methods + .method public hidebysig specialname static + int32 get_X () cil managed + { + // Method begins at RVA 0x2050 + // Code size 6 (0x6) + .maxstack 8 + + IL_0000: ldc.i4 32 + IL_0005: ret + } // end of method C::get_X + + .method public hidebysig specialname static + void set_X ( + int32 'value' + ) cil managed + { + // Method begins at RVA 0x2058 + // Code size 1 (0x1) + .maxstack 8 + + IL_0000: ret + } // end of method C::set_X + + .method public hidebysig specialname rtspecialname + instance void .ctor () cil managed + { + // Method begins at RVA 0x2068 + // Code size 8 (0x8) + .maxstack 8 + + IL_0000: ldarg.0 + IL_0001: call instance void [System.Runtime]System.Object::.ctor() + IL_0006: nop + IL_0007: ret + } // end of method C::.ctor + + .method private hidebysig specialname rtspecialname static + void .cctor () cil managed + { + // Method begins at RVA 0x2074 + // Code size 11 (0xb) + .maxstack 8 + + IL_0000: ldc.i4 1 + IL_0005: call void C::set_X(int32) + IL_000a: ret + } // end of method C::.cctor + + // Properties + .property int32 X() + { + .get int32 C::get_X() + .set void C::set_X(int32) + } + +} // end of class C + diff --git a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/PropertyDeclaration.cs b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/PropertyDeclaration.cs index f7fd87434..b45e0df09 100644 --- a/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/PropertyDeclaration.cs +++ b/ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/PropertyDeclaration.cs @@ -82,6 +82,22 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax set { SetChildByRole(ExpressionBodyRole, value); } } + public bool IsAutomaticProperty { + get { + if (!Getter.IsNull && !Getter.Body.IsNull) + { + return false; + } + + if (!Setter.IsNull && !Setter.Body.IsNull) + { + return false; + } + + return true; + } + } + public override void AcceptVisitor(IAstVisitor visitor) { visitor.VisitPropertyDeclaration(this); diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs index d1254b244..090b182e1 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs @@ -382,7 +382,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms } } } - else if (fieldOrPropertyDecl is PropertyDeclaration pd) + else if (fieldOrPropertyDecl is PropertyDeclaration { IsAutomaticProperty: true } pd) { pd.Initializer = assignment.Right.Detach(); }