嵌套类列表,对象引用未设置为实例

时间:2018-06-21 17:50:12

标签: c# class nested

我正在尝试设置一个列表,但是我得到一个未设置为实例错误的对象。如果我删除PricingList类中的List <>,它将正常工作。我确定这是我所缺少的简单东西,在req.pricingList.plist.Add(prlist)行中生成了错误。

class Program
{
    static void Main(string[] args)
    {
        //   Do Test 
        PriceRequest req = new PriceRequest();
        PricingHeader hdr = new PricingHeader();
        Pricing prlist = new Pricing();
        PricingList lst = new PricingList();

        req.pricingList = lst;
        req.pricingList.plist.Add(prlist);
        req.pricingList.plist[0].quantity = "1";

        Console.WriteLine("  ");
    }
}

public class PriceRequest
{

    public PricingHeader pricingHeader { get; set; }
    public PricingList pricingList { get; set; }
}

public class PricingHeader
{
    public string supplierCode { get; set; }
    public string accountNumber { get; set; }
    public string accountNumberReference { get; set; }
    public string version { get; set; }
}

public class PricingList
{
    public List<Pricing> plist { get; set; }
}

public class Pricing
{
    public string quantity { get; set; }
    public string unitPrice { get; set; }
    public string suplierPartId { get; set; }
}

1 个答案:

答案 0 :(得分:-1)

您似乎尚未初始化PricingList实例的plist属性。