簡體   English   中英

C# 將項目添加到列表

[英]C# adding items to List

我正在嘗試使用以下內容將項目添加到列表中;

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  }})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM