Skip to content

Allow configuration binding to set-only properties #63508

Closed as not planned
Closed as not planned

Description

@ogxd

Currently, according to the documentation, with the binding option BindNonPublicProperties the binder will attempt to set all non-read-only properties.
However, this is not true for set-only properties (it's not a read-only property).

I took a look at the code to see why this does not work, and it seems this is intentional:

// We don't support set only, non public, or indexer properties
if (property.GetMethod == null ||
    (!options.BindNonPublicProperties && !property.GetMethod.IsPublic) ||
    property.GetMethod.GetParameters().Length > 0)
{
    return;
}

I am not sure why this is so because by removing the null check it still works, tests are passing and it is now closer to the behavior documentation describes.

Removing this allows nice things such as leveraging set-only properties for custom types in configuration, built from primitive types supported by the binder.
Here is one example with TimeSpan, a type very commonly used in configurations:

public class MyConfiguration
{
    public double TimeoutSeconds { set => Interval = TimeSpan.FromSeconds(value); }
    public TimeSpan Timeout { get; private set; }
}

With this example, we can use "TimeoutSeconds" for configuration keys with integer values while having a TimeSpan in the code and a setter for the tests.

Activity

  1. ghost added
    untriaged
    on Jan 7, 2022
  2. tarekgh commented on Jan 7, 2022

    @tarekgh
    Member

    I am seeing @natemcmaster is the one added this code in the commit. He may remember why we did that?

  3. removed
    untriaged
    on Jan 11, 2022
  4. added this to the Future milestone on Jan 11, 2022
  5. added
    in-pr
    on Mar 19, 2026
  6. cincuranet commented on Mar 24, 2026

    @cincuranet
    Contributor

    After I started with the implementation, I wasn't sure it is worth it - a lot of edge cases, not that common use case, etc. And well-articulated points made by @tarekgh in #125768 (comment) really cemented my decision.

  7. modified the milestones: Future, 11.0.0 on Mar 24, 2026
  8. locked and limited conversation to collaborators on Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Extensions-Configurationin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions