我有以下類:列表具有無效的參數
public class Selections
{
public List<Selection> selection { get; set; }
}
public class Selection
{
public Promotion promotion { get; set; }
public Products products { get; set; }
}
public class Products
{
public List<int> productId { get; set; }
}
我創建列表和分配屬性值,但是當我加入列表我收到提示:
The best overloaded method match for 'System.Collections.Generic.List.Add(Selection)' has some invalid arguments
C#代碼:
Selections productSelections = new Selections();
List<Selection> listOfProductSelections = new List<Selection>();
Selection dataSelection = new Selection()
{
promotion = new ProviderModels.Common.Promotion()
{
promotionID = Convert.ToInt32(applicablePromotion.PromotionId),
lineOfBusiness = applicablePromotion.LineOfBusiness
},
products = new ProviderModels.Common.Products()
{
productId = GetIdsOfSelectedProducts(context, selectedOffer)
}
};
productSelections.selection.Add(listOfProductSelections);
我錯過了什麼嗎?
確定。但在此之前,我還必須添加'listOfProductSelections.Add(dataSelection)'。對?另外,雖然我初始化'選擇dataSelection =新選擇()' –
在上面的代碼中調用''AddRange'on productSelections.selection'我碰上空引用異常。如果該屬性未初始化,則它將爲'null'。所以你會遇到異常。 – Sefe