binaryformatter

Binary Deserialization with different assembly version

I have a project which uses BinaryFormatter to serialize a collection of structs with string and bool? datatypes. The serialization/deserialization works fine, however if I were to change the assembly which does the work it fails to deserialize because of the header in the binary file indicating that it requires Assembly x instead of As...

Byte serialization

I recently had a discussion with a colleague about serialization of byte data over a network. He used the BinaryFormatter class to "unparse" the byte data I was sending to him. This did not work and he obviously had exceptional... exceptions. Binaryformatter could not "unparse" the data correctly since my data was simply a byte array. H...

How do I ignore event subscribers when serializing an object?

Note that I'm answering my own question since I think it might be useful to have the information on the site! FAQ: It's also perfectly fine to ask and answer your own question, but pretend you're on Jeopardy: phrase it in the form of a question. When the following class is serialized with a BinaryFormatter, any objects subscribin...

Does BinaryFormatter apply any compression?

When .NET's BinaryFormatter is used to serialize an object graph, is any type of compression applied? I ask in the context of whether I should worry about the object graph having many repeated strings and integers. Edit - Hold on, if strings are interned in .NET, there's no need to worry about repeated strings, right? ...

Stuck on Serialization in C#

I have a class that handles serialization in C#, called Serializer. It's implementation is below: public class Serializer { public void SerializeRulesManager(string filename, RulesManager rulesManager) { Stream stream = File.Open(filename, FileMode.Create); try { BinaryFormatter binar...

How to increase deserialization speed?

Serializing/deserializing with BinaryFormatter, resulting serialized file is ~80MB in size. The deserialization takes a few minutes. How could I improve on this? Here's the deserialization code: public static Universe DeserializeFromFile(string filepath) { Universe universe = null; FileStream fs = new FileStream...

How to deserialize or recover a binary serialized dictionary that's not finished serializing?

When I used my app, on close, it tried to serialize a dictionary that's 300 KB. Because of no disk space, it could only write 292 KB. Is there a way to successfully deserialize whatever is in there? I used BinaryFormatter and if I lose some elements that's way better than losing the whole dictionary. When I deserialize I get this excep...

How to change the order of Deserialization using BinaryFormatter in C#?

Lets say I have classA which contains classB and both are [Serializable]. I assumed, that on Deserialization classB would be deserialized first. This is not the case however, as I could confirm by just logging when each [OnDeserialized] methods were hit. Now I have the following issue: After classA's deserialization is complete, it i...

BinaryFormatter alternatives

A BinaryFormatter-serialized array of 128³ doubles, takes up 50 MB of space. Serializing an array of 128³ structs with two double fields takes up 150 MB and over 20 seconds to process. Are there fast simple alternatives that would generate compact files? My expectation is that the above examples would take up 16 and 32 MB, respectively,...

C# Object Binary Serialization

I want to make a binary serialize of an object and the result to save it in a database. Person person = new Person(); person.Name = "something"; MemoryStream memorystream = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(memorystream, person); How can I transform memorystream in a string type to be saved ...

how does binaryformatter serializes objects?

BinaryFormatter behaving in weird way in my code. I have code like following [Serializable] public class LogEntry { private int id; private List<object> data = new List<object>(); public int Id { get { return id; } } public IList<object> Data { get { return data.AsReadOnly(); } ...

Can a IFormatter deserizalize a object of an unreferenced type?

Supose I serialized a third party library type object with BinaryFormatter. An assemby that does not references this library tries to deserialize the bytes. Will it work? I do not expect to it be cast to the correct type, I just want to retrieve it as an object instance so I can group it and serialize it again. ...

Binary stream 'NN' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization

I am passing user defined classes over sockets. The SendObject code is below. It works on my local machine, but when I publish to the WebServer which is then communicating with the App Server on my own machine it fails. public bool SendObject(Object obj, ref string sErrMsg) { try { MemoryStream ms ...

.Net Where to find the official specification of the BinaryFormatter serialization format?

I'd like to know what is the serialization format of the BinaryFormatter. I found this site which give some good informations, but it was obtained by reverse engineering and it is not complete. Where can I find the official specification of the BinaryFormatter serialization format? ...

BinaryFormatter deserialize gives SerializationException

I'm getting an: System.Runtime.Serialization.SerializationException: Unable to find assembly 'myNameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null When trying to deserialize some data in another program than the program I serialized it with. After some googling I've found out that apparently this can only be done using ...

Sending large serialized objects over sockets is failing only when trying to grow the byte Array, but ok when using a massive byte array

I have code where I am trying to grow the byte array while receiving the data over my socket. This is erroring out. public bool ReceiveObject2(ref Object objRec, ref string sErrMsg) { try { byte[] buffer = new byte[1024]; byte[] byArrAll = new byte[0]; bool bAllBytesRead = fals...

Why is BinaryFormatter trying to serialize an Event on a Serializable class?

I have a simple class that is marked as Serializable, and it happens to have an event. I tried to mark the event member as NonSerialized, however the compiler complains. Yet when I go to serialize the class instance, the BinaryFormatter throws an exception that the event is non serializable. Does that mean you can't serialize classes tha...

What is the best way to deserialize generics written with a different version of a signed assembly?

In other cases it has been suggested that you simply add a SerializationBinder which removes the version from the assembly type. However, when using generic collections of a type found in a signed assembly, that type is strictly versioned based on its assembly. Here is what I've found works. internal class WeaklyNamedAppDomainAssembl...

ISerializable and backward compatibility

hello I have to work an an old application that used binaryFormatter to serialize application data into filestream (say in a file named "data.oldformat") without any optimizazion the main class has been marked with attribute <serializable()>public MainClass ....... end class and the serialization code dim b as new binaryformatte...

BinaryFormatter with MemoryStream Question

I am testing BinaryFormatter to see how it will work for me and I have a simple question: When using it with the string HELLO, and I convert the MemoryStream to an array, it gives me 29 dimensions, with five of them being the actual data towards the end of the dimensions: BinaryFormatter bf = new BinaryFormatter(); ...