gpt4 book ai didi

C# 向列表中添加项目

转载 作者:行者123 更新时间:2023-12-05 07:11:47 25 4
gpt4 key购买 nike

我正在尝试将项目添加到列表中用于以下内容;

public class Reason
{
[JsonProperty("code")]
public int Code { get; internal set; }
}

public class Item
{
[JsonProperty("Id")]
public int Id { get; set; }

[JsonProperty("quantity")]
public int quantity { get; set; }

[JsonProperty("reason")]
public Reason reason { get; set; } = new Reason();
}

public class RootObject
{
[JsonProperty("dropOff")]
public DropOff dropOff { get; set; } = new DropOff();

[JsonProperty("providerId")]
public int providerId { get; set; }

[JsonProperty("orderReference")]
public string orderReference { get; set; }

[JsonProperty("returnMethodId")]
public int returnMethodId { get; set; }

[JsonProperty("items")]
public List<Item> items { get; set; } = new List<Item>();
}


root.items.Add(new Item { Id = 8675072, quantity = 1, Reason.Code = 2 });

当我尝试添加 Reason.Code 时出现以下错误,

非字段、方法或属性需要对象引用。

我需要在哪里初始化以将 Reason.Code 添加到列表中?

最佳答案

root.items.Add(new Item { Id = 8675072, quantity = 1, reason = { Code = 2  }})

关于C# 向列表中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60634852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com