对象反序列化的时候出错?????

chinamzone 2008-04-24 09:54:05
请看下列代码???
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Myspace
{
public class Myclass
{
static void Main()
{
IFormatter formatter= new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
StateObject obj = (StateObject) formatter.Deserialize(stream);
stream.Close();

Console.WriteLine(obj.ip.ToString());

}
}

[Serializable]
public class StateObject
{
public byte[] buffer = new byte[124000];
public IPAddress ip = IPAddress.None;
}

}

未处理的异常: System.InvalidCastException: 无法将类型为“Myspace.StateObject”
的对象强制转换为类型“Myspace.StateObject”。
在 Myspace.Myclass.Main()
...全文
161 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinamzone 2008-05-05
  • 打赏
  • 举报
回复
已经好了,谢谢回帖!
chinamzone 2008-04-24
  • 打赏
  • 举报
回复
是的,是由另外一个程序序列化生成的
xinyun80 2008-04-24
  • 打赏
  • 举报
回复
你的文件MyFile.bin是序列化而生成的吗?
gomoku 2008-04-24
  • 打赏
  • 举报
回复
我的测试没有问题,是不是文件格式不对?

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, new StateObject());
stream.Close();

110,534

社区成员

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

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

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