【发布时间】:2011-09-13 08:48:11
【问题描述】:
我目前正在 Visual Studio 2010 中进行测试。我制作了一个客户端和服务器,它们都将通过 UdpClient 连接。
我想将一个对象从客户端发送到服务器。我有两种方法可以将对象转换为字节并将其转换为对象。现在,当我测试我的应用程序时,我无法将它转换回服务器上接收到的对象
我的服务器看到接收到对象并尝试将其从字节转换为对象,但这会出错。
System.Runtime.Serialization.SerializationException was unhandled Message=Unable to find assembly
这似乎没问题,因为两个应用程序位于不同的命名空间中...
这些是我的转换方法;客户端和服务器都一样
public byte[] ToBytes() {
using (MemoryStream stream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Position = 0;
byte[] byteRij = new byte[1024];
stream.Read(byteRij, 0, (int)stream.Length);
return byteRij;
}
}
public static Datagram ToDatagram(byte[] rij) {
using (MemoryStream stream = new MemoryStream()) {
stream.Write(rij, 0, rij.Length);
stream.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
return (Datagram)formatter.Deserialize(stream);
}
}
我该如何解决这个问题? 提前致谢
【问题讨论】:
-
您的
ToBytes()方法将无法正常工作,因为序列化的表单大于 1024 字节。为什么不使用ToArray()method?