Skip to content

Commit 63abbb0

Browse files
committed
Fix generated members accessibility for sealed types
1 parent 704e507 commit 63abbb0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.Execute.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public static void ProcessMemberDeclarations(
8686
// If the target class is sealed, make protected members private and remove the virtual modifier
8787
sealedMemberDeclarations = annotatedMemberDeclarations.Select(static member =>
8888
{
89+
// Constructors become public for sealed types
90+
if (member is ConstructorDeclarationSyntax)
91+
{
92+
return member.ReplaceModifier(SyntaxKind.ProtectedKeyword, SyntaxKind.PublicKeyword);
93+
}
94+
95+
// Other members become private
8996
return
9097
member
9198
.ReplaceModifier(SyntaxKind.ProtectedKeyword, SyntaxKind.PrivateKeyword)

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservableRecipientAttribute.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,36 @@ protected virtual partial void OnDeactivated()
228228
OnDeactivatedResult = true;
229229
}
230230
}
231+
232+
[TestMethod]
233+
public void Test_ObservableRecipientAttribute_SealedPersonWithCustomOnActivatedAndOnDeactivated()
234+
{
235+
SealedPersonWithCustomOnActivatedAndOnDeactivated model = new();
236+
237+
model.IsActive = true;
238+
239+
Assert.IsTrue(model.OnActivatedResult);
240+
241+
model.IsActive = false;
242+
243+
Assert.IsTrue(model.OnDeactivatedResult);
244+
}
245+
246+
[ObservableRecipient]
247+
public sealed partial class SealedPersonWithCustomOnActivatedAndOnDeactivated : ObservableObject
248+
{
249+
public bool OnActivatedResult { get; private set; }
250+
251+
public bool OnDeactivatedResult { get; private set; }
252+
253+
private partial void OnActivated()
254+
{
255+
OnActivatedResult = true;
256+
}
257+
258+
private partial void OnDeactivated()
259+
{
260+
OnDeactivatedResult = true;
261+
}
262+
}
231263
}

0 commit comments

Comments
 (0)