public class SerializeHelper { public static bool Serialize(Object data, string fileName) { try { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, data); } return true; } catch (Exception e) { return false; } } public static bool Deserialize(string fileName, ref object o) { try { if (System.IO.File.Exists(fileName)) { using (FileStream fs = new FileStream(fileName, FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); o = bf.Deserialize(fs); } } return true; } catch (Exception e) { return false; } } }
var a = SerializeHelper.Serialize(tokenResponse, "token.bin"); object o = new object(); ; var b = SerializeHelper.Deserialize("token.bin", ref o); TokenResponseModel t = o as TokenResponseModel;
原文:http://www.cnblogs.com/tgdjw/p/5079389.html