Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回IEnumerable<;T>;来自web服务_C#_.net_Web Services_Generics_C# 4.0 - Fatal编程技术网

C# 如何返回IEnumerable<;T>;来自web服务

C# 如何返回IEnumerable<;T>;来自web服务,c#,.net,web-services,generics,c#-4.0,C#,.net,Web Services,Generics,C# 4.0,我想知道为什么不能通过web服务返回IEnumerable 在我的Web服务中,我返回了一个IEnumerable,但当我在VS2010中检查IntelliSense时,我发现它给了我一个T[] 有人能给我解释一下这种行为吗?来自: 我可以在Web服务中使用泛型吗? 不幸的是,没有。Web服务必须公开基于WSDL的契约。这种契约总是受到所使用的消息格式的表达能力的限制。例如,基于HTTP-GET的web服务只支持诸如int或string之类的基本类型,而不支持诸如DataSet之类的复杂类型。基

我想知道为什么不能通过web服务返回
IEnumerable

在我的Web服务中,我返回了一个
IEnumerable
,但当我在VS2010中检查IntelliSense时,我发现它给了我一个
T[]

有人能给我解释一下这种行为吗?

来自:

我可以在Web服务中使用泛型吗?

不幸的是,没有。Web服务必须公开基于WSDL的契约。这种契约总是受到所使用的消息格式的表达能力的限制。例如,基于HTTP-GET的web服务只支持诸如int或string之类的基本类型,而不支持诸如DataSet之类的复杂类型。基于SOAP的web服务功能更强,但SOAP不能表示泛型类型参数。因此,目前无法定义依赖泛型类型的web服务。也就是说,您可以定义依赖于封闭构造的泛型类型的.NET web服务,例如:

public class MyWebService 
{
   [WebMethod]
   public List<string> GetCities() 
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisco");
      cities.Add("London");
      return cities;
   }
}
公共类MyWebService
{
[网络方法]
公共列表(城市)
{
列表城市=新列表();
城市。添加(“纽约”);
城市。添加(“旧金山”);
城市。添加(“伦敦”);
回归城市;
}
}

在上面的示例中,列表将被封送为字符串数组

数组
T[]
支持
IEnumerable

正在返回
IEnumerable

public IEnumerable某物()
{
返回新的T[]{};
}

您是否询问有关ASMX web服务或WCF的信息?两者的答案是不同的。
    public IEnumerable<T> Something()
    {
        return new T[] {};
    }