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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com
"Justin Crites" <jc*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...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?