using Microsoft.Extensions.Configuration;
namespace RoBot.Core.Helper
{
public class AppConfigHelper
{
private static readonly IConfiguration _configuration;
static AppConfigHelper()
{
_configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory) // 控制台运行目录
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
}
///
/// 获取字符串配置值
///
public static string Get(string key)
{
return _configuration[key];
}
///
/// 获取强类型配置值
///
public static T GetValue(string key)
{
return _configuration.GetValue(key);
}
///
/// 获取绑定到类的配置对象(适用于嵌套配置)
///
public static T GetSection(string sectionName) where T : new()
{
var obj = new T();
_configuration.GetSection(sectionName).Bind(obj);
return obj;
}
}
}