简体   繁体   English

无法从“列表”转换 <T> 到&#39;T []&#39;

[英]Cannot Convert from 'List<T>' to 'T[]'

I am trying to pass a list object of type List<UploadQueue> to a WCF SOAP method of the same parameter type and I am getting the error: 我试图将类型为List<UploadQueue>的列表对象传递给相同参数类型的WCF SOAP方法,但出现错误:

Cannot Convert from 'System.Collections.Generic.List' to 'WebAPI.Upload.UploadQueue[]' 无法从“ System.Collections.Generic.List”转换为“ WebAPI.Upload.UploadQueue []”

I don't understand this because my WCF method's (below) parameter type is List<UploadQueue> : 我不明白这一点,因为我的WCF方法的(以下)参数类型是List<UploadQueue>

IService.DoUpload(List<UploadQueue> request)

Here is the code that calls "DoUpload" which returns the above error. 这是调用“ DoUpload”的代码,该代码返回上述错误。

    List<UploadQueue> results = new List<UploadQueue>();
    HttpPostedFile m_objFile  = default(HttpPostedFile);
    int m_objFlag = default(int);
    Guid m_objGuid = Guid.NewGuid();
    DateTime m_objDate = DateTime.Now;

    try
    {
        if (Request.Files.Count > 0)
        {
            for (var j = 0; i <= (Request.Files.Count - 1); j++)
            {
                m_objFile = Request.Files[j];

                if (!(m_objFile == null | string.IsNullOrEmpty(m_objFile.FileName) | m_objFile.ContentLength < 1))
                {
                    results.Add(new UploadQueue(
                        m_objGuid,
                        m_objFlag,
                        m_objFile.ContentLength,
                        m_objFile.FileName,
                        m_objDate)
                    );

                }

            }
        }

    }
    catch (Exception ex)
    {
        //handle error
    }

    retParam = upload.DoUpload(results);

Ideas? 有想法吗? Thanks. 谢谢。

In your client project, you need to right click on the service reference and select "Configure Service Reference". 在您的客户项目中,您需要右键单击服务参考,然后选择“配置服务参考”。 On configuration screen, in the Data Type section, you need to set the collection type to System.Collections.Generic.List instead of System.Array. 在配置屏幕上的“数据类型”部分,您需要将集合类型设置为System.Collections.Generic.List而不是System.Array。

The generated client has replaced the List with an Array (The default behaviour). 生成的客户端已用数组替换列表 (默认行为)。 With VS.NET 2008 you have the option of generating this with a List instead- look at the Configure Service Dialog Box . 使用VS.NET 2008,您可以选择使用列表生成此文件,而不是查看“ 配置服务”对话框 As other have said ToArray will work. 正如其他人所说,ToArray可以工作。

Try doing results.ToArray(). 尝试执行results.ToArray()。 That will probably fix it. 那可能会解决它。

upload.DoUpload(results.ToArray());

The problem is that the soap service says that it wants an array of objects, and not a list. 问题是肥皂服务说它需要一个对象数组,而不是一个列表。 When the proxy class is built from the WSDL, it converts it to the most basic object it can that satisfies the needs of the service which is an array. 当从WSDL构建代理类时,它将其转换为可以满足服务需求的最基本对象,该对象是一个数组。

retParam = upload.DoUpload(results.ToArray());

...或类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM