Add support for binder to create models #467
Description
Currently if you want to bind configuration to any sort of user-defined model, you have to create the model and then bind:
var model = new MyModel();
configuration.Bind(model);
You have to do that because GetValue<T>
only uses type conversion to convert the value in a specific key into a destination type. So something like this works...
var enabled = configuration.GetValue<bool>("enabled");
...but something like this doesn't work:
// Always returns null.
var model = configuration.GetValue<MyModel>("settings");
This becomes really painful if you want to bind an array of settings back into an array. If I want an enumerated list of settings, it has to be like:
var settings = new List<string>();
configuration.Bind(settings);
Other similar scenarios become multi-step pain, too. Instead of only using type conversion in the GetValue<T>
mechanism, it would be nice if that process would also make use of the (private) CreateInstance
method in the binder already and create a new version of the model/bind that new version if possible.