二进制序列化C#:deserialize方法给我例外

时间:2014-12-13 11:58:41

标签: c# .net wpf serialization binaryformatter

我在socket之间进行通信的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 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);

这条指令给我一个例外 类型为System.Runtime.Serialization.SerializationException'的第一次机会异常。发生在mscorlib.dll

如果我打印e.message ="无法找到l' assembly' ProgettoServerV2,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。"

e.stacktrace =在System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo,String name)中的System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()\ r \ n中\ n在System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName,String [] memberNames,BinaryTypeEnum [] binaryTypeEnumA,Object [] typeInformationA,Int32 [] memberAssemIds,ObjectReader objectReader,Int32 objectId,BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)\ r \ n在System.Runtime.Serialization.Formatters.Binary .__ BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)\ r \ n在System.Runtime.Serialization.Formatters.Binary .__ BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)\ r \ System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize中的System.Runtime.Serialization.Formatters.Binary .__ BinaryParser.Run()\ r \ n中的n(HeaderHandler处理程序,__BinaryParser serParser,Bool ean fCheck,Boolean isCrossAppDomain,IMethodCallMessage methodCallMessage)\ r \ n在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream,HeaderHandler handler,Boolean fCheck,Boolean isCrossAppDomain,IMethodCallMessage methodCallMessage)\ r \ n在System中。在GraficaClient.GestoreClipboard.gestioneClipboard()中的运行时.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)\ r \ n在c:\ Users \ pietro \ Desktop \ GraficaClient \ GraficaClient \ GestoreClipboard.cs:riga 91

目前我正在使用VisualStudio 2013。 我该怎么解决呢。

1 个答案:

答案 0 :(得分:0)

确保程序可以加载缺少的程序集。在错误消息中有什么难以理解的?遇见我在你的页面长篇文章中引用你唯一相关的信息:

  

e.message ="无法找到l' assembly' ProgettoServerV2,Version = 1.0.0.0,Culture = neutral,   公钥=空'"

确保此程序集可用并且反序列化将起作用。