无法传输列表< T>以Web服务? [英] Can't transfer list<T> to web service?

查看:200
本文介绍了无法传输列表< T>以Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的服务器和我的web服务相同的类。
我有以下的WebMethod:

I have the same classes on my server and on my web service. I have the following WebMethod:

[WebMethod]
        public int CreateOrder(List<Purchase> p, string username)
        {
            o.Add(new Order(p,username));

            return o.Count;
        }

但是以下code,在服务器上运行:

However the following code, run at server:

protected void CartRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        List<Purchase> l = ((List<Purchase>)Session["Cart"]);

        if (e.CommandName == "Order")
        {
            localhost.ValidateService WS = new localhost.ValidateService();
            WS.CreateOrder(l, Session["username"].ToString());
        }
    }

提供了以下错误:参数1:不能从转换'System.Collections.Generic.List&LT;购买&GT;'以localhost.Purchase []

我怎么可以在列表&LT转移;购买&GT; 对象到Web服务

How can I transfer the list<Purchase> object to the web service?

推荐答案

在使用Web服务一样,默认情况下列表&LT; T&GT; 被转换成一个阵列( T [] )。这样做将您的列表到一个数组 .ToArray()之前将它传递给方法。

When using web services like that, by default List<T> gets converted into an array (T[]). Convert your list into an array by doing .ToArray() before passing it to the method.

另一种方法是更改​​Web服务code生成设置,而不是使用数组列表。

Another option is to change the web service code generation settings to use lists instead of arrays.

看来你也有重复的课程,无论是当地的一个叫购买而这在Web服务中产生的之一,也被称为购买。即使他们有相同的名字,他们是两种不同类型的(它们的命名空间是不同的)。你要么必须坚持一组类型,或者使用类似 Automapper 您的两套类型之间进行映射

It seems you also have duplicate classes, both a local one called Purchase and the one that's generated over the web service, also called Purchase. Even though they have the same name, they're two different types (their namespaces are different). You'll either have to stick to one set of types, or use something like Automapper to map between your two sets of types.

这篇关于无法传输列表&LT; T&GT;以Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆