【发布时间】:2012-07-16 09:35:30
【问题描述】:
我得到了以下内容,而不是复杂的代码,无论如何我在反序列化时遇到异常。 例外情况是:二进制流“0”不包含有效的 BinaryHeader。可能的原因是无效的流或序列化和反序列化之间的对象版本更改。
但我不明白我的代码有什么问题
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
namespace Server
{
[Serializable]
class testclass
{
int a;
int b;
int c;
public testclass()
{
a = 1;
b = 2;
c = 3000;
}
}
class Program
{
static void Main(string[] args)
{
testclass test = new testclass();
IFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(new byte[512],0,512,true,true);
bf.Serialize(ms,test);
testclass detest=(testclass)bf.Deserialize(ms);
Console.ReadLine();
}
}
}
【问题讨论】:
标签: c# serialization binaryformatter