gpt4 book ai didi

c# - 无法将 list 传输到 Web 服务?

转载 作者:行者123 更新时间:2023-11-30 13:34:38 24 4
gpt4 key购买 nike

我的服务器和网络服务上有相同的类。我有以下网络方法:

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

return o.Count;
}

但是下面的代码,在服务器上运行:

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());
}
}

出现以下错误:Argument '1': cannot convert from 'System.Collections.Generic.List<Purchase>' to 'localhost.Purchase[]' .

如何转移 list<Purchase>反对网络服务?

最佳答案

当使用这样的网络服务时,默认情况下 List<T>被转换成一个数组(T[])。通过执行 .ToArray() 将列表转换为数组在将其传递给方法之前。

另一种选择是更改 Web 服务代码生成设置以使用列表而不是数组。

您似乎也有重复的类(class),都是本地类(class),名为 Purchase以及通过 Web 服务生成的,也称为 Purchase .尽管它们具有相同的名称,但它们是两种不同的类型(它们的 namespace 不同)。您要么必须坚持使用一组类型,要么使用类似 Automapper 的类型在你的两组类型之间进行映射。

关于c# - 无法将 list<T> 传输到 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2628579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com