A
AnkitAsDeveloper [Ankit]
Hi i am serializing a 'ref struct' object as follows :
private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}
private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;
}
// now i am serializing it in follwing way...
WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;
Seri( "a.waypoint", waypoint1 );
//Serialization works fine.... but following deserialization throws me
exception...
WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;
// Exception :
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.
// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}
WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}
Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.
[Ankit Jain]
http://www.ankitjain.info/ankit/
private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}
private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;
}
// now i am serializing it in follwing way...
WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;
Seri( "a.waypoint", waypoint1 );
//Serialization works fine.... but following deserialization throws me
exception...
WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;
// Exception :
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.
// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}
WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}
Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.
[Ankit Jain]
http://www.ankitjain.info/ankit/