反序列化错误:解析完成之前遇到的流结束

时间:2016-06-02 19:26:44

标签: c# serialization binary

我在编译和反序列化我创建的自定义对象时遇到问题。我不太确定错误发生的原因,我在代码中看不到可能直接导致此问题的任何内容。我没有继承接口就对它进行了测试,它仍然会抛出错误。

接口:

public interface ITest
{
    void LoadTest(string id, string className, string classMethod, bool isNegative, List<string> args, string expectedReturn);

    string GetId();
    string GetClassName();
    string GetMethod();
    List<string> GetArgs();
    string GetExpectedReturn();
    bool IsNegative();

    bool HasRan();
    void RunTest(...);

    string GetTestReturn();
    bool GetTestResult();
}

有序列化问题的类:

[Serializable]
public class TestXML : ITest
{
    private string _id;
    private string _className;
    private string _classMethod;
    private bool _isNegative;
    private List<string> _args;
    private string _expectedReturn;

    private bool _hasRan;

    private string _actualReturn;

    private bool _passOrFail;

    public static string ServerEncodeStatic(TestXML test)
    {
        IFormatter formatter = new BinaryFormatter();

        var ms = new MemoryStream();
        var sr = new StreamReader(ms);

        formatter.Serialize(ms, test);

        ms.Position = 0;

        //TestXML t = (TestXML)formatter.Deserialize(ms); // ERROR occurs here as well if this is uncommented

        var toret = sr.ReadToEnd();

        return toret;
    }

    public static TestXML ServerDecodeStatic(string data)
    {
        IFormatter formatter = new BinaryFormatter();
        using (var ms = new MemoryStream())
        {
            using (var sw = new StreamWriter(ms))
            {
                sw.Write(data);
                sw.Flush();

                ms.Position = 0;
                ms.Seek(0, SeekOrigin.Begin);

                TestXML t = (TestXML)formatter.Deserialize(ms); // ERROR occurs here

                return (TestXML)formatter.Deserialize(ms);
            }
        }
    }

    public void LoadTest(string id, string className, string classMethod, bool isNegative, List<string> args, string expectedReturn)
    {
        _id = id;
        _className = className;
        _classMethod = classMethod;
        _isNegative = isNegative;
        _args = args;
        _expectedReturn = expectedReturn;
    }


    public string GetId()
    {
        return _id;
    }

    public string GetClassName()
    {
        return _className;
    }

    public string GetMethod()
    {
        return _classMethod;
    }

    public List<string> GetArgs()
    {
        return _args;
    }

    public string GetExpectedReturn()
    {
        return _expectedReturn;
    }

    public bool IsNegative()
    {
        return _isNegative;
    }


    public bool HasRan()
    {
        return _hasRan;
    }

    public void RunTest(...)
    {
        ...
    }


    public string GetTestReturn()
    {
        return _actualReturn;
    }

    public bool GetTestResult()
    {
        return _passOrFail;
    }
}

编辑: 我已经重写了这两个函数,现在它们正在工作!:

    public static string ServerEncodeStatic(TestXML test)
    {
        IFormatter formatter = new BinaryFormatter();
        var ms = new MemoryStream();

        formatter.Serialize(ms, test);

        return Convert.ToBase64String(ms.ToArray());
    }

    public static TestXML ServerDecodeStatic(string data)
    {
        IFormatter formatter = new BinaryFormatter();
        var ms = new MemoryStream(Convert.FromBase64String(data));

        var toret = (TestXML)formatter.Deserialize(ms);

        return toret;
    }

1 个答案:

答案 0 :(得分:2)

你不能把输出视为字符串;如果它包含NUL(Chr(0)),则该字符串将在该点被截断。使用Convert.ToBase64String()代替StreamReaderStreamWriter。那些不是你正在使用的文本。

formatter.Serialize(ms, test);
return Convert.ToBase64String(ms.ToArray());

初始化用于反序列化的memstream:

using (var ms = new MemoryStream(Convert.FromBase64String(b64str))
    ...