Skip to content

Add TestAccessor support for auto-property backing fields - #14901

Open
JayashreeSF3546 wants to merge 4 commits into
dotnet:mainfrom
JayashreeSF3546:Fix_Issue_8476
Open

Add TestAccessor support for auto-property backing fields#14901
JayashreeSF3546 wants to merge 4 commits into
dotnet:mainfrom
JayashreeSF3546:Fix_Issue_8476

Conversation

@JayashreeSF3546

@JayashreeSF3546 JayashreeSF3546 commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #8476

Proposed changes

  • Added support in TestAccessor to resolve compiler-generated backing fields for auto-implemented properties.
  • Added a new helper method GetAutoPropertyBackingField() that searches for fields named k__BackingField.
  • Updated member lookup logic to attempt auto-property backing field resolution when a field is not found.
  • Added unit tests for reading, writing, read-only, and inherited auto-properties.

Customer Impact

  • Developers can use auto-properties in test scenarios without creating separate private backing fields solely for TestAccessor support.
  • Reduces unnecessary boilerplate code and simplifies testable implementation patterns.

Regression?

  • No

Risk

  • Low.
  • Change is isolated to TestAccessor test infrastructure.
  • Existing field and property lookup behavior remains unchanged unless a matching auto-property backing field exists.
  • Covered by new unit tests for common scenarios.

Screenshots

NA

Test methodology

  • Added test to verify reading an auto-property backing field.
  • Added test to verify writing an auto-property backing field.
  • Added test for read-only auto-properties.
  • Added test for inherited auto-properties.

Test environment(s)

  • Windows 11
  • .NET SDK: 11.0.100-preview.3.26170.106
Microsoft Reviewers: Open in CodeFlow

…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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 TestAccessor dynamic 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;
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.

[Tracking] Update Test accessor to provide access to compiler generated fields on auto properties.

2 participants