代码拉取完成,页面将自动刷新
using System;
using System.ComponentModel;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using zijian666.Core;
namespace zijian666.WebApi
{
public partial class WebApiOptions
{
/// <summary>
/// 绑定配置类
/// </summary>
/// <param name="key">配置前缀</param>
/// <param name="configType">配置类</param>
public void BindConfigurationSection(string key, Type configType)
{
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
if (configType is null)
{
return;
}
//_services.ConfigureOptions(configType);
_services.AddSingleton(configType, p =>
{
var configInstance = ActivatorUtilities.CreateInstance(p, configType);
var configuration = p.GetService<IConfiguration>();
configuration?.Bind(key, configInstance);
return configInstance;
});
}
/// <summary>
/// 绑定指定配置类,配置前缀为 <seealso cref="CategoryAttribute.Category"/> 或类名(未标记特性时)
/// </summary>
/// <param name="configType"></param>
public void BindConfigurationSection(Type configType)
{
if (configType is null)
{
return;
}
var attr = configType.GetCustomAttribute<CategoryAttribute>();
var key = attr?.Category ?? configType.Name;
BindConfigurationSection(key, configType);
}
/// <summary>
/// 绑定所有被标记为<seealso cref="CategoryAttribute"/>的配置类
/// </summary>
public void BindConfigurationSection()
{
foreach (var (type, attr) in AssemblyResolver.Default.GetTypesWithAttribute<CategoryAttribute>())
{
BindConfigurationSection(attr.Category, type);
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。