C#如何初始化包含列表的对象 标准值

问题描述 投票:2回答:2

这个问题与this问题有关。我设法进一步,但我现在无法使用默认值初始化我的整个对象,以防止它在列表级别为空。这样做的目的是将“null”值传递给我的SQL查询。最终我想要的是我的数据库中的一条记录,它将表示:此行已被记录,但相关值为“null”。

我尝试过布莱恩的小提琴,但似乎不适合用标准值初始化整个模型。

期望:在对象初始化时,应使用“空”值,然后在有通过JSON反序列化的值的情况下覆盖。

这是我尝试过的。这些都不会产生预期的效果。我收到此错误:

Application_Error:System.ArgumentNullException:值不能为null。

每次我尝试访问数据模型中的一个列表。

namespace Project.MyJob
{
 public class JsonModel
 {
    public JsonModel()
    {
        Type_X type_x = new Type_X(); // This works fine.
        List<Actions> action = new List<Actions>(); // This is never visible 
        /*in my object either before I initialise JObject or after. So whatever 
        gets initialised here never makes it to my object. Only Type_X appears 
        to be working as expected. */
        action.Add(new Actions {number = "null", time = "null", station = 
        "null", unitState = "null"}) // This also does not prevent my 
        //JsonModel object from being null.
    }
    public string objectNumber { get; set; }
    public string objectFamily { get; set; }
    public string objectOrder { get; set; }
    public string location { get; set; }
    public string place { get; set; }
    public string inventionTime { get; set; }
    public string lastUpdate { get; set; }
    public string condition { get; set; }

    public Type_X Type_X { get; set; }
    public List<Actions> actions { get; set; }         
 }

 public class Actions
 {
    public Actions()
    {
      // None of this seems to play a role at inititialisation.
      count = "null";
      datetime = "null";
      place = "null";
      status = "null";
    }

    // public string count { get; set; } = "null"; should be the same as above 
      // but also does not do anything.
    public string count { get; set; }
    public string datetime { get; set; }
    public string place { get; set; }
    public string status { get; set; }
 }

 public class Type_X
 {  
    public Type_X
    {
      partA = "null"; // This works.
    }

    public string partA { get; set; }

    public string PartB { get; set; }

    public string partC { get; set; }

    public string partD { get; set; }

    public string partE { get; set; }
  }
}

这就是我现在根据Brian的答案初始化对象的方法。

JObject = JsonConvert.DeserializeObject< JsonModel >(json.ToString(), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

当我尝试迭代Actions的内容时,它(逻辑上)给了我上面提到的null错误。

for (int i = 0, len = JObject.actions.Count(); i < len; i++)

我目前对构造函数初始化的理解:

  • 如果我定义诸如count = "null";之类的值,它们应该出现在任何创建的新对象中。
  • 如果存在默认值,那么我还会期望具有默认值(例如ex。的计数)的项目的列表将是Count() 1而不是null。这怎么可能呢?
c# json
2个回答
3
投票

这会让你脱离你的绑定:

private List<Actions> _actions = new List<Actions>();
public List<Actions> actions { get => _actions; set => _actions = value ?? _actions; }

这导致尝试将动作设置为null以将其设置为先前的值,并且它最初不是null所以它永远不会是null

我不是很确定我正在读你的问题,所以这里是partA的相同片段:

private string _partA = "null";
public string partA { get => _partA; set => _partA = value ?? _partA; }

0
投票

我发现在某些情况下,使用模型上的默认构造函数初始化通用列表会增加易用性。否则,在应用任何逻辑之前,您总是希望验证它们不是null(即使是像检查列表长度这样简单的事情)。特别是如果实体在用户代码之外被水合,即数据库,webapi等......

一种选择是将初始化分为两部分。第1部分是默认构造函数的基本初始化,第2部分是水合逻辑的其余部分:

JObject = new List <YourModel>();

... <反序列化代码>

或者,您可以在反序列化代码中执行此操作,但这会增加一些复杂性。遵循此方法将允许您清理其他区域中的代码,因为每次访问都不需要立即进行空检查。

© www.soinside.com 2019 - 2024. All rights reserved.