将appsetting.json部分绑定到具有非公共属性或具有非匹配名称的属性的模型

发布于 2025-02-08 03:25:23 字数 1438 浏览 0 评论 0 原文

.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

但到目前为止 - 没有运气

.net6 - I have this working fine

{
  "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>();

That works fine ^^.

But it is not binding unless property has name or access level - public. Is it possible to have something like this? --

internal class MyClass 
{
    internal string A { get; set; } // <-- internal
    internal string B { get; set; }
}

or

internal class MyClass 
{
    public string A { get; internal set; } // <-- internal setter
    public string B { get; internal set; }
}

or

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
}

I did not look deep into DLLs, I thought that this is JSON binding, so I tried
using System.Text.Json.Serialization; JsonPropertyNameAttribute JsonIncludeAttribute

But so far - no luck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2025-02-15 03:25:23

我找到了道路。 @kirkwoll(谢谢 - 学习)不起作用。我想,我不能保证 .get&lt; myClass&gt;()使用该技术。我尝试了文档中描述的各种方式而没有成功。

但是有一种简单的方法,就在那里。您只需创建像这样的读取模型

public class MyClass 
{
    public string A { get; private set; } 
    public string B { get; private set; }
}

并像这样绑定 - 使用其他选项,

var inst = config.GetSection("SomeSetting")
    .Get<MyClass>(options => { options.BindNonPublicProperties = true; });

以解决 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

public class MyClass 
{
    public string A { get; private set; } 
    public string B { get; private set; }
}

and bind it like this - use the additional options

var inst = config.GetSection("SomeSetting")
    .Get<MyClass>(options => { options.BindNonPublicProperties = true; });

This solves the issue of Read-Only model bind to JSON in IConfiguration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文