我正在尝试使用以下代码向列表中添加项目:
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
吗? - Sean