runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Consider overload of Get<T> that accepts child section name

Open brockallen opened this issue 9 years ago • 6 comments

It'd be nice to have a Get<T>(string section) that looks like this:

namespace Microsoft.Extensions.Configuration
{
    public static class ConfigurationExtensions
    {
        public static T Get<T>(this IConfiguration section, string key)
        {
            return section.GetSection(key).Get<T>();
        }
    }
}

brockallen avatar Mar 04 '17 16:03 brockallen

This would be very useful. Alternatively, it would be good to have support for arbitrary nesting like so: Configuration["level1"]["level2"] or Configuration["level1.level2"] or Configuration["level1//level2"]

AmadeusW avatar May 16 '17 05:05 AmadeusW

@brockallen I believe this method is now available thanks to @HaoK.

public static T GetValue<T>(this IConfiguration configuration, string key)
{
    return GetValue(configuration, key, default(T));
}

user1336 avatar Jun 30 '17 14:06 user1336

Does not work, the GetValue is just for string and primites, use following 'hack'

 var settings = new MySectionConf();
 Config.GetSection(nameof(MySectionConf)).Bind(settings); or Config.GetSection("customKey").Bind(settings);
 return settings;

smariussorin avatar Sep 03 '18 15:09 smariussorin

Tagging subscribers to this area: @eiriktsarpalis Notify danmosemsft if you want to be subscribed.

ghost avatar May 15 '20 19:05 ghost

Due to lack of recent activity

I think there's no activity since this was opened... ~8 years ago. In any event, this still feels like it would be useful, and I've not seen a reason why it's not feasible or a good idea.

brockallen avatar Apr 21 '25 13:04 brockallen

Triage: sounds an interesting convenience method, we may consider it in future.

rosebyte avatar Mar 02 '26 08:03 rosebyte