Merge pull request #4185 from icsharpcode/investigate/catel-required-record

Handle required members on record copy constructors
This commit is contained in:
Siegfried Pammer
2026-09-27 14:48:15 +02:00
committed by GitHub
2 changed files with 17 additions and 6 deletions
@@ -88,6 +88,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
#if CS110
public record WithRequiredMembers
{
public required Type ServiceType { get; init; }
public required object ServiceKey { get; init; }
}
#endif
[AttributeUsage(AttributeTargets.All)]
public class RecordTestAttribute : Attribute
{
@@ -707,12 +707,14 @@ namespace ICSharpCode.Decompiler.CSharp
&& IsRecordType(method.Parameters[0].Type);
}
private bool IsAllowedAttribute(IAttribute attribute)
private bool IsAllowedAttribute(IEntity owner, IAttribute attribute)
{
switch (attribute.AttributeType.ReflectionName)
{
case "System.Runtime.CompilerServices.CompilerGeneratedAttribute":
return true;
case "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute":
return owner is IMethod { IsConstructor: true };
default:
return false;
}
@@ -726,7 +728,7 @@ namespace ICSharpCode.Decompiler.CSharp
leave IL_0000 (nop)
*/
Debug.Assert(method.IsConstructor && method.Parameters.Count == 1);
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(attr)) || method.GetReturnTypeAttributes().Any())
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(method, attr)) || method.GetReturnTypeAttributes().Any())
return false;
if (method.Accessibility != Accessibility.Protected && (!isSealed || method.Accessibility != Accessibility.Private))
return false;
@@ -839,7 +841,7 @@ namespace ICSharpCode.Decompiler.CSharp
return false;
if (!isSealed && !method.IsOverridable)
return false;
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(attr)) || method.GetReturnTypeAttributes().Any())
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(method, attr)) || method.GetReturnTypeAttributes().Any())
return false;
if (method.Accessibility != Accessibility.Protected && (!isSealed || method.Accessibility != Accessibility.Private))
return false;
@@ -1019,7 +1021,7 @@ namespace ICSharpCode.Decompiler.CSharp
return false;
if (method.IsSealed)
return false;
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(attr)) || method.GetReturnTypeAttributes().Any())
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(method, attr)) || method.GetReturnTypeAttributes().Any())
return false;
var body = DecompileBody(method);
if (body == null)
@@ -1093,7 +1095,7 @@ namespace ICSharpCode.Decompiler.CSharp
return false;
if (!isSealed && !method.IsOverridable)
return false;
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(attr)) || method.GetReturnTypeAttributes().Any())
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(method, attr)) || method.GetReturnTypeAttributes().Any())
return false;
if (orderedMembers == null)
return false;
@@ -1283,7 +1285,7 @@ namespace ICSharpCode.Decompiler.CSharp
return false;
if (!method.IsOverride || method.IsSealed)
return false;
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(attr)) || method.GetReturnTypeAttributes().Any())
if (method.GetAttributes().Any(attr => !IsAllowedAttribute(method, attr)) || method.GetReturnTypeAttributes().Any())
return false;
if (orderedMembers == null)
return false;