二进制序列化C#:反序列化方法给我异常
我在套接字之间通信的Wpf应用程序中遇到序列化/反序列化问题。 详情如下: 在这两个应用程序中 我有一个实现ISerializable的类:二进制序列化C#:反序列化方法给我异常,c#,.net,wpf,serialization,binaryformatter,C#,.net,Wpf,Serialization,Binaryformatter,我在套接字之间通信的Wpf应用程序中遇到序列化/反序列化问题。 详情如下: 在这两个应用程序中 我有一个实现ISerializable的类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization; using Syst
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace GraficaClient
{
[Serializable()] //Set this attribute to all the classes that want to serialize
class SerializerObject : ISerializable
{
public String type;
public String txt;
/*public String rft;
public Byte[] audio;
public Byte[] img;
*/
public SerializerObject()
{
}
//Deserialization constructor.
public SerializerObject(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
type = (String)info.GetValue("type", typeof(String));
txt= (String)info.GetValue("txt", typeof(String));
/* rft = (String)info.GetValue("rft", typeof(String));
audio = (Byte[])info.GetValue("audio", typeof(Byte[]));
img = (Byte[])info.GetValue("img", typeof(Byte[]));*/
}
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("type", type);
info.AddValue("txt", txt);
/*info.AddValue("rft", rft);
info.AddValue("audio",audio);
info.AddValue("img", img);*/
}
}
}
在我的一个应用程序中,我将信息放入其中(在本例中,仅两个字符串)
并在流中序列化它
TcpClient c = new TcpClient();
SerializerObject o = new SerializerObject();
o.type="t";
o.text="hello";
c.Connect(ip,port);
NetworkStream stream = c.GetStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, o);
在另一边
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
BinaryFormatter b = new BinaryFormatter();
SerializerObject o= (SerializerObject)b.Deserialize(stream);
这个指令给了我一个例外
mscorlib.dll中首次出现类型为“System.Runtime.Serialization.SerializationException”的异常
如果我打印e.message=“不可能找到l'assembly'ProgettoServerV2,版本=1.0.0.0,区域性=neutral,PublicKeyToken=null。”
e、 stacktrace=in System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()\r\n in System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo,String name)\r\n in System.Runtime.Serialization.Formatters.Binary.ObjectMap..(String objectName,String[]memberNames,BinaryTypeEnum[]BinaryTypeEnum,Object[]TypeInformation,Int32[]MemberAssesMids,ObjectReader ObjectReader,Int32 objectId,BinaryAssemblyInfo assemblyInfo,SizedArray assemblyInfo,SizedArray assemIdToAssemblyTable)\r\n在System.Runtime.Serialization.Formatters.BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped记录)\r\n在System.Runtime.Serialization.Formatters.Binary.\u BinaryParser.ReadObjectWithMapTyped(BinaryHeaderium BinaryHeaderium)\r\n在System.Runtime.Serialization.Formatters.Binary.\u BinaryParser.Run()\r\n在System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize中(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize中的HeaderHandler处理程序、uu BinaryParser serParser、Boolean fCheck、Boolean isCrossAppDomain、IMethodCallMessage方法CallMessage)\r\n(流序列化流、HeaderHandler处理程序、Boolean fCheck、Boolean isCrossAppDomain、IMethodCallMessage方法CallMessage)\r\n在c:\Users\pietro\Desktop\GraficClient\GraficClient\GraficClipboard.GestoreClipboard()中的System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)\r\n
目前我正在使用VisualStudio 2013。
如何解决此问题。确保程序可以加载丢失的程序集。错误消息中有什么让人难以理解?Met me引述了您的长文章中唯一相关的信息: e、 message=“无法找到l'assembly'ProgettoServerV2,版本=1.0.0.0,区域性=中性, PublicKeyToken=null'。” 确保此程序集可用,反序列化将正常工作