admin管理员组文章数量:1200985
Context:
I have a 8 console app and I have a poco mapped to a section of the appsetting.json file.
Code to deserialize the appsetting.json AppSettings section to the Poco:
_settings = _configuration.GetRequiredSection("AppSettings").Get<AppSettings>();
AppSettings:
public class AppSettings
{
public List<PipeCfg> Pipes { get; set; }
}
PipeCfg:
public class PipeCfg {
....
public const string DefaultFileNamePattern = "^.+$";
private string[] _fileNamePatterns;
private static readonly string[] DefaultFileNamePatterns = new [] {DefaultFileNamePattern};
public string[] FileNamePatterns
{
get => _fileNamePatterns == null || _fileNamePatterns.Length == 0 ? DefaultFileNamePatterns: _fileNamePatterns;
set
{
// Here I logged the value that is passed to the function
string valueAsStr =
value switch
{
null => "NULL",
[] => "Empty",
_ => String.Join(", ", value)
}
;
_log.Info($"FileNamePatterns({Id}) -> {valueAsStr}");
_fileNamePatterns = value;
}
}
.....
appsettings.json:
{
....
"AppSettings": {
"Pipes": [
{
....
"FileNamePatterns": ["^.*\\.txt$"]
}
],
...
FileNamePatterns
is a new property I added to the PipeCfg
existing class, and my thought was to initialize the FileNamePatterns array to a default value in case it is not specified in the appsettings.json file. This is to keep the app backwards compatible with the existing appsettings.json file.
Issue:
The value that is assigned to FileNamePatterns is actually: ["^.+$", "^.*\\.txt$"]
instead of ["^.*\\.txt$"]
which I found surprising. If I don't assign the default value and have simply public string[] FileNamePatterns {get; set;}
it works as expected, of course.
Any idea why the api is doing this?
TIA
本文标签:
版权声明:本文标题:.net core - IConfiguration.GetRequiredSection("...").Get<> doesn't overwrite array prope 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738615656a2102893.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论