public static void _Serialize(string filename, int?ver, object[] objs) { string lockname = "Serializer: " + filename.Replace("\\", "@"); using (new NamedLock(lockname)) { Stream stream = HFile.Open(filename, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); { if (ver != null) { bFormatter.Serialize(stream, new Ver(ver.Value)); } } { System.Int32 count = objs.Length; bFormatter.Serialize(stream, count); for (int i = 0; i < count; i++) { bFormatter.Serialize(stream, objs[i]); } } stream.Flush(); stream.Close(); } }
public static bool _Deserialize(string filename, int?ver, out object[] objs) { string lockname = "Serializer: " + filename.Replace("\\", "@"); using (new NamedLock(lockname)) { Stream stream = HFile.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter bFormatter = new BinaryFormatter(); { if (ver != null) { try { Ver sver = (Ver)bFormatter.Deserialize(stream); if (sver.ver != ver.Value) { stream.Close(); objs = null; return(false); } } catch (Exception) { stream.Close(); objs = null; return(false); } } } { System.Int32 count = (System.Int32)bFormatter.Deserialize(stream); objs = new object[count]; for (int i = 0; i < count; i++) { objs[i] = bFormatter.Deserialize(stream); } } stream.Close(); } return(true); }