J
Joe Cool
I have this class in a project:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
namespace myProject
{
[Serializable]
public class Attachment
{
#region class variables
ArrayList _fields = null;
string _application;
int _docid;
#endregion
#region contructors
public Attachment()
{
_fields = new ArrayList();
_application = string.Empty;
_docid = 0;
}
#endregion
#region properties
public string Application
{
get { return _application; }
set { _application = value; }
}
public ArrayList Fields
{
get { return _fields; }
set { _fields = value; }
}
public int DocID
{
get { return _docid; }
set { _docid = value; }
}
#endregion
#region public methods
public byte[] Serialize()
{
MemoryStream memoryStream = null;
BinaryFormatter formatter = null;
try
{
memoryStream = new MemoryStream();
formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, this);
return memoryStream.ToArray();
}
catch (Exception ex)
{
return null;
}
finally
{
memoryStream.Close();
}
}
public void Deserialize(byte[] attachmentBytes)
{
BinaryFormatter formatter = null;
MemoryStream memoryStream = null;
Attachment tempAttachment = null;
object tempObject = null;
try
{
formatter = new BinaryFormatter();
memoryStream = new MemoryStream(attachmentBytes);
formatter.Binder = new GenericBinder();
memoryStream.Position = 0;
tempObject = formatter.Deserialize(memoryStream);
tempAttachment = (Attachment)tempObject;
this.Application = tempAttachment.Application;
this.Fields = tempAttachment.Fields;
this.DocID = tempAttachment.DocID;
}
catch(Exception ex)
{
return;
}
}
#endregion
}
}
When I try to invoke the Deserialize on a byte array that I just
serialized, it throws this exception:
Binary stream '0' does not contain a valid BinaryHeader. Possible
causes are invalid stream or object version change between
serialization and deserialization.
I have googled t his exception and cannot find any solution that fixes
my problem. Any help would be appreciated.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
namespace myProject
{
[Serializable]
public class Attachment
{
#region class variables
ArrayList _fields = null;
string _application;
int _docid;
#endregion
#region contructors
public Attachment()
{
_fields = new ArrayList();
_application = string.Empty;
_docid = 0;
}
#endregion
#region properties
public string Application
{
get { return _application; }
set { _application = value; }
}
public ArrayList Fields
{
get { return _fields; }
set { _fields = value; }
}
public int DocID
{
get { return _docid; }
set { _docid = value; }
}
#endregion
#region public methods
public byte[] Serialize()
{
MemoryStream memoryStream = null;
BinaryFormatter formatter = null;
try
{
memoryStream = new MemoryStream();
formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, this);
return memoryStream.ToArray();
}
catch (Exception ex)
{
return null;
}
finally
{
memoryStream.Close();
}
}
public void Deserialize(byte[] attachmentBytes)
{
BinaryFormatter formatter = null;
MemoryStream memoryStream = null;
Attachment tempAttachment = null;
object tempObject = null;
try
{
formatter = new BinaryFormatter();
memoryStream = new MemoryStream(attachmentBytes);
formatter.Binder = new GenericBinder();
memoryStream.Position = 0;
tempObject = formatter.Deserialize(memoryStream);
tempAttachment = (Attachment)tempObject;
this.Application = tempAttachment.Application;
this.Fields = tempAttachment.Fields;
this.DocID = tempAttachment.DocID;
}
catch(Exception ex)
{
return;
}
}
#endregion
}
}
When I try to invoke the Deserialize on a byte array that I just
serialized, it throws this exception:
Binary stream '0' does not contain a valid BinaryHeader. Possible
causes are invalid stream or object version change between
serialization and deserialization.
I have googled t his exception and cannot find any solution that fixes
my problem. Any help would be appreciated.