代码之家  ›  专栏  ›  技术社区  ›  CodeMonkey

Web服务强制转换异常为什么?

  •  1
  • CodeMonkey  · 技术社区  · 17 年前
    Error Cannot implicitly convert type 'string[]' to 'System.Collections.Generic.List<string>'
    

    上面的错误是在我调用web服务的方法时引起的

    List<string> bob = myService.GetAllList();
    

    地点:GetAllList=

    [WebMethod]
            public List<string> GetAllList()
            {
    
                List<string> list ....
                return list;
            }
    

    我已经重建了整个解决方案,更新了服务引用,但我仍然得到了一个cast异常,有什么想法吗?

    2 回复  |  直到 17 年前
        1
  •  5
  •   REA_ANDREW    17 年前

    您需要这样做:

    List<string> bob = new List<string>(myService.GetAllList());
    

    泛型列表构造函数的重载采用指定类型的IEnumerable来初始化数组。您不能像异常状态一样,将其隐式地强制转换为该类型。

        2
  •  1
  •   Gerrie Schenck    17 年前

    请尝试以下方法:

    List<string> bob = new List<string>(myService.GetAllList());