Making a Typed DataTable Serializable

C

cpnet

I have a DataTable defined in a strongly-typed DataSet:

[Serializable()]
public class MyDataSet: DataSet...
{
...

public class MyDataTable: DataTable...
{ ...}

...
}


I have a class that references MyDataTable:


[Serializable()]
public class MyClass
{
private MyDataTable myDataTable;
}


When I try to serialize (binary serialization) MyClass, it tells me I can't
because MyDataTable is not serializable. I had thought that as a child
class of MyDataSet, that MyDataTable would be serializable, but I guess not.
I built my dataset and datatable with the built in dataset designer, which
auto-generates the .cs code. Is there anyway to assign the [Serializable]
attribute to MyDataTable, without manually editing the .cs file for the
dataset? (I don't want to do this because as soon as I use the designer
again, I'll lose my manual changes). MyClass does need to be able to
serialize myDataTable, so I can't just mark it as [NonSerialized()].

Thanks
 
W

Walter Wang [MSFT]

Hi,

Thank you for your post.

In ADO.NET 1.x, the DataTable class suffers from three main limitations. A
DataTable can't be used with Web service methods and doesn't provide a
direct way to serialize its contents. In addition, while DataTable
instances can be passed across a remoting channel, strongly typed DataTable
instances cannot be remoted. These limitations have been removed in ADO.NET
2.0.

In case you cannot upgrade to .NET 2.0, here we can only manually mark the
MyDataTable class as Serializable to make it work.

If you have read Jeffrey Richter's article:

#Run-time Serialization, Part 3
http://msdn.microsoft.com/msdnmag/issues/02/09/net/default.aspx

You maybe wondering if it's possible to use ISerializationSurrogate to
serialize the DataTable without modifying generate source code.
Unfortunately, the answer is no and following article explains why:

#Binary Serialization of ADO.NET Objects
http://msdn.microsoft.com/msdnmag/issues/02/12/CuttingEdge/

Hope this helps.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top