Skip to content

Treat init-only custom properties as read-only - #2555

Merged
Manodasan Wignarajah (manodasanW) merged 1 commit into
staging/3.0from
user/sergiopedri/init-only-binding-support
Sep 10, 2026
Merged

Treat init-only custom properties as read-only#2555
Manodasan Wignarajah (manodasanW) merged 1 commit into
staging/3.0from
user/sergiopedri/init-only-binding-support

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Summary

Keep public init-only properties and indexers readable through generated ICustomProperty implementations without reporting them as writable or emitting invalid assignments.

Motivation

The custom property provider generator currently classifies any public setter as writable, including init accessors. This produces CS8852 errors for immutable classes and readonly structs, including required init-only properties, and blocks otherwise valid XAML bindings. These members must remain discoverable and readable, with writes rejected just like getter-only members.

Changes

  • src\Authoring\WinRT.SourceGenerator2\CustomPropertyProviderGenerator.Execute.cs: require a public, non-init setter for CanWrite, reusing the existing read-only emission for both properties and indexers.
  • src\Tests\SourceGenerator2Test\Helpers\CSharpGeneratorTest{TGenerator}.cs: add an opt-in helper that checks compilation/emit diagnostics and loads the resulting assembly for behavior assertions, leaving existing snapshot validation unchanged.
  • src\Tests\SourceGenerator2Test\Test_CustomPropertyProviderGenerator.cs: add nine regression cases covering classes, readonly structs, required members, inherited properties, explicit selection, nonpublic setters, and init-only indexers. Assert that initialized values remain readable, init-only writes throw NotSupportedException, and ordinary property/indexer setters remain writable.

Preserve readable init-only bindings and add compilation and behavior regression coverage for properties and indexers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@manodasanW
Manodasan Wignarajah (manodasanW) merged commit c1d700a into staging/3.0 Sep 10, 2026
13 checks passed
@manodasanW
Manodasan Wignarajah (manodasanW) deleted the user/sergiopedri/init-only-binding-support branch September 10, 2026 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working CsWinRT 3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants