I am wondering if it is possible to bind a configuration section to an object dynamically. Normally to bind a configuration section we would write code like this:
var section = Configuration.GetSection(nameof(MyCustomSection));
services.Configure<MyCustomSection>(o => secto.Bind(o));
I am wondering if its possible to do it without declaring the type <MyCustomSection>.
//This doesn't work, just trying to show you how I would like to do it
services.Configure(MyType, o => section.Bind(o));
For example, if I wish to bind injection I can do it like this:
services.AddTransient<IDateTime, SystemDateTime>();
But I can also do it in a dynamic way such as this:
services.AddTransient(Type1, Type2));
Is the same possible for services.Configure? I looked at method parameters but it doesn't seem to support it. Just wondering if there is another way or maybe I'm just overlooking something?
EDIT:
services.AddSingleton(p =>
{
var type = new MySection();
Configuration.GetSection("MySection").Bind(type);
return type;
});
Then I call it in a class like this:
public class Test {
public Test(IOptions<MySection> section)
{
var finalValue = section.Value;
}
}
finalValue is always null;