我正在使用Asp.Net Core 2.2,我想将一个类型的实例注册为基本类型。
在启动文件中,我尝试按如下所示注册类型和实例:
foreach (var type in typesFromAssemblies)
{
var settings = Activator.CreateInstance(type);
foreach (var prop in type.GetProperties())
{
if (!prop.CanRead || !prop.CanWrite)
{
continue;
}
var key = type.Name + "." + prop.Name;
// load instance from database
var setting = GetSettingByKey<string>(provider, key, loadSharedValueIfNotFound: true);
if (setting == null)
{
continue;
}
object value = CommonHelper.GetMovCustomTypeConverter(prop.PropertyType)
.ConvertFromInvariantString(setting);
// set property
prop.SetValue(settings, value, null);
}
// how I can set "settings" in services?
// services.Add(new ServiceDescriptor(settings.GetType(), settings, lifetime));
}
如何为服务设置“设置”实例?