首页 > 代码库 > 序列化和反序列化

序列化和反序列化

参考使用 类:

 

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary



class
ConfigFileFactory { public static void SaveConfigurature() { IFormatter formatter = new BinaryFormatter(); if (!Directory.Exists(FilePath.Instance.XmlConfigFilePath)) { Directory.CreateDirectory(FilePath.Instance.XmlConfigFilePath); //文件路径
} Stream stream
= new FileStream(FilePath.Instance.XmlFullFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); formatter.Serialize(stream, XXXXCLASS); //被序列化的类 stream.Close(); } public static SerializableSettingViewModel GetConfiguration() { Stream stream2 = new FileStream(FilePath.Instance.XmlFullFileName, FileMode.Open, FileAccess.Read, FileShare.None); IFormatter formatter = new BinaryFormatter(); XXXXCLASS viewModel = (XXXXCLASS)formatter.Deserialize(stream2); //反序列化的类 stream2.Close(); return viewModel; } }

其中 类中要标记为 可序列化 [Serializable]

序列化和反序列化