艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

.NET Core 通过索引器实现读取.json配置文件

   public class AppSettings
{
IConfiguration config;
public AppSettings():this("appsettings.json") { }
public AppSettings(string jsonFileName)
{
var configBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(jsonFileName);
config = configBuilder.Build();
}
public string this[string name]
{
get { return config.GetSection(name).Value;}
}
}

用法

AppSettings get = new AppSettings();
string v = get["key"];

相关的文章