Serialization Problem RRS feed

  • Question

  • I have a class like below


    public abstract class Class1 : ISerializable
    public delegate long RunTimeMethod();
    protected Dictionary<string, RunTimeMethod> _runTimeMethods = new Dictionary<string, RunTimeMethod>();

    When running the following code to serialize this class into memory stream for several times, one of the serialized ms.ToArray().Length returns a different size of bytes with an additional string of "Class1+RunTimeMethod".


    using (MemoryStream ms = new MemoryStream())\
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, instance);


    However, when use the following code, everytime the serialized file is the same even when the ms.ToArray().Length return above is different.

    FileStream fs = new FileStream( @"c:\test.txt", FileMode.Create );

    BinaryFormatter formatter = new BinaryFormatter();

    formatter.Serialize( fs, instance );


    I don't know why this is behaving like this? Are there any difference to serialize the same class to a MemoryStream or FileStream?

    Thanks for any advice

    Monday, December 7, 2009 11:01 AM


All replies

  • It may depend on what you do in the GetObjectData method.
    Monday, December 7, 2009 1:55 PM
  • If i remove the delegate from the GetObjectData, it works fine. But my question is that why the delegate sometimes can't be serialized? is this a bug in the serialization?
    Wednesday, December 9, 2009 12:55 AM
  • Unless you post the code you have in GetObjectData, it's not possible to know what goes wrong.
    When you implement ISerializable, it's your code which tells how your class is serialized.
    Wednesday, December 9, 2009 12:34 PM
  • Add the NonSerialized attribute to the delegate as well.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Marked as answer by Polytheme Saturday, December 12, 2009 11:54 AM
    Friday, December 11, 2009 9:17 AM