Serialization of an object containing LinkedList

J

Justin Crites

I have an object which I want to be serializable. I have marked with
with [SerializableAttribute]. The object only has a single data
member, which is a LinkedList<int>. This linked list is a private
member and cannot be exposed publically without violating encapsulation
of the class.

If I make the class derive from ISerializable and
IDeserializationCallback, I can attempt to "forward" these methods onto
the linked list. For example (assuming _list is our LinkedList<int>):

void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context) {
_list.GetObjectData(info, context);
}

void IDeserializationCallback.OnDeserialization(object sender)
{
_list.OnDeserialization(sender);
}

This works fine. However, there's a problem: LinkedList does not
publically expose the necessary constructor. ISerializable requires
that the type have implemented a special constructor for
deserialization. This constructor takes the same arguments as
GetObjectData(), and I can implement that in my type, but what do I do?

So that's the question: how can I implement a serializable type that
contains a LinkedList?
 
N

Nicholas Paldino [.NET/C# MVP]

Justin,

You shouldn't have to do any of that. Unless you have some other reason
for implementing custom serialization, the LinkedList<int> should serialize
just fine with using the Serializable attribute and nothing else.

I ran a test here serializing a class which contained a LinkedList and
it worked just fine.

Hope this helps.
 
G

Guest

What about serialization of a LinkedList of List<T> where T is either a
primitive type or a class? In both cases, I get the following exception when
creating the XmlSerializer() instance:

There was an error reflecting type 'HoldsLinkedList'. --->
System.InvalidOperationException: You must implement a default accessor on
System.Collections.Generic.LinkedList`1[[System.Collections.Generic.List`1[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]] because it inherits from
ICollection.

where

public class HoldsLinkedList
{
private LinkedList<List<int>> list;

public LinkedList<List<int>> List
{
get { return list; }
set { list = value; }
}
}

Nicholas Paldino said:
Justin,

You shouldn't have to do any of that. Unless you have some other reason
for implementing custom serialization, the LinkedList<int> should serialize
just fine with using the Serializable attribute and nothing else.

I ran a test here serializing a class which contained a LinkedList and
it worked just fine.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Justin Crites said:
I have an object which I want to be serializable. I have marked with
with [SerializableAttribute]. The object only has a single data
member, which is a LinkedList<int>. This linked list is a private
member and cannot be exposed publically without violating encapsulation
of the class.

If I make the class derive from ISerializable and
IDeserializationCallback, I can attempt to "forward" these methods onto
the linked list. For example (assuming _list is our LinkedList<int>):

void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context) {
_list.GetObjectData(info, context);
}

void IDeserializationCallback.OnDeserialization(object sender)
{
_list.OnDeserialization(sender);
}

This works fine. However, there's a problem: LinkedList does not
publically expose the necessary constructor. ISerializable requires
that the type have implemented a special constructor for
deserialization. This constructor takes the same arguments as
GetObjectData(), and I can implement that in my type, but what do I do?

So that's the question: how can I implement a serializable type that
contains a LinkedList?
 

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