Код: Выделить всё
public record TestConfig(NCrontab.CrontabSchedule Schedule);
_builder.ConfigureAppConfiguration(options =>
{
options.AddTomlFile("Config.toml");
options.AddTomlFile("Config.Develop.toml");
});
_builder.ConfigureServices((ctx, services) =>
{
var test = ctx.Configuration.GetSection(nameof(TestConfig)).Get() ?? throw new Exception();
}
Код: Выделить всё
public class CronTabConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override object? ConvertFrom(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
{
if(value is null)
{
return null;
}
if (value is string strValue)
{
return CrontabSchedule.TryParse(strValue);
}
return base.ConvertFrom(context, culture, value);
}
public static void RegisterTypeConverter()
{
TypeDescriptor.AddAttributes(typeof(string), new TypeConverterAttribute(typeof(CronTabConverter)));
}
}
Я не против переключения файла конфигурации на Json, если это можно сделать таким образом.
Подробнее здесь: https://stackoverflow.com/questions/791 ... figruation
Мобильная версия