将响应对象类型强制转换为列表C#

时间:2017-05-14 10:34:20

标签: c# c#-4.0 c#-3.0 system.reflection

我正在点击一个webservice,它返回一个对象我有一个集合airpricepoint的项目。我创建了类似的类项目和airpricepoint我试图将lowFareSearchRsp.Items转换为IList集合,以便我可以foreach循环并访问内部集合但我无法循环我无法访问fareitems内的集合

IList collection = (IList)lowFareSearchRsp.Items;
if (collection != null)
{
    foreach (var fareitems in collection)
    {
        items itemsobj = new items();
        Document.airpricepoint airpricepointobj = new airpricepoint();
        airpricepointobj.AirPricingInfo = new List<airpricinginfo>();
        airpricepointobj.AirPricingResultMessage = fareitems.AirPricingResultMessage;
        airpricepointobj.FeeInfo = fareitems.FeeInfo;
        airpricepointobj.FareNote = fareitems.FareNote;
        airpricepointobj.TaxInfo = fareitems.TaxInfo;
        airpricepointobj.Key = fareitems.Key;
        airpricepointobj.TotalPrice = fareitems.TotalPrice;
        airpricepointobj.BasePrice = fareitems.BasePrice;
    }


public class items
{
    public IList<airpricepoint> AirPricePoint { get; set; }
}

public class airpricepoint
{
    public IList<airpricinginfo> AirPricingInfo { get; set; }
    public object AirPricingResultMessage { get; set; }
    public object FeeInfo { get; set; }
    public object FareNote { get; set; }
    public object TaxInfo { get; set; }
    public string Key { get; set; }
    public string TotalPrice { get; set; }
    public string BasePrice { get; set; }
}

1 个答案:

答案 0 :(得分:0)

如果它确实是一个Web服务,那么您已经知道您正在接收的数据的结构(格式)。通常它将采用JSON或XML格式;如果它是WCF服务,则打包在合同类中。

这意味着您可以使用任何JSON或XML序列化库对数据进行反序列化。