diff --git a/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs b/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs index 944c5daf98..7a3e47d40a 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs @@ -58,6 +58,7 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con { case NewObj newObjInst: if (newObjInst.ILStackWasEmpty && v.Kind == VariableKind.Local + && !TypeContainsInitOnlyProperties(newObjInst.Method.DeclaringTypeDefinition) && !currentMethod.IsConstructor && !currentMethod.IsCompilerGeneratedOrIsInCompilerGeneratedClass()) { @@ -187,6 +188,18 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con ILInlining.InlineIfPossible(block, pos, context); } + private static bool TypeContainsInitOnlyProperties(ITypeDefinition? typeDefinition) + { + foreach (var property in typeDefinition?.Properties ?? []) + { + if (property.Setter?.IsInitOnly ?? false) + { + return true; + } + } + return false; + } + internal static bool IsRecordCloneMethodCall(CallInstruction ci) { if (ci.Method.DeclaringTypeDefinition?.IsRecord != true)