Why I can serialize a LinkedList<myClass> ?

  • Thread starter Thread starter Alberto Bencivenni
  • Start date Start date
A

Alberto Bencivenni

Hi All,


Is there a reason why it is not possible to serialize a geenric
LinkedList<T> object containing a custom class marke serializable?


Thanks,

Alberto
 
Alberto said:
Is there a reason why it is not possible to serialize a geenric
LinkedList<T> object containing a custom class marke serializable?
It is possible over here:

using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
class A { }

class Program {
public static void Main() {
LinkedList<A> l = new LinkedList<A>();
l.AddLast(new A());
new BinaryFormatter().Serialize(Console.OpenStandardOutput(), l);
}
}

So you'll have to be more specific about your problem.
 
Hi Jeroen,

Yes, we would like to serialize it via BinaryFormatter on a file on
disk.

FileInfo f = new FileInfo(filename);

Stream myStream = f.OpenWrite();

BinaryFormatter myBinaryFormat = new BinaryFormatter();

myBinaryFormat.Serialize(myStream, myLinkedList);

myStream.Close();

Thanks,

Alberto
 
Alberto said:
Yes, we would like to serialize it via BinaryFormatter on a file on
disk.

FileInfo f = new FileInfo(filename);

Stream myStream = f.OpenWrite();

BinaryFormatter myBinaryFormat = new BinaryFormatter();

myBinaryFormat.Serialize(myStream, myLinkedList);

myStream.Close();
This code works without any problems on my machine, for a trivial LinkedList
with only one element. So again: what's the *specific* error you run into?
There is no *fundamental* restriction on serializing a LinkedList, so
without more details of what "myLinkedList" is exactly and why it's not
working for you it's impossible to guess.

Here's the easy test for the most obvious thing that could be going wrong,
though: is your custom class actually serializable? Simply marking it
[Serializable] is not enough: all of its members (public and private) have
to be serializable too, for starters. Can you successfully serialize
individual instances of your class outside the LinkedList?
 
Back
Top