I have a collection of objects that I need to write to a binary file.
I need the bytes in the file to be compact, so I can't use BinaryFormatter
. BinaryFormatter
throws in all sorts of info for deserialization needs.
If I try
byte[] myBytes = (byte[]) myObject
I get a runtime exception.
I need this to be fast so I'd rather not be copying arrays of bytes around. I'd just like the cast byte[] myBytes = (byte[]) myObject
to work!
OK just to be clear, I cannot have any metadata in the output file. Just the object bytes. Packed object-to-object. Based on answers received, it looks like I'll be writing low-level Buffer.BlockCopy
code. Perhaps using unsafe code.
To convert an object to a byte array:
You just need copy this function to your code and send to it the object that you need to convert to a byte array. If you need convert the byte array to an object again you can use the function below:
You can use these functions with custom classes. You just need add the
[Serializable]
attribute in your class to enable serializationWell a cast from
myObject
tobyte[]
is never going to work unless you've got an explicit conversion or ifmyObject
is abyte[]
. You need a serialization framework of some kind. There are plenty out there, including Protocol Buffers which is near and dear to me. It's pretty "lean and mean" in terms of both space and time.You'll find that almost all serialization frameworks have significant restrictions on what you can serialize, however - Protocol Buffers more than some, due to being cross-platform.
If you can give more requirements, we can help you out more - but it's never going to be as simple as casting...
EDIT: Just to respond to this:
Please bear in mind that the bytes in your objects are quite often references... so you'll need to work out what to do with them.
I suspect you'll find that designing and implementing your own custom serialization framework is harder than you imagine.
I would personally recommend that if you only need to do this for a few specific types, you don't bother trying to come up with a general serialization framework. Just implement an instance method and a static method in all the types you need:
One thing to bear in mind: everything becomes more tricky if you've got inheritance involved. Without inheritance, if you know what type you're starting with, you don't need to include any type information. Of course, there's also the matter of versioning - do you need to worry about backward and forward compatibility with different versions of your types?
I found another way to convert object in byte[]. Hier is my solution:
Regards
I believe what you're trying to do is impossible.
The junk that
BinaryFormatter
creates is necessary to recover the object from the file after your program stopped.However it is possible to get the object data, you just need to know the exact size of it (more difficult than it sounds) :
this can be recovered via:
The reason why the above methods don't work for serialization is that the first four bytes in the returned data correspond to a
RuntimeTypeHandle
. TheRuntimeTypeHandle
describes the layout/type of the object but the value of it changes every time the program is ran.EDIT: that is stupid don't do that -->
If you already know the type of the object to be deserialized for certain you can switch those bytes forBitConvertes.GetBytes((int)typeof(yourtype).TypeHandle.Value)
at the time of deserialization.If you want the serialized data to be really compact, you can write serialization methods yourself. That way you will have a minimum of overhead.
Example:
Take a look at Serialization, a technique to "convert" an entire object to a byte stream. You may send it to the network or write it into a file and then restore it back to an object later.