我在序列化和反序列化我创建的自定义对象时遇到问题。我不太确定为什么会发生错误,我在代码中没有看到任何可能直接导致此问题的内容。我在没有继承接口(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()
而不是 StreamReader
和 StreamWriter
。这些用于不是您正在使用的文本。
formatter.Serialize(ms, test);
return Convert.ToBase64String(ms.ToArray());
初始化内存流以进行反序列化:
using (var ms = new MemoryStream(Convert.FromBase64String(b64str))
...
关于c# - 反序列化错误: End of Stream encountered before parsing was completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600526/