Skip to content

Commit a6b72dd

Browse files
committed
Remove ToFullString() to setup [ObservableRecipient] constructors
1 parent 4b825ad commit a6b72dd

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,18 @@ protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers
117117
// If the target type has no constructors, generate constructors as well
118118
if (!info.HasExplicitConstructors)
119119
{
120-
foreach (ConstructorDeclarationSyntax ctor in memberDeclarations.OfType<ConstructorDeclarationSyntax>())
120+
foreach (ConstructorDeclarationSyntax originalConstructor in memberDeclarations.OfType<ConstructorDeclarationSyntax>())
121121
{
122-
string text = ctor.NormalizeWhitespace().ToFullString();
123-
string replaced = text.Replace("ObservableRecipient", info.TypeName);
122+
ConstructorDeclarationSyntax modifiedConstructor = originalConstructor.WithIdentifier(Identifier(info.TypeName));
124123

125124
// Adjust the visibility of the constructors based on whether the target type is abstract.
126125
// If that is not the case, the constructors have to be declared as public and not protected.
127126
if (!info.IsAbstract)
128127
{
129-
replaced = replaced.Replace("protected", "public");
128+
modifiedConstructor = modifiedConstructor.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)));
130129
}
131130

132-
builder.Add((ConstructorDeclarationSyntax)ParseMemberDeclaration(replaced)!);
131+
builder.Add(modifiedConstructor);
133132
}
134133
}
135134

0 commit comments

Comments
 (0)