我目前正在 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);
}
}
我该如何解决这个问题?提前致谢