2009-08-13 63 views
1

我有一個看起來像這樣了與發送一個WebService對象的列表.NET程序

[WebMethod] 
    public int Import(System.Collections.Generic.List<Record> importRecords) 
    { 
     int count = 0; 
     if (importRecords != null && importRecords.Count > 1) 
     { 
      DataLayer datalayer = new DataLayer(); 
      foreach (Record brec in importRecords) 
       if (rec != null) 
       { 
        datalayer.InsertUpdateRecord(rec); 
        count++; 
       } 
     } 
     return count; 
    } 

一個Web服務,我有一個希望使用此方法

將數據發送到Web服務客戶端軟件
ImportService.BVRImportService importService = new ImportService.ImportService(); 
ImportService.Record myRecord = new ImportService.Record(); 
myRecord.FirstName = "Adam"; 
System.Collections.Generic.List<ImportService.Record> myRecords = 
    new List<ImportService.Record>(); 
myRecords.Add(myRecord); 
importService.ImportData(myRecords); 

當我嘗試編譯客戶端軟件時,我總是收到此消息。

Error 1 The best overloaded method match for 'ImportTask.ImportService.ImportService.ImportData(ImportTask.ImportService.Record[])' has some invalid arguments 
Error 2 Argument '1': cannot convert from 'System.Collections.Generic.List<ImportTask.ImportService.Record>' to 'ImportTask.ImportService.BVRRecord[]' 

有誰知道我做錯了嗎?

回答

2

它看起來像客戶端包含記錄[]而不是List<Record>的引用。您可以通過調用List<Record>上的.ToArray方法來解決此問題。

importService.ImportData(myRecords.ToArray()); 

我也很迷惑在代碼中使用Record,但在錯誤消息中使用了BVRRecord。你是在改變你的解決方案中的類型名稱還是實際上有2種不同的類型?如果是後者,則在調用ImportData之前,還需要轉換爲BVRRecord類型。

+0

謝謝你的工作。 – zSynopsis 2009-08-13 15:37:34

+0

我在將對象更改爲記錄之前複製了該錯誤消息。它仍然做同樣的事情。但是你的解決方案解決了錯誤。 – zSynopsis 2009-08-13 15:40:10

+2

您也可以通過單擊「添加服務引用」對話框中的「高級」按鈕並將其設置爲使用通用列表而不是數組來進行修復。 – 2009-08-13 18:54:55