Add TestAccessor support for auto-property backing fields - #14901
Open
JayashreeSF3546 wants to merge 4 commits into
Open
Add TestAccessor support for auto-property backing fields#14901JayashreeSF3546 wants to merge 4 commits into
JayashreeSF3546 wants to merge 4 commits into
Conversation
…ed fields on auto properties.
…c-member restriction The original implementation fell back to the compiler-generated backing field whenever GetProperty(NonPublic) returned null. Since GetProperty with only the NonPublic flag only fails to match fully-public properties, this fallback only ever engaged for public auto-properties - directly violating TestAccessor's documented invariant that it must not expose public members (verified by TestAccessor_DynamicAccess_PublicProperty, which regressed to a failing state). Move the backing-field fallback into TrySetMember, triggered only when a non-public property is found via reflection but has no setter (e.g. get-only auto-properties). This preserves the public-member restriction while still enabling the real motivating scenario: writing to a private/ internal read-only auto-property for test setup. Updated the accompanying tests to use non-public auto-properties so they correctly exercise the new fallback path instead of relying on public members, which the accessor is designed to reject.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds functionality to the test-only TestAccessor utility so unit tests can set values for non-public get-only auto-properties by locating and writing the compiler-generated backing field, reducing the need for custom private fields in test scaffolding.
Changes:
- Updates
TestAccessordynamic setter logic to fall back to an auto-property backing field when a property has no setter. - Adds
GetAutoPropertyBackingField()helper to locate compiler-generated<PropertyName>k__BackingField. - Adds unit tests covering read/write auto-properties, get-only auto-properties, and inherited get-only auto-properties.
File summaries
| File | Description |
|---|---|
| src/test/unit/System.Windows.Forms/TestAccessorTests.cs | Adds coverage for dynamic access of non-public auto-properties, including get-only and inherited scenarios. |
| src/Common/tests/TestUtilities/TestAccessor.cs | Implements backing-field fallback for get-only auto-properties via a new helper method. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+170
to
+176
| case PropertyInfo propertyInfo: | ||
| // Non-public auto property without a setter (e.g. get-only). Fall back to the | ||
| // compiler-generated backing field so tests can still set up state directly. | ||
| FieldInfo backingField = GetAutoPropertyBackingField(propertyInfo.DeclaringType!, propertyInfo.Name) | ||
| ?? throw new InvalidOperationException($"'{propertyInfo.Name}' has no setter and no backing field could be found."); | ||
| backingField.SetValue(_instance, value); | ||
| break; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8476
Proposed changes
Customer Impact
Regression?
Risk
Screenshots
NA
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow