0

I have 2 different projects with 2 different namespace. When i try to serialize object to file with binary serialization everything is fine until i try to deserialize that object in file from second project.

This type of exception is thrown

: 'Unable to find assembly 'Synchro One, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'

 public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
    {
        using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
        {
            var binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(stream, objectToWrite);
        }
    }


    public static T ReadFromBinaryFile<T>(string filePath)
    {
        using (Stream stream = File.Open(filePath, FileMode.Open))
        {
            var binaryFormatter = new BinaryFormatter();
            return (T)binaryFormatter.Deserialize(stream);
        }
    }

These methods are used in both solutions

Marekus
  • 31
  • 5
  • 2
    Do you mean you're trying to serialize an object of one type and then deserialize it as an object of a different type? That's not what binary serialization is designed for. – Jon Skeet Feb 21 '18 at 13:23
  • For this kind of serialization to work you need to have a shared assembly/object. This object should then be used in both sides – Headhunter Xamd Feb 21 '18 at 13:28
  • The assembly name and the typename (includes the namespace) is stored in the serialized bytestream. See for example my answer [here](https://stackoverflow.com/a/48042351/578411). Use a type you share between projects or use a serialization format that doesn't rely on binary compatibility. – rene Feb 21 '18 at 13:30
  • 1
    I see two possible reasons to try to do what you are doing. And both of them are bad ideas: 1. Is that you try to get around strong typisation. Do not do that. Strong Typisation is your biggest friend. Without it, we end up in the PHP or JavaScript cases (http://www.sandraandwoo.com/2015/12/24/0747-melodys-guide-to-programming-languages/). 2. You are in a XY Problem (https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and somehow thought this appraoch would be hte proper Y to solve it. It is most likely **not** – Christopher Feb 21 '18 at 13:32
  • Also: appending to a file that contains a binary serialized stream is bound to break, have unwanted effects on deserialization – rene Feb 21 '18 at 13:33

0 Answers0