简体   繁体   English

如何将空的 object 从 appsettings.json 加载到字典中?

[英]How can I load an empty object into a dictionary from appsettings.json?

I'm trying to load values into a custom options class on app startup in C#/.net6.我正在尝试在 C#/.net6 中的应用程序启动时将值加载到自定义选项 class 中。 Some of the input from appsettings.json is not being loaded as I would like.来自appsettings.json的一些输入没有按照我的意愿加载。

My options class looks like this:我的选项 class 如下所示:

    public class CustomOptions
    {
        
        public Dictionary<string, CustomInfo> MyOptions { get; set; }
        
    }

    public class CustomInfo
    {

        public Dictionary<string, string> DictionaryField { get; set; } = new Dictionary<string, string>();

        public List<string> ListField { get; set; } = new List<string>();
    }

and appsettings.json contains the following: appsettings.json包含以下内容:

"CustomOptions" : {
    "MyOptions": {
      "OptionOne": {},
      "OptionTwo": {
        "DictionaryField": {
          "ValueOne": "somevalue",
          "ValueTwo": "someothervalue"
        },
        "ListField": [
          "ListItem"
        ]
      }
    }
  }

and loading the options in Program.cs with并加载 Program.cs 中的选项

builder.Services.AddOptions<EndpointOptions>().Bind(builder.Configuration.GetSection(EndpointOptions.Key));

As it stands, OptionTwo is loaded correctly with all fields set, yet OptionOne does not even appear in the injected IOptions<TOptions> service.就目前而言,OptionTwo 已正确加载所有字段集,但 OptionOne 甚至没有出现在注入的IOptions<TOptions>服务中。

What I would like is for OptionOne to be loaded into the MyOptions dict with a default CustomInfo object or null value.我想要的是将 OptionOne 加载到具有默认CustomInfo object 或 null 值的MyOptions字典中。 Is this possible?这可能吗?

Edit: I need to be able to add more options beyond OptionOne and OptionTwo at some point, and ideally would be able to do this through appsettings.json , hence the need for a dictionary in MyOptions class. All these options will still follow the CustomInfo class.编辑:我需要能够在某些时候添加 OptionOne 和 OptionTwo 之外的更多选项,并且理想情况下能够通过appsettings.json执行此操作,因此需要在MyOptions class 中添加字典。所有这些选项仍将遵循 CustomInfo class。

The JsonConfigurationFileParser ignores any empty object and simply add value of null as configuration value, that is ignored during binding . JsonConfigurationFileParser忽略任何空的 object 并简单地将null的值添加为配置值,该值在绑定期间被忽略。

If you really need both options to be added to the dictionary, you could add at least one property to OptionOne section:如果您确实需要将这两个选项都添加到字典中,您可以将至少一个属性添加到OptionOne部分:

"OptionOne": {
  "DictionaryField": {}
}

Frankly, in the sense of low memory allocating, it makes sense to not create an empty instance of CustomInfo , but use the TryGetValue method on dictionary to test if the value exists.坦率地说,在低 memory 分配的意义上,不创建CustomInfo的空实例是有意义的,而是使用字典上的TryGetValue方法来测试该值是否存在。

the easiest way is to use this code最简单的方法是使用此代码

CustomOptions customOptions = configuration.GetSection("CustomOptions").Get<CustomOptions>();

var myOptions = customOptions.MyOptions;

classes班级

public class CustomOptions
{
    public MyOptions MyOptions { get; set; }
}

public class MyOptions
{
    public CustomInfo OptionOne { get; set; }
    public OptionTwo OptionTwo { get; set; }
}
public class CustomInfo
{

    public Dictionary<string, string> DictionaryField { get; set; } = new Dictionary<string, string>();

    public List<string> ListField { get; set; } = new List<string>();
}

public class DictionaryField
{
    public string ValueOne { get; set; }
    public string ValueTwo { get; set; }
}

public class OptionTwo
{
    public DictionaryField DictionaryField { get; set; }
    public List<string> ListField { get; set; }
}

如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 appsettings.json 填充字典? - How can I populate a Dictionary from appsettings.json? 如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json? 如何从 appsettings.json 加载设置字典? - How to load settings dictionary from appsettings.json? 如何将我的 appsettings.json 部分转换为 object? - How can I convert my appsettings.json section to an object? 如何在 appsettings.json 中加载多态对象 - How to load polymorphic objects in appsettings.json 如何在 class 中从 appsettings.json 获取 ConnectionString? - How can I get ConnectionString form appsettings.json in a class? 如何在 a.Net 6 控制台应用程序中读取 appsettings.json? - How can I read the appsettings.json in a .Net 6 console application? 如何从 appSettings.json 中读取特定的数据结构? - How can I read particular data structure from appSettings.json? 如何通过从appsettings.json的数组部分获取值和键来使用GetSection方法? - How can I use GetSection method by getting values and keys from appsettings.json' array parts? 如何从 appsettings.json 获取日期时间? - How to get DateTime from appsettings.json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM