namespace Base
{
public class AppSettings
{
static IConfiguration Configuration { get; set; }
public AppSettings(IConfiguration configuration)
{
Configuration = configuration;
}
///
/// 封装要操作的字符
///
/// 节点配置
///
public static string App(params string[] sections)
{
try
{
if (sections.Any())
{
return Configuration[string.Join(":", sections)];
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "";
}
///
/// 递归获取配置信息数组
///
///
///
///
public static List App(params string[] sections)
{
List list = new();
try
{
if (Configuration != null && sections.Any())
{
Configuration.Bind(string.Join(":", sections), list);
}
}
catch
{
return list;
}
return list;
}
public static T Bind(string key, T t)
{
Configuration.Bind(key, t);
return t;
}
public static T GetAppConfig(string key, T defaultValue = default)
{
T setting = (T)Convert.ChangeType(Configuration[key], typeof(T));
var value = setting;
if (setting == null)
value = defaultValue;
return value;
}
///
/// 获取配置文件
///
/// eg: WeChat:Token
///
public static string GetConfig(string key)
{
return Configuration[key];
}
///
/// 获取配置节点并转换成指定类型
///
/// 节点类型
/// 节点路径
/// 节点类型实例
public static T Get(string key)
{
return Configuration.GetSection(key).Get();
}
}
}