Honor ConfigurationKeyName in constructor binding - #133793
Closed
bbartels wants to merge 4 commits into
Closed
Conversation
Co-authored-by: bbartels <23058572+bbartels@users.noreply.github.com>
…-binding Honor ConfigurationKeyName for constructor binding in ConfigurationBinder
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Avoid repeated property scans during constructor binding by reusing a case-insensitive property map.
Pull request overview
Updates reflection-based constructor binding to honor [ConfigurationKeyName] while preserving CLR-name matching and validation behavior.
Changes:
- Binds constructor parameters using matched properties’ effective configuration keys.
- Adds positive and negative primary-constructor tests.
- Keeps source-generator behavior unchanged.
File summaries
| File | Summary |
|---|---|
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs |
Adds annotated primary-constructor test models. |
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs |
Adds constructor-binding success and validation tests. |
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs |
Uses effective property configuration keys. Moderate issue: reuse a case-insensitive property map to avoid repeated quadratic scans. |
Review details
Suppressed comments (1)
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs:603
- This calls
FindMatchingPropertyonce per constructor parameter even thoughDoAllParametersHaveEquivalentPropertieshas already scanned the same property list once per parameter. Compared with the previousHashSetlookup, constructor binding is now O(parameters × properties) for every object (and this path runs for each collection element). Build a case-insensitive name-to-property map once and reuse it for validation and binding to avoid the avoidable quadratic regression.
[!NOTE]
This comment was generated by GitHub Copilot.
parameterValues[index] = BindParameter(parameters[index], FindMatchingProperty(parameters[index].Name!, properties)!, type, config, options);
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Lite
This was referenced Sep 12, 2026
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.
Constructor binding in
Microsoft.Extensions.Configuration.Bindervalidated and read constructor parameters by CLR property/parameter name, so[ConfigurationKeyName]on a corresponding property could still fail for primary constructors and records. This updates the runtime binder to use the matched property's effective configuration key for parameter value lookup without changing constructor/property matching semantics.Runtime binder
GetPropertyName(property)), not the raw parameter name.Validation / error behavior
Source generator scope
ConfigurationKeyNameinto constructor-parameter binding; this PR only closes the reflection-path parity gap.Tests
ConfigurationKeyNameAttribute.With this change,
secretKeyis populated fromexample-secret-keyduring constructor binding, while constructor/property equivalence checks continue to useSecretKey.