优化功能

This commit is contained in:
不做码农
2023-06-01 18:47:11 +08:00
parent 68d43240e7
commit 65543e651c
4 changed files with 33 additions and 7 deletions

View File

@@ -45,17 +45,26 @@ namespace Infrastructure
public static List<T> App<T>(params string[] sections)
{
List<T> list = new();
// 引用 Microsoft.Extensions.Configuration.Binder 包
Configuration.Bind(string.Join(":", sections), list);
try
{
if (Configuration != null && sections.Any())
{
Configuration.Bind(string.Join(":", sections), list);
}
}
catch
{
return list;
}
return list;
}
public static T Bind<T>(string key, T t)
{
Configuration.Bind(key, t);
return t;
}
public static T GetAppConfig<T>(string key, T defaultValue = default)
{
T setting = (T)Convert.ChangeType(Configuration[key], typeof(T));
@@ -74,5 +83,16 @@ namespace Infrastructure
{
return Configuration[key];
}
/// <summary>
/// 获取配置节点并转换成指定类型
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="key">节点路径</param>
/// <returns>节点类型实例</returns>
public static T Get<T>(string key)
{
return Configuration.GetSection(key).Get<T>();
}
}
}