I have one of these very typical 'Binary stream '#' does not contain a valid BinaryHeader' problems, but after much searching can't see a cause for it. I've moved the problem down to the absolute basics and still I get the error.
This is my class I'm serializing:
[Serializable] ref class ExportRequestData { public:
property int ThisRecordingId;
property DateTime ^ThisRecordingStartTime;
property array<DateTime> ^FromTimes;
property array<TimeSpan> ^Durations;
property String ^CameraAFilename;
property String ^CameraBFilename;
property String ^CameraCFilename;
property String ^CameraDFilename;
property String ^SubtitleTags;
property String ^SaveAsFilename;
ExportRequestData(int ThisRecordingId, DateTime ^ThisRecordingStartTime, array<开发者_StackOverflow;DateTime> ^FromTimes, array<TimeSpan> ^Durations,
String ^CameraAFilename, String ^CameraBFilename, String ^CameraCFilename, String ^CameraDFilename,
String ^SubtitleTags, String ^SaveAsFilename);
};
And this is my code stripped down to do an absolute basic test:
try
{
ExportRequestData ^ExportRequestData1 = gcnew ExportRequestData(ThisRecordingId, ThisRecordingStartTime, FromTimes, Durations,
CameraAFilename, CameraBFilename, CameraCFilename, CameraDFilename,
SubtitleTags, SaveAsFilename);
MemoryStream ^MemoryStream1 = gcnew MemoryStream();
BinaryFormatter ^BinaryFormatter1 = gcnew BinaryFormatter();
BinaryFormatter1->Serialize(MemoryStream1, ExportRequestData1);
MemoryStream1->Flush();
MemoryStream1->Position = 0;
MemoryStream1->Seek(0, SeekOrigin::Begin);
ExportRequestData ^ExportRequestData2;
ExportRequestData2 = (ExportRequestData^)BinaryFormatter1->Deserialize(MemoryStream1); //<<<< THIS CAUSES ERROR
MemoryStream1->Close();
}
catch (Exception ^e)
{
MessageBox::Show(e->ToString(), L"Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
}
This is the error produced: "Binary stream '128' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."
So I'm deserializing the same memeory stream I have just serialized, after seeking back to the start of it, thus removing any posibility of the stream being corrupted prior to deserializing. The contents of MemoryStream1 looks good (starts with 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF..., and ends with ...0x00, 0x00, 0x00, 0x00, 0x0B which matches several examples I've seen. The only odity I've noticed is that within the stream the field names are preceeded with "<backing_store>" but I can't find anything to say why that is bad. I'm down to a conclusion that maybe DateTime or TimeSpan can't be serialised, but I can't find anything to say that is the case and that isn't the error message being produced. If anyone can shed some light on this I'd be very grateful as my head is hurting from all the banging.
Thanks Adam
Well you let me down stackoverflowers but I got to the bottom of it. The problem is the DateTime^ objects in the class. If I use DateTime instead it works, but it seems to be a more sensible solution to actually convert to a string and avoid any potential local time problems. I've posted about it here http://www.electronic-designer.co.uk/programming/visual-cpp-cli-dot-net/serialization/serialization-of-a-class
精彩评论