提问人:Benjamin 提问时间:6/25/2020 最后编辑:Benjamin 更新时间:6/25/2020 访问量:168
使用选项时,应如何在 .NET Core 配置中键入可枚举属性?
How should you type an enumerable property in .NET Core configuration when using options?
问:
我正在尝试找出在通过appsettings.json配置填充的选项对象中使用可枚举属性的最佳方法。
我在下面有一个示例对象,我将从如下所示的 appsettings.json 文件中配置该对象:
public class MyOptions
{
public ICollection<string> ListOne { get; set; } = Array.Empty<string>();
public string[] ListTwo { get; set; } = Array.Empty<string>();
public ICollection<string> ListThree { get; } = Array.Empty<string>();
}
{
"MyOptions": {
"ListOne": [ "one", "two", "three" ],
"ListTwo": [ "one", "two", "three" ],
"ListThree": [ "one", "two", "three" ]
}
}
这里的问题是,在使用 FxCop 分析器时,会针对 ListOne 发出警告:。
还有一个针对 ListTwo 的不同版本:.
如果你像在 ListThree 中所做的那样从 ListOne 使用 setter 来修复警告,那么配置根本不会读入,因为它需要一个 setter。CA2227: Collection properties should be read only
CA1819: Properties should not return arrays
所以我的问题是,如果你不能做到这些,你如何在没有任何警告的情况下填充一个可枚举的属性?
答: 暂无答案
评论