流的末尾遇到解析完成之前?末尾

2023-09-02 02:01:20 作者:我很好

我想反序列化一个流,但我总​​是得到这个错误流的末尾遇到过分析完成?

I am trying to deserialize a stream but I always get this error "End of Stream encountered before parsing was completed"?

下面是code:

        //Some code here
        BinaryFormatter b = new BinaryFormatter();
        return (myObject)b.Deserialize(s);//s---> is a Stream object that has been fill up with data some line over here

任何一个有想法?

Any one have ideas?

推荐答案

尝试将位置设置为0的数据流,并且不使用你的对象,但对象类型。

Try to set the position to 0 of your stream and do not use your object but the object type.

        BinaryFormatter b = new BinaryFormatter();
        s.Position = 0;
        return (YourObjectType)b.Deserialize(s);