Skip to content

Honor overridden virtual property metadata in configuration binder - #135672

Merged
rosebyte merged 4 commits into
dotnet:mainfrom
vladimir-aubrecht:fix/binder-override-metadata
Oct 30, 2026
Merged

rosebyte merged 4 commits into
dotnet:mainfrom
vladimir-aubrecht:fix/binder-override-metadata

Conversation

@vladimir-aubrecht

Copy link
Copy Markdown
Contributor

Summary

  • Select ConfigurationKeyName and ConfigurationIgnore metadata from the most-derived declaration of an overridden virtual property.
  • Deduplicate virtual property slots in the reflection binder while preserving fallback to inherited accessors that still dispatch virtually.
  • Mirror the same metadata and accessor selection behavior in the configuration binding source generator.
  • Add regression coverage for renamed and ignored virtual property overrides.

This correctness fix is split from #134424 so that override metadata handling can be reviewed independently from property-level TypeConverterAttribute support.

Testing

  • Microsoft.Extensions.Configuration.Binder.Tests: 364 passed
  • Microsoft.Extensions.Configuration.Binder.SourceGeneration.Tests: 436 passed, 28 skipped

Note

This pull request description was generated with assistance from GitHub Copilot.

Both the reflection binder and the source generator selected the
base-most declaration of an overridden virtual property, discarding
any [ConfigurationKeyName]/[ConfigurationIgnore] attribute applied
only to the override. Combined with the reflection binder also (in
some cases) separately processing the base declaration, a renamed
override of a getter-only collection property could be bound twice,
once under the original name and once under the renamed one.

Both binders now select metadata (name/ignore) from the most-derived
declaration in the override chain, falling back to a less-derived
declaration only to resolve an accessor the override doesn't
re-declare (e.g. a get-only override of a read-write virtual
property), since invoking that accessor still dispatches virtually to
the actual override at run time.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 13, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Resolve effective accessors and inherited metadata across partial property overrides, ignore unsupported indexers, and preserve PropertyInfo invocation paths to avoid argument-array allocations. Add reflection and source-generator regression coverage for the reviewed edge cases.
Resolve effective accessors and inherited property metadata once during property discovery, then reuse sparse cached data throughout binding. Use targeted non-indexer property lookups to avoid repeated hierarchy-wide property array allocations.

@rosebyte rosebyte left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@rosebyte
rosebyte requested review from mrek-msft and svick October 21, 2026 15:17
@marek-safar marek-safar removed the community-contribution Indicates that the PR has been added by a community member label Oct 21, 2026
@rosebyte
rosebyte merged commit 294b271 into dotnet:main Oct 30, 2026
89 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
svick pushed a commit that referenced this pull request Nov 17, 2026
…at cannot inherit attributes (#136899)

Fixes #136759

This is a targeted mitigation for `release/11.0`. Main already avoids
this lookup following
[#135672](#135672),
which also changes overridden-property handling. This PR preserves
11.0's binding behavior rather than backporting that broader change.

When an attribute is not declared directly, reflection retrieves
attribute-usage metadata and prepares a parent-property lookup before
discovering that the property cannot inherit. The binder therefore pays
this cost for ordinary nonvirtual properties and virtual properties that
introduce a new slot.

This change checks the getter and setter's virtual-slot flags first. If
neither accessor can inherit, it uses direct attribute lookup. Potential
overrides retain the existing inherited lookup. The shared helper covers
both property binding and constructor/property matching. No caching is
added.

An equivalent early rejection in reflection remains a possible future
improvement, but requires a runtime update. The binder-local mitigation
also benefits applications updating the package on an existing runtime.

# Regression

The additional lookup was introduced with `ConfigurationIgnoreAttribute`
in
[#126396](#126396).
This change retains that feature while removing unnecessary inheritance
work.

Measured on .NET 10.0.14, macOS arm64, with dependencies held constant.
Mean time per operation:

| Operation | Binder 10.0 | Affected 11.0 source | Patched |
|---|---:|---:|---:|
| `Get<T>`, 1 plain property | 0.237 us | 0.591 us | 0.261 us |
| `Get<T>`, 10 plain properties | 1.672 us | 5.375 us | 1.913 us |
| `Get<T>`, 50 plain properties | 9.403 us | 27.642 us | 10.299 us |
| Repeated `Bind`, 50 plain properties | 9.074 us | 28.874 us | 10.321
us |
| Fresh-scope snapshot, 50 plain properties | 9.423 us | 28.611 us |
10.752 us |

For 50-property plain `Get<T>`, allocation decreases from 27,611 bytes
to 17,553 bytes, compared with 17,552 bytes for Binder 10.0. The
displayed cases remain approximately 10–15% slower than Binder 10.0.

# Risk

Expected compatibility risk is low: inherited lookup is bypassed only
when neither accessor can inherit. Property discovery, constructor
selection, and options lifetimes are unchanged.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants