将appsetting.json部分绑定到具有非公共属性或具有非匹配名称的属性的模型
.NET6-我的工作正常
{
"SomeSetting": {
"A": "value1",
"B": "value2"
}
}
internal class MyClass // <-- NOTE, the class is internal
{
public string A { get; set; } // <-- but property must be public, including public setter
public string B { get; set; }
}
. . . . .
var inst = config.GetSection("SomeSetting").Get<MyClass>();
,可以正常工作^^。
但是,除非属性具有姓名或访问级别 - 公开,否则它不会具有约束力。有可能有这样的东西吗? -
internal class MyClass
{
internal string A { get; set; } // <-- internal
internal string B { get; set; }
}
或
internal class MyClass
{
public string A { get; internal set; } // <-- internal setter
public string B { get; internal set; }
}
我
internal class MyClass
{
[SomeAttribute()]
internal string A { get; set; } // <-- internal
// or
[SomeOtherAttribute("B")]
internal string BBB { get; set; } // <-- property name is not matching json file
}
没有深入研究DLL,我认为这是JSON的绑定,所以我尝试了
使用System.Text.json.Serialization;
jsonpropertynameattribute
jsonincludeattribute
但到目前为止 - 没有运气
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了道路。 @kirkwoll(谢谢 - 学习)不起作用。我想,我不能保证
.get&lt; myClass&gt;()
使用该技术。我尝试了文档中描述的各种方式而没有成功。但是有一种简单的方法,就在那里。您只需创建像这样的读取模型
并像这样绑定 - 使用其他选项,
以解决
IconFiguration
中的JSON的读取问题>I found the way. The immutability with System.Text.Json as offered by @KirkWoll (thank you - good learning) did not work. I guess, I can't guarantee that
.Get<MyClass>()
uses that technology. I tried every way described in the document without success.But there was a simple way, right there. You just create your read-only model like this
and bind it like this - use the additional options
This solves the issue of Read-Only model bind to JSON in
IConfiguration