C# binary serialization fail

I used the following codes to serialize an object of a custom class (which also has some members that are also custom classes):

    MyType engine = new MyType(...);
    using (FileStream fs = new FileStream(filename, FileMode.Create))
{
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(fs, engine);
}

After running the code, I am getting a file which doesnt seem to have the right contents to capture the object:

          :Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Also, when I deserialized the object, i am getting an error:

End of Stream encountered before parsing was completed.

After I added to all related custom classes the attribute [Serializable]. The output file looks more like correct now in that it has detailed data of the object in the file. But then I am getting another different error when I deserialized:

Unable to find assembly ‘Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.

Any advice a appreciated.

  • 1

    Did you forget to Dispose the Stream? And: BinaryFormatter is dangerous and obsolete

    – 




  • Actually, I just rewrite the code above with a using block. I got a non-zero size file, but the file doesnt seem to capture the object I want to serailize. This is the file content:  :Lib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

    – 




  • Please use edit to add the changes to your question. It would be more readable than comments here…..

    – 

  • and when i tried derialize the file above, it fails… wonder if there is additional requirements on the class design except adding the attribute Serializable to make this work

    – 

  • 1

    End of Stream encountered before parsing was completed. — please edit your question to add a minimal reproducible example showing serializable classes and code that reproduces the problem, as suggested by How to Ask.

    – 




This seems to work:

[Serializable]
public class MyType {
    public int a;
    public int b;
}
using System.Runtime.Serialization.Formatters.Binary;

internal class Program
{
    private static void Main(string[] args)
    {
        MyType engine = new MyType();
        engine.a = 2;
        engine.b = 4;
        File.Delete("d:\\temp\\test.bin");
        FileStream fs = new FileStream("d:\\temp\\test.bin", FileMode.CreateNew);
        //Format the object as Binary
        BinaryFormatter formatter = new BinaryFormatter();
        //It serialize the employee object
#pragma warning disable SYSLIB0011
        formatter.Serialize(fs, engine);
#pragma warning restore SYSLIB0011
        fs.Flush();
        fs.Close();

        fs = new FileStream("d:\\temp\\test.bin", FileMode.Open);

#pragma warning disable SYSLIB0011
        MyType engine2 = (MyType)formatter.Deserialize(fs);
#pragma warning restore SYSLIB0011
        System.Console.WriteLine();
        System.Console.WriteLine($"engine2.a = {engine2.a}");
        System.Console.WriteLine($"engine2.b = {engine2.b}");
    }
}

output:

engine2.a = 2
engine2.b = 4

Leave a Comment