6

我正在尝试反序列化保存到文件中的对象(使用二进制格式化程序)。无论我尝试什么,我都会遇到异常:在解析完成之前遇到流结束

我有以下代码行:

public static T DeserializeFromBinaryFile<T>(string fileName)
{
    T instance = default(T);
    FileStream fs = new FileStream(fileName, FileMode.Open);
    try
    {
        BinaryFormatter formatter = new BinaryFormatter();
        instance = (T)formatter.Deserialize(fs);
    }
    catch{}
    finally
    {
        fs.Close();
    }

    return instance;
}

我也试过:

public static T DeserializeFromBinaryFile<T>(string fileName)
{
    T instance = default(T);
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    MemoryStream ms = new MemoryStream();
    try
    {
        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);
        ms.Write(bytes, 0, (int)fs.Length);
        ms.Position = 0;
        ms.Seek(0, SeekOrigin.Begin); 
    }
    catch { }

    try
    {
        BinaryFormatter formatter = new BinaryFormatter();
        instance = (T)formatter.Deserialize(ms);
    }
    catch { }
    finally
    {
        ms.Close();
        fs.Close();
    }

    return instance;
}

但无论我做什么,总是会遇到异常:

在解析完成之前遇到流结束

补充:我刚刚做了一些额外的测试。如果我有一个简单的对象,只有几个属性,它就可以正常工作。使用更(大)复杂的对象,封装其他对象、枚举等,这就是我得到异常的时候。

4

3 回答 3

1

尝试在第二个 try/catch 块内为您的流设置positionto :0

BinaryFormatter b = new BinaryFormatter();
s.Position = 0;
return (YourObjectType)b.Deserialize(s);
于 2013-04-02T13:49:10.217 回答
0

您是否尝试过反序列化文件流而不是内存流?这个对我有用。

FileStream fs = new FileStream(fileName, FileMode.Open);
fs.position=0;
instance = (T)formatter.Deserialize(fs);

问候

于 2014-06-11T14:46:25.743 回答
0

Every member within the class to be serialized, must have the [Serializable()] attribute or be marked [NonSerialized]. Note, enums and all simple built in types are already serializable and do not require the [Serializable()] attribute.

The example below gives the same error. Uncomment the [Serializable()] above public class B, and deserialization will occur without errors.

*It is not possible to say with certainty whether this is the cause of your error, because you did not provide a sample of the class resulting in the error.

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; 

//[Serializable()]
public class B
{
    public int y = 2;
}
[Serializable()]
public class A
{
    public int x = 2;
    /*[NonSerialized]*/ public B b;
    public static T DeserializeFromBinaryFile<T>(string fileName)
    {
        T instance = default(T);
        FileStream fs = new FileStream(fileName, FileMode.Open);
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();
            instance = (T)formatter.Deserialize(fs);
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            fs.Close();
        }

        return instance;
    }
    public static void SerializeBinaryFile<T>(string fileName,T t)
    {
        try
        {
            using (FileStream fs = File.Open(fileName, FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, t);
            }
        }
        catch (System.Exception ex)
        {

        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        A testA = new A();
        A.SerializeBinaryFile("test.dat", testA);
        A testReadA = A.DeserializeFromBinaryFile<A>("test.dat");
    }
}

https://msdn.microsoft.com/en-us/library/ms973893.aspx

于 2015-08-22T17:21:30.783 回答