views:

325

answers:

1

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(); }
    }
    ...
}
....
....
private static readonly BinaryFormatter logSerializer = new BinaryFormatter();
....
....
public void SerializeLog(IList<LogEntry> logEntries)
{
        using (MemoryStream serializationStream = new MemoryStream())
        {
            logSerializer.Serialize(serializationStream, logEntries);
            this.binarySerializedLog = serializationStream.GetBuffer();
        }
}

In some machine (32 or 64 bit machine), it is serializing in binary format - which is expected. But in some machine ( all of them are 64 bit machine and not for debug builds) it is not serializing, binarySerializedLog is showing ToString() value of all individual Data, class name (...LogEntry) and id value. My question is - are there specific reason for this type of behavior or am I doing some mistake? Thanks in advance.

A: 

Your question isn't very clear (can you define "not serializing"?), but some thoughts:

You should really use ToArray() to capture the buffer, not GetBuffer() (which is cheaper, but returns the oversized array, and should only be used in conjunction with Length).

Where are you seeing this .ToString()? BinaryFormatter writes the objects type, then either uses reflection to write the fields (for [Serializable]) or uses customer serialization (for ISerializable). It never calls .ToString() (unless that is what your ISerializable does). However, strings etc will be in the output "as is".

Note that BinaryFormatter can be brittle between versions, so be careful if you are keeping this data for any length of time (it is generally fine for transport, though, assuming you update both ends at the same time). If you know in advance what your .Data objects are, there are a range of contract-based serializers that might provide more stability. I can provide more specific help if you think this would be worth investigating.

Marc Gravell
After serializing into binary format it should look like - AAEAAAD/////AQAAAAAAAAAMAgAAAHlNaWNyb3NvZnQ....AAAAAAA== - Instead of that, it is showing something like - ExecutionResponse`1[Response2]LogEntry2 - where ExecutionResponse`1[Response2] is in .Data (in this particular case Data is contains single item of type ExecutionResponse which has list of Response2 objects (here single Response2 object in the list), LogEntry is the current class name nad 2 is the id.
malay
Well, the latter is broadly what I'd expect - this *looks* to be the type metadata that goes before the raw data, then the fields... I'm not convinced it is wrong (yet).
Marc Gravell
But in the second case not all the data present in serialized log, right? I mean it will not be possible to de-serialize from the second log ( i.e. in ExecutionResponse`1[Response2]LogEntry2). Nothing about Response2 is present there even though Response2 is serializable.
malay
So what *do* you get back if you try to deserialize it?
Marc Gravell