gpt4 book ai didi

c# - 反序列化错误 : End of Stream encountered before parsing was completed

转载 作者:行者123 更新时间:2023-11-30 23:24:44 24 4
gpt4 key购买 nike

我在序列化和反序列化我创建的自定义对象时遇到问题。我不太确定为什么会发生错误,我在我的代码中没有看到任何可能直接导致此问题的内容。我在没有继承接口(interface)的情况下测试了它,它仍然会抛出错误。

接口(interface):

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;
}

最佳答案

您不能只将输出视为字符串;如果它包含 NUL (Chr(0)),则字符串将在此时被截断。使用 Convert.ToBase64String() 而不是 StreamReaderStreamWriter。这些用于不是您正在使用的文本。

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

为反序列化初始化 memstream:

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

关于c# - 反序列化错误 : End of Stream encountered before parsing was completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com