Fix generated members accessibility for sealed types

This commit is contained in:
Sergio Pedri
2021-12-31 18:06:42 +01:00
parent 704e5073f1
commit 63abbb051d
2 changed files with 39 additions and 0 deletions
@@ -86,6 +86,13 @@ partial class TransitiveMembersGenerator<TInfo>
// If the target class is sealed, make protected members private and remove the virtual modifier
sealedMemberDeclarations = annotatedMemberDeclarations.Select(static member =>
{
// Constructors become public for sealed types
if (member is ConstructorDeclarationSyntax)
{
return member.ReplaceModifier(SyntaxKind.ProtectedKeyword, SyntaxKind.PublicKeyword);
}
// Other members become private
return
member
.ReplaceModifier(SyntaxKind.ProtectedKeyword, SyntaxKind.PrivateKeyword)
@@ -228,4 +228,36 @@ public partial class Test_ObservableRecipientAttribute
OnDeactivatedResult = true;
}
}
[TestMethod]
public void Test_ObservableRecipientAttribute_SealedPersonWithCustomOnActivatedAndOnDeactivated()
{
SealedPersonWithCustomOnActivatedAndOnDeactivated model = new();
model.IsActive = true;
Assert.IsTrue(model.OnActivatedResult);
model.IsActive = false;
Assert.IsTrue(model.OnDeactivatedResult);
}
[ObservableRecipient]
public sealed partial class SealedPersonWithCustomOnActivatedAndOnDeactivated : ObservableObject
{
public bool OnActivatedResult { get; private set; }
public bool OnDeactivatedResult { get; private set; }
private partial void OnActivated()
{
OnActivatedResult = true;
}
private partial void OnDeactivated()
{
OnDeactivatedResult = true;
}
}
}