CongurationBinder and TypeConverter #100961
Unanswered
Kinematics
asked this question in
Q&A
Replies: 1 comment
-
You can add |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In the
Microsoft.Extensions.Configuration.Binder
package, ConfigurationBinder.cs, the code does not appear to respectTypeConverter
attributes on bound properties.Basically, it does discovery on properties that the configuration is trying to bind to, and passes the property type to the
TryConvertValue()
function (link), but not the property itself. TheTryConvertValue()
function then requests a converter for that type, by which point it has lost the context of the property being assigned, which means it won't pick up anyTypeConverter
attribute.The specific case I'm working on is trying to migrate an old enum value to a
bool?
. I have a TypeConverter to do the conversion, and applied it to the property being loaded in, but it generates an exception because it's trying to use the defaultBooleanConverter
on the numeric value instead.This could just be me not understanding where in the pipeline the conversion process is supposed to happen. Trying to do the conversion explicitly works OK (whether when loading JSON, or doing type conversion), but doesn't work when running through the Configuration extension because it co-opts all that stuff. The JSON side is understandable since it's loading from a variety of source types, but I'd think that at least the type conversion side would be respected.
Is there a proper way to do this, or should this be filed as a bug/feature request?
Beta Was this translation helpful? Give feedback.
All reactions