C#序列化和反序列化
程序员文章站
2022-06-16 08:25:30
...
C#序列化和反序列化
需要在前面加[Serializable],如:[Serializable] public class Class{}
序列化:
FileStream fileStream = new FileStream(文件路径, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, 对象);
fileStream.Close();
反序列化:
FileStream fileStream = new FileStream(文件路径, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
对象=b.Deserialize(fileStream);
fileStream.Close();