Skip to content

Commit dabfc75

Browse files
committed
Update TransformCollectionAndObjectInitializers to check for init-only properties
1 parent 2f311c2 commit dabfc75

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con
5858
{
5959
case NewObj newObjInst:
6060
if (newObjInst.ILStackWasEmpty && v.Kind == VariableKind.Local
61+
&& !TypeContainsInitOnlyProperties(newObjInst.Method.DeclaringType.GetDefinition())
6162
&& !currentMethod.IsConstructor
6263
&& !currentMethod.IsCompilerGeneratedOrIsInCompilerGeneratedClass())
6364
{
@@ -187,6 +188,18 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con
187188
ILInlining.InlineIfPossible(block, pos, context);
188189
}
189190

191+
private static bool TypeContainsInitOnlyProperties(ITypeDefinition? typeDefinition)
192+
{
193+
foreach (var property in typeDefinition?.Properties ?? [])
194+
{
195+
if (property.Setter?.IsInitOnly ?? false)
196+
{
197+
return true;
198+
}
199+
}
200+
return false;
201+
}
202+
190203
internal static bool IsRecordCloneMethodCall(CallInstruction ci)
191204
{
192205
if (ci.Method.DeclaringTypeDefinition?.IsRecord != true)

0 commit comments

Comments
 (0)