我试图将一个对象序列化为一个二进制文件。我使用BinaryFormatter.Serialize来序列化对象,但是当我试图调用它时,流参数“意外符号‘(’在类中、结构中或接口成员声明中‘)会出现一个解析器错误。
这是我的密码:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class Serializer{
Properties prop = new Properties ();
IFormatter f = new BinaryFormatter();
Stream s = new FileStream("Properties/prop.bin", FileMode.Create, FileAccess.Write, FileShare.None);
f.Serialize(s, prop);
s.Close();
}错误发生在:
f.Serialize(s, prop); //the error is on the 's'我在这里也有同样的错误:
s.Close();我如何纠正这些错误?
下面是我正在序列化的内容:
public class Properties{
public string y = "2";
public string x = "4";
}发布于 2016-02-11 22:07:00
您的代码需要在函数中。
public class Serializer{
public void Seralize()
{
Properties prop = new Properties ();
IFormatter f = new BinaryFormatter();
Stream s = new FileStream("Properties/prop.bin", FileMode.Create, FileAccess.Write, FileShare.None);
f.Serialize(s, prop);
s.Close();
}
}但是,我建议您避免使用BinaryFormatter程序集版本更改可以很容易地破坏您的文件,而是使用XML或其他二进制格式化程序。
https://stackoverflow.com/questions/35350898
复制相似问题