为什么将属性初始化为null而不是配置中的空列表? [英] Why does a property get initialized to a null instead of an empty list from configuration?

查看:61
本文介绍了为什么将属性初始化为null而不是配置中的空列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net-core Web API项目,其配置包含以下定义:

 "MyClass":{"Value1":1,"Value2":[]} 

此配置用于初始化以下类:

 公共类MyClass{public int Value1 {get;放;}public int [] Value2 {get;放;}} 

该类通过使用 Startup.cs 中的 ConfigureServices 方法中的以下代码从配置中初始化:

  public void ConfigureServices(IServiceCollection服务){services.AddOptions();services.Configure< MyClass>(Configuration.GetSection("MyClass")));services.AddSingleton< AService>();services.AddMvc();} 

我的服务构造函数如下:

  public FeedbackRetrievalService(IOptions< MyClass> myclass){_myclass = myclass;} 

在构造函数中,我注意到 Value2 null ,即使它在配置中确实显示为空列表-这也是我希望该属性获得的值被初始化.我希望仅当配置中缺少该字段时,才使用 null 初始化该属性.有什么方法可以将字段初始化为空列表?

解决方案

问题是JSON配置 loader 和配置 binder 是两个单独的组件.加载配置时,配置提供程序会将整个配置文件加载到键/值结构中,其中键是属性路径.在您的示例中, MyClass:Value2:0 MyClass:Value2:1 可能是 Value2 JSON数组中两个数组元素的路径./p>

配置 binder 随后将解释该结构并将其变成您的类的对象.那时,它将仅具有先前加载的键/值结构中存在的可用信息.

现在,如果在绑定之前查看已加载的配置,您会发现空数组不会产生任何结果.而且,如果您考虑路径的工作原理,这很有意义: MyClass:Value2:0 MyClass:Value2:1 分别是索引的索引0和1的值. Value2 数组.但是,这对于一个空数组将如何工作?根本没有空数组的条目.

因此,由于空数组不会出现在加载的配置中,因此活页夹无法初始化您的数组.

但是,您可以做的是为绑定的配置类型设置默认值:

 公共类MyClass{public int Value1 {get;放;}public int [] Value2 {get;放;} = Array.Empty< int>();} 

现在,如果将具有该空数组的配置绑定到该类型,则绑定器将永远不会触及 Value2 ,而其默认值将保持不变.

I have an asp.net-core web api project with a configuration containing the following definition:

"MyClass": {
   "Value1": 1,
   "Value2": []
}

This configuration is used to initialize the following class:

public class MyClass
{
    public int Value1 { get; set; }
    public int[] Value2 { get; set; }
}

This class is initialized from configuration using the following code in the ConfigureServices method in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();

    services.Configure<MyClass>(Configuration.GetSection("MyClass"));
    services.AddSingleton<AService>();

    services.AddMvc();
}

My service constructor looks like this:

public FeedbackRetrievalService(IOptions<MyClass> myclass)
{
    _myclass = myclass;
}

In the constructor I notice that Value2 is null, even though it does appear in the configuration as an empty list - which is the value I would expect the property to be initialized with. I expected that the property would be initialized with null only when the field is missing from the configuration. Is there any way with which I could initialize the field as an empty list?

解决方案

The problem is that the JSON configuration loader and the configuration binder are two separate components. When you load a configuration, the configuration provider will load the whole configuration file into a key/value structure where the key is a property path. In your example, MyClass:Value2:0 and MyClass:Value2:1 could be paths for two array elements in your Value2 JSON array.

The configuration binder will then later interpret that structure and turn it into an object of your class. At that time, it will only have the information available that exists in that key/value structure that was previously loaded.

Now, if you look at the loaded configuration before binding it, you will notice that an empty array does not result in anything. And if you think about how the path works, it makes sense: MyClass:Value2:0 and MyClass:Value2:1 are the values for index 0 and 1 respectively of the Value2 array. But how would this work for an empty array? There simply isn’t an entry for an empty array.

So since the empty array does not appear in the loaded configuration, the binder cannot initialize your array.

What you can do however is set up default values for bound configuration types:

public class MyClass
{
    public int Value1 { get; set; }
    public int[] Value2 { get; set; } = Array.Empty<int>();
}

Now, if you bind your configuration with that empty array to that type, the Value2 will never be touched by the binder, leaving its default value intact.

这篇关于为什么将属性初始化为null而不是配置中的空列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆