我真的不知道如何解釋這一點,但我很難讓我的代碼正常作業。
我正在 .NET 中開發一個 Web API,并在我的代碼中有這個模型:
public class NewBasketDTO
{
public string Identifier { get; set; }
public Array Items { get; set; }
}
我想在這里實作的是,我希望能夠像這樣以 JSON 格式將物件傳遞給我的陣列:
{
identifier: "someidentifier",
items: [
{ productId: 1, quantity: 1 },
{ productId: 3, quantity: 2 },
{ productId: 4, quantity: 1 }
]
}
但是我遇到了很大的問題,因為在 PostMan 中我收到了這個錯誤:
System.NotSupportedException: 集合型別“System.Array”是抽象的、介面或只讀的,并且無法實體化和填充。路徑:$.items | 行號:2 | 位元組位置行內:12。
如何將物件傳遞給我的公共陣列項?
我已經搜索了這個并沒有找到足夠的答案,在此先感謝您的幫助
uj5u.com熱心網友回復:
System.Array是一個抽象類,不能被實體化。您可能想要定義Items為一種List<ItemDTO>型別。
public class ItemDTO
{
public int ProductId { get; set; }
public int Quantity { get; set; }
}
public class NewBasketDTO
{
public string Identifier { get; set; }
public List<ItemDTO> Items { get; set; }
}
或者,您可以使用以下陣列ItemDTO:
public class NewBasketDTO
{
public string Identifier { get; set; }
public ItemDTO[] Items { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/389234.html
下一篇:在創建串列時使用case陳述句
