2011-07-20 53 views
1

我有一個方法在vs2010中的C#應該返回系統的通用對象。因爲這些方法返回不同類型的結果,如int,datatable,string。該對象可以容納INT以及字符串,但是當我緊握或分配數據表的通用對象則拋出誤差 -System.Object類可以保存數據表對象的對象嗎?

Server was unable to process request. ---> There was an error generating the XML document. ---> The type System.Data.DataTable may not be used in this context. To use System.Data.DataTable as a parameter, return type, or member of a class or struct, the parameter, return type, or member must be declared as type System.Data.DataTable (it cannot be object). Objects of type System.Data.DataTable may not be used in un-typed collections, such as ArrayLists.

,因爲我正在開發Web方法所以這就是爲什麼它引發的錯誤的其他部分。

通用對象可以保存數據表對象嗎?

+0

請花一些時間並重新提出問題,以便我們可以更好地理解它。 – naveen

回答

-1

是的,它可以。在Stackoverflow上有很多相似或相關的問題。看到這個,例如,它可以幫助你:

.NET - Convert Generic Collection to DataTable

+0

@Mamra Dalal @:可以在C中的對象類中保存數據表的對象嗎? – Priyanka

+0

是的Priyanka它可以。爲什麼downvote?你可以解釋嗎? –

+0

我沒有給出反對票。 – Priyanka

0

是通用的對象可以容納一個DataTable(也可以easyly檢查 - 見下面的代碼)。我想你的問題不是通過自己來保存泛型對象,而是使用WebService和序列化集合的可能性(本地方法可以)。

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main() 
     { 
      List<DataTable> t = new List<DataTable>(); 
      t.Add(new DataTable()); 
      ArrayList x = new ArrayList(); 
      x.Add(new DataTable()); 
     } 
    } 
} 
0

所有的.NET類型從System.Object繼承,因此System.Object可以容納DataTable

您的問題似乎與您的設計更相關。如果它與Web服務相關,請考慮不要返回DataTable或DataSet之類的類型,因爲這些類型可能不容易在非.NET平臺上使用。而是在類/結構中表示數據。

相關問題