From 5672c58bcf214ecc6dac3729e7a953b2edf10a0a Mon Sep 17 00:00:00 2001 From: sonyps5201314 Date: Fri, 31 Oct 2025 02:38:30 +0800 Subject: [PATCH] Fixed an issue where the `TestRef` type in the example below could not be decompiled correctly after the commit: `Fixed an issue where the logic of moving the initialization statement of class members in the constructor to the class member declaration was incompatible with complex expressions using parameters.` ```cs public ref struct TestRef(ref int a) { public ref int AAA = ref a; public void Print() { int val = AAA; Console.WriteLine(val); } } ``` --- ...TransformFieldAndConstructorInitializers.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs index f4c93297a..918a59585 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs @@ -231,27 +231,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms if (initializer.DescendantsAndSelf.Any(n => n is ThisReferenceExpression || n is BaseReferenceExpression)) break; - bool assignFromCurrentCtorParameters = false; - var v = initializer.Annotation()?.Variable; - if (v == null) - { - v = (initializer.Annotation()?.ElementResult as ILVariableResolveResult)?.Variable; - } - if (v != null) - { - assignFromCurrentCtorParameters = v.Kind == IL.VariableKind.Parameter; - } - else + bool assignFromCurrentCtorParameters = v?.Kind == IL.VariableKind.Parameter; + if (!assignFromCurrentCtorParameters) { // If we didn't get an ILVariableResolveResult for the whole initializer expression, // walk its descendants and try to find any reference that corresponds to a // constructor parameter. We consider both ILVariable annotations (including // by-reference wrappers) and high-level parameter symbols. - foreach (var node in initializer.DescendantsAndSelf) + foreach (var node in initializer.Descendants) { - var localVar = node.Annotation()?.Variable - ?? (node.Annotation()?.ElementResult as ILVariableResolveResult)?.Variable; + var localVar = node.Annotation()?.Variable; if (localVar != null) { if (localVar.Kind == IL.VariableKind.Parameter)