can System.Object class hold the object of datatable object?
I'm having one method in the C# in vs2010 which should return generic object of system. Because the methods return different type of result like int, datatable,string . the object can hold int 开发者_JAVA技巧as well as string but when i'm holding or assigning datatable to the generic object then it is throwing error as -
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.
because i'm developing the web method so that's why the other part of error it throws.
Can generic object hold the datatable object ?
Yes generic object can hold a DataTable (it can be easyly checked - see code below). I guess your problem is not with holding generic objects by itself, but with the WebService and possibility to serialize collection (local method would be ok).
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());
}
}
}
All .Net types inherit from System.Object
, therefore System.Object
can hold a DataTable
.
Your problem seems to be more related to your design. If it is related to a web service, consider not returning types like DataTable or DataSet as these may not be easily consumed on non .Net platforms. Instead represent the data in a class/struct.
Yes, it can. There are many similar or related questions asked on Stackoverflow. See this for example, it may help you:
.NET - Convert Generic Collection to DataTable
精彩评论