[求助]请教一个对象序列化和反序列化的问题!!!!

greennba99 2005-02-22 11:20:29
public static byte[] doSerialize(object obj)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formater=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream ms=new System.IO.MemoryStream();
formater.Serialize(ms,obj);
byte[] info=ms.ToArray();
ms.Close();
return info;
}


private System.Collections.ArrayList doDeserialize(byte[] bt)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formater=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream ms=new System.IO.MemoryStream(bt);
ms.Position=0;
System.Collections.ArrayList NameList=(System.Collections.ArrayList)formater.Deserialize(ms);
ms.Close();
return NameList;
}

我用以上两个方法对ArrayList进行序列化和反序列号(在服务端序列化,通过网络传到客户端反序列化),但总是出现以下异常,请问要怎样解决?非常感谢!!!!

未处理的“System.Runtime.Serialization.SerializationException”类型的异常出现在 mscorlib.dll 中。

其他信息: BinaryFormatter 版本不兼容。应为版本 1.0。接收到的版本为 16777216.50331648。
...全文
101 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
webserv2 2005-02-22
  • 打赏
  • 举报
回复
OBJECT对象序列化转换为byte[] byte[]反序列化转换为OBJECT对象


Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim img As Image = PictureBox1.Image
Dim img2 As Image = CType(GetObject(ReadObject(img)), Image)
PictureBox2.Image = img2
End Sub

Public Shared Function ReadObject()Function ReadObject(ByVal obj As Object) As Byte()
Dim ms As MemoryStream = New MemoryStream
Dim bf As BinaryFormatter = New BinaryFormatter
bf.Serialize(ms, obj)
ms.Close()
Return ms.ToArray()
End Function

Public Shared Function GetObject()Function GetObject(ByVal bytes As Byte()) As Object
Dim ms As MemoryStream = New MemoryStream(bytes, 0, bytes.Length)
Dim bf As BinaryFormatter = New BinaryFormatter
Return bf.Deserialize(ms)

End Function
greennba99 2005-02-22
  • 打赏
  • 举报
回复
是在客户端反序列化时出现了以上异常。

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧