Skip to content

Remove explicit backing field generated Relay Commands when possible#1162

Open
Avid29 wants to merge 12 commits intoCommunityToolkit:mainfrom
Avid29:relay-command-sg_field-keywords
Open

Remove explicit backing field generated Relay Commands when possible#1162
Avid29 wants to merge 12 commits intoCommunityToolkit:mainfrom
Avid29:relay-command-sg_field-keywords

Conversation

@Avid29
Copy link
Copy Markdown
Member

@Avid29 Avid29 commented Feb 7, 2026

Closes #1073

When the C# language version supports it, this change replaces the backing field for generated RelayCommands with an implicit field using the new field keyword.

PR Checklist

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • PR doesn't include merge commits (always rebase on top of our main, if needed)
  • Tested code with current supported SDKs
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes
  • Every new API (including internal ones) has full XML docs
  • Code follows all style conventions

Other information

Technically, this is a breaking change since someone may have been using the backing fields for whatever reason

@Avid29
Copy link
Copy Markdown
Member Author

Avid29 commented Feb 7, 2026

Sorry. This PR is horribly named, but GitHub isn't letting me edit for some reason

@Avid29 Avid29 marked this pull request as draft February 7, 2026 04:12
@Avid29
Copy link
Copy Markdown
Member Author

Avid29 commented Feb 7, 2026

Some tests are failing. It appears to be because they test that both the field and property is generated, except now a field is no longer always generated.

For example: Test_ObservableProperty_CommandNamesThatCantBeLowered and Test_RelayCommandAttribute_WithPartialCommandMethodDefinitions both fit this group of tests

…RelayCommand tests

NOTE: This conditional compilation is based on roslyn version, not C# version or target platform. This could hypothetically lead to reduced test coverage in the future.
@Avid29 Avid29 marked this pull request as ready for review February 7, 2026 15:17
@Avid29 Avid29 changed the title Relay command sg field keywords Remove explicit backing field generated Relay Commands when possible Feb 18, 2026
Avid29 added 4 commits March 18, 2026 17:14
…RelayCommand tests

NOTE: This conditional compilation is based on roslyn version, not C# version or target platform. This could hypothetically lead to reduced test coverage in the future.
@Avid29 Avid29 force-pushed the relay-command-sg_field-keywords branch from 314633d to 4a2c060 Compare March 18, 2026 15:14
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <summary>The backing field for <see cref=\"{commandInfo.PropertyName}\"/>.</summary>")), SyntaxKind.OpenBracketToken, TriviaList())))
.AddAttributeLists(forwardedFieldAttributes);
ImmutableArrayBuilder<MemberDeclarationSyntax> declarations = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on other usage of ImmutableArrayBuilder.Rent, it's meant to be disposed.
https://github.com/search?q=repo%3ACommunityToolkit%2Fdotnet%20ImmutableArrayBuilder&type=code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I thought that the ToImmutable call at the end of the function disposed the builder, but upon further investigation, I was wrong. I'll add a using declaration, so dispose is called at the end of the scope.

@huguesv
Copy link
Copy Markdown

huguesv commented Apr 11, 2026

I'd love for this to go in. I had looked into what it meant to implement this a few months ago and it was pretty much what you ended up doing. Thanks for actually doing it!

I was too wondering what to do about the breaking change to anyone using the field directly. It seems only avoidable if we require folks to opt-in (via an extra parameter to the attribute?) but that would make the optimal scenario more verbose and worse in my opinion.

@Avid29
Copy link
Copy Markdown
Member Author

Avid29 commented Apr 12, 2026

@huguesv I've added an optional named parameter to the RelayCommandAttribute named ForceBackingField, which can be used to force a backing field to be used even when the field keyword is available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use field keyword on generated relay commands

2 participants