二进制流'0'不包含有效的BinaryHeader

时间:2013-02-20 02:04:38

标签: c#

我得到这个例外“二进制流'0'不包含有效的BinaryHeader。可能的原因是序列化和反序列化之间无效的流或对象版本更改。”我正在寻找关于这个例外的解决方案2天现在没有成功。我试过(我认为)everthing。 有人可以看看下面的代码吗?错误在哪里?

感谢。

    private player ByteArrayToPlayer(byte[] temp)
    {
        try
        {
            MemoryStream objectStream = new MemoryStream(temp);

            IFormatter BinaryFormatter
                        = new BinaryFormatter();

            objectStream.Position =0;

            return (player)BinaryFormatter.Deserialize(objectStream);     <--- get exception
        }
        catch (Exception ex)
        {
            ;
        }
        return null;


    }
    private byte[] PlayerToByteArray(player o)
    {
        try
        {
            objectStream = new MemoryStream();

           IFormatter BinaryFormatter
                        = new BinaryFormatter();

            BinaryFormatter.Serialize(objectStream, o);
            objectStream.Seek(0, SeekOrigin.Begin);
            return objectStream.ToArray();
        }
        catch (Exception ex)
        {
            ;

        }

        return null;
    }

   //Here are classes which serialize and deserialize.

    [Serializable()]
    public class player :ISerializable
   {
       public byte[] temp;
       public int id;
       public bool isConnected;
       public cube[] receivedCubes;
       public cube[] cubesToSend;
       public string name;
       public bool isLoaded = false;
       public List<index>[] indexes;         // list of field indexes

       public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
       {
           info.AddValue("receivedCubes", receivedCubes);
           info.AddValue("name", name);
           info.AddValue("temp", temp);
           info.AddValue("isConnected", isConnected);
           info.AddValue("isLoaded", isLoaded);
           info.AddValue("cubesToSend", cubesToSend);
           info.AddValue("indexes", indexes);
           info.AddValue("id", id);

       }
       public player()
       { }

       public player(SerializationInfo info, StreamingContext ctxt)
       {
           receivedCubes = (cube[])info.GetValue("receivedCubes", typeof(cube[]));
           name = (String)info.GetValue("name", typeof(string));
           temp = (byte[])info.GetValue("temp", typeof(byte[]));
           isConnected = (bool)info.GetValue("isConnected", typeof(bool));
           isLoaded = (bool)info.GetValue("isLoaded", typeof(bool));
           cubesToSend = (cube[])info.GetValue("cubesToSend", typeof(cube[]));
           indexes = (List<index>[])info.GetValue("indexes", typeof(List<index>[]));
           id = (int)info.GetValue("id", typeof(int));

       }



   }

    [Serializable()]
    public class index   //object idex indicate where cube entry on the screen from other player
    {
        public int x;       
        public int y;
        public int indexOfTargetCube;     // that mark own of index( cube)
        public byte direct;
        public index()
        {
        }

          public index (SerializationInfo info, StreamingContext ctxt)
       {

           x = (int)info.GetValue("x", typeof(int));
           y = (int)info.GetValue("y", typeof(int));
           indexOfTargetCube = (int)info.GetValue("temp", typeof(int));
           direct=(byte)info.GetValue("isConnected",typeof(byte));


       }
          public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("x", x);
            info.AddValue("y", y);
            info.AddValue("indexOfTargetCube", indexOfTargetCube);
            info.AddValue("direct", direct);


        }

    }


[Serializable()]
public class cube :ISerializable
{
    public int x;  // position x
    public int y;   // position y
    public int size;  // size of cube
    public byte direct=2;  // direct , default is rigtUp=2;
    public  byte speed;    // speed cube
    public bool live;      // isLive ???
    public int room = 0;   // where cube is
    public bool isOut;     // idicate for dealer, that is cube out from players
    public bool isMarked = false; // indicate that cube have index
    public byte numOfExit = 0;    // num. of exit for dealer
    public int exploseTimer = 0;   // timer for explose
    public byte sizeTimer;          // timer for size cube
    public byte speedTimer=0;      // timer for speed cube
    public byte stopTimer=0;      // timer for stop cube
    public byte lastDirect=0;      // last direct, if cube keep position
    public int assumeX = 0;        // for index calculate
    public int assumeY=0;          // for index calculate

    public cube()
    {

    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("x", x);
        info.AddValue("y", y);
        info.AddValue("size", size);
        info.AddValue("direct", direct);
        info.AddValue("speed", speed);
        info.AddValue("live", live);
        info.AddValue("room", room);
        info.AddValue("isOut", isOut);
        info.AddValue("isMarked", isMarked);
        info.AddValue("numOfExit", numOfExit);
        info.AddValue("exploseTimer", exploseTimer);
        info.AddValue("sizeTimer", sizeTimer);
        info.AddValue("speedTimer", speedTimer);
        info.AddValue("stopTimer", stopTimer);
        info.AddValue("lastDirect", lastDirect);
        info.AddValue("assumeY", assumeY);
        info.AddValue("assumeY", assumeY);

    }


       public cube(SerializationInfo info, StreamingContext ctxt)
       {
           x=(int)info.GetValue("x", typeof(int));
           y=(int)info.GetValue("y", typeof(int));
           size=(int)info.GetValue("size", typeof(int));
           direct=(byte)info.GetValue("direct",typeof(byte));
           speed=(byte)info.GetValue("speed", typeof(byte));
           live=(bool)info.GetValue("live", typeof(bool));
           room=(int)info.GetValue("room", typeof(int));
           isOut=(bool)info.GetValue("isOut", typeof(bool));
           isMarked=(bool)info.GetValue("isMarked",typeof(bool));
           numOfExit=(byte)info.GetValue("numOfExit",typeof(byte));
           exploseTimer=(int)info.GetValue("exploseTimer",typeof(int));
           sizeTimer=(byte)info.GetValue("sizeTimer",typeof(byte));
           speedTimer=(byte)info.GetValue("speedTimer",typeof(byte));
           stopTimer=(byte)info.GetValue("stopTimer",typeof(byte));
           lastDirect=(byte)info.GetValue("lastDirect",typeof(byte));
           assumeX=(int)info.GetValue("assumeY",typeof(int));
           assumeY=(int)info.GetValue("assumeY",typeof(int));

       }
 }

1 个答案:

答案 0 :(得分:0)

解决这两个错别字,然后它对我来说很好用:

你的意思是'assumeX',它试图两次添加相同的密钥:

info.AddValue("assumeY", assumeY);

在这两个地方使用assumeX。交叉变量不会抛出,但会有错误的数据:

assumeX=(int)info.GetValue("assumeY",typeof(int));