当try 使用.NET 6从appSettings.json获取配置中的集合时,我遇到了一个奇怪的行为.
我的班级:
public record SomeOptions
{
public IReadOnlyCollection<string> SomeColl { get; init; } = new List<string> { "wrongString" };
}
我的appcastings.json:
{
"Some": {
"SomeColl": [ "expectedString" ]
}
}
然后我try 获得这样的值:
var section = configuration.GetSection("Some");
var options = section.Get<SomeOptions>();
我从属性(world-String)获得了默认值.但如果改变属性就像:
public record SomeOptions
{
public IReadOnlyCollection<string> SomeColl { get; init; } = null!;
}
或
public record SomeOptions
{
public ICollection<string> SomeColl { get; init; } = new List<string>();
}
我从appsettings.json
(expected字符串)中获得了值.有人能解释一下--为什么会发生这种情况吗?