无法将类型'System.collections.ArrayList'显式转换为mywebservicecall.ServiceReference1.groupitems

时间:2017-12-13 09:56:05

标签: c# web-services c#-4.0 arraylist json.net

我正在尝试传递如下所示的数组列表,但却出现上述错误。只需传递myitem就能正常运行,但我也需要能够传递多个。

这是我的代码:

var deserializedm = JsonConvert.DeserializeObject<List<Mulitipay>>(mpay.ToString());

int num = 0;
ArrayList list = new ArrayList();

foreach (Mulitipay m in deserializedm)
{
    num++;
    string f = m.Fund;
    string a = m.Amount;
    int am = System.Convert.ToInt32(a);
    string g = m.Gl;
    string d = m.Details;


    var itd = new ServiceReference1.glItemDetails();
    itd.fundCode = f;
    itd.additionalReference = reference;


    var its = new ServiceReference1.Data();
    its.description = d;
    its.reference = g;
    its.amountInMinorUnits = am;

    var myitem = new ServiceReference1.sampleGroupItem();
    myitem.lgItemDetails = itd;
    myitem.itemSummary = its;
    myitem.lineId = "tetetetet1232323";
    list.Add(myitem);
}

var alllist = list;

simpleivr.sale = new ServiceReference1.samplesale ();
simpleivr.sale.saleSummary = new ServiceReference1.Data();
simpleivr.sale.saleSummary.description = "stuff1";
simpleivr.sale.saleSummary.reference = "000303";
simpleivr.sale.saleSummary.amountInMinorUnits = 400;
simpleivr.sale.items = new ServiceReference1.SampleGroupItem [] { alllist};

Webservice(网络服务的样本不是完整的东西)

/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order=2)]
[System.Xml.Serialization.XmlArrayItemAttribute("Groupitem" IsNullable=false)]
public SampleGroupItem[] GroupItems{
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
        this.RaisePropertyChanged("Groupitems");
    }
}

1 个答案:

答案 0 :(得分:0)

数组列表与List<Object>类似。您将其分配给类型为SampleGroupItem的数组。您只需将ArrayList更改为List<SampleGroupItem>,然后调用方法.ToArray()
所以你的代码将成为

  var deserializedm = JsonConvert.DeserializeObject<List<Mulitipay>>(mpay.ToString());
        int num = 0;
        List<ServiceReference1.SampleGroupItem> list = new List<ServiceReference1.SampleGroupItem>();

        foreach (Mulitipay m in deserializedm)
        {
            num++;
            string f = m.Fund;
            string a = m.Amount;
            int am = System.Convert.ToInt32(a);
            string g = m.Gl;
            string d = m.Details;


            var itd = new ServiceReference1.glItemDetails();
            itd.fundCode = f;
            itd.additionalReference = reference;


            var its = new ServiceReference1.Data();
            its.description = d;
            its.reference = g;
            its.amountInMinorUnits = am;

            var myitem = new ServiceReference1.sampleGroupItem();
            myitem.lgItemDetails = itd;
            myitem.itemSummary = its;
            myitem.lineId = "tetetetet1232323";
            list.Add(myitem);
        }

        simpleivr.sale = new ServiceReference1.samplesale ();
        simpleivr.sale.saleSummary = new ServiceReference1.Data();
        simpleivr.sale.saleSummary.description = "stuff1";
        simpleivr.sale.saleSummary.reference = "000303";
        simpleivr.sale.saleSummary.amountInMinorUnits = 400;
        simpleivr.sale.items = list.ToArray();