I use BinaryWriter to write my items to stream. How i can write object to Stream and back without BinaryFormatter?
2 Answers
Simply, you need to (one of):
- write code that (de)serializes each field/property in turn, using the (reader/)writer API over each member
- write code that automates the first option at compile-time, generating C#
- write code that automates the first option at runtime, generating IL (or C# which it compiles)
Taking into account nested objects, nulls, reference-tracking, collections, lists, serialization callbacks, string value-vs-reference equality, conversion operators, custom iterators, surrogates, serialization-contexts, IO buffering, etc.
Alternatively, use one of the many binary serializers that exist that already do that. I'm hugely biased as the author, but I'd use protobuf-net.
Comments
My biased answer is http://binaryserializer.codeplex.com.
It allows you to define bindings to control the exact format of the data.
BinaryFormatter? What are your actual requirements here?