Faced the problem that you need to write data to a binary file in one project, and read them from another project. When trying to do this, the program issued an error: "Unhandled type exception" System.Runtime.Serialization.SerializationException "in mscorlib.dll

Additional information: Could not find assembly "Discount1, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null". "

I give the code where I write the data to the file.

BinaryFormatter formatter = new BinaryFormatter(); FileStream fs; fs = new FileStream("2.dat", FileMode.Open, FileAccess.Write); formatter.Serialize(fs, shoppers); fs.Close(); Console.WriteLine("#МАСССИВ ОБЪЕКТОВ СЕРИАЛИЗОВАН#"); 

Now the code where I read data from another project, before that, transferred the file with data from the old project to the new one:

  BinaryFormatter formatter1 = new BinaryFormatter(); FileStream fs1; fs1 = new FileStream("2.dat", FileMode.Open, FileAccess.Read); Nodes[] shoppers1 = (Nodes[])formatter1.Deserialize(fs1); foreach (Nodes n in shoppers1) { Console.Write(n.Name + " "); Console.Write(n.Surname + " "); Console.Write(n.Lastname + " "); if (n is NewNodes) Console.Write(n.PhoneNumber()); Console.WriteLine(); } fs1.Close(); 
  • one
    That program gave you such a mistake? o_O - VladD
  • @VladD, Unhandled exception of type "System.Runtime.Serialization.SerializationException" in mscorlib.dll Additional information: Could not find assembly "Discount1, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null". - IWProgrammer
  • Ok, this is closer to the subject. And what is really inside the file? Take a look. // It is possible that you have to reverse-engineering of the recording format. This is a long and difficult task. - VladD
  • @VladD, file header. As soon as he became serious, he immediately brought him out to check. There is a set of objects of a certain class - IWProgrammer
  • one
    Do you happen to have different library assemblies used in different projects? Discount1 can the fact that the assembly is not used as a shared library? - Dmitry Nail

2 answers 2

Your problem is that the BinaryFormatter class that you use to serialize and deserialize into a file saves the full name of the type along with the full name of the assembly. Accordingly, if the formatter does not detect this assembly during deserialization, a SerializationException . In order to deserialize the data in the second project, you need to connect exactly the assembly whose types were serialized earlier in the first project. If there is no such possibility, and you yourself ensure that there are similar definitions of the types of serialized objects in another project, you can create a binder that overrides the assembly name and type during deserialization and passes the necessary information to the formatter. To do this, it is enough to determine the successor of the abstract class System.Runtime.Serialization.SerializationBinder

  public class CustomBinder : SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { Assembly currentasm = Assembly.GetExecutingAssembly(); return Type.GetType($"{currentasm.GetName().Name}.{typeName.Split('.')[1]}"); } } 

and then after creating the BinaryFormatter instance, assign its instance to the Binder property of the created Binder instance:

  Proj2.Node node; IFormatter formatter = new BinaryFormatter(); formatter.Binder = new CustomBinder(); using (FileStream fs = new FileStream("data.bin", FileMode.Open, FileAccess.Read)) { node = formatter.Deserialize(fs) as Proj2.Node; } 

    I did not have a return Type.GetType($"{currentasm.GetName().Name}.{typeName.Split('.')[1]}"); because of this return Type.GetType($"{currentasm.GetName().Name}.{typeName.Split('.')[1]}");

    Changed 1 to 0, was not the index.

    • Are you sure your answer on the topic? :) - Alexander Muksimov