You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow configuration binding to set-only properties #63508
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 propertiesif(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:
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.
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.
Activity
tarekgh commentedon Jan 7, 2022
I am seeing @natemcmaster is the one added this code in the commit. He may remember why we did that?
cincuranet commentedon Mar 24, 2026
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.