//Serialize any class that has inherited from Serializable public void Serialize(ISerializable value) { IsWriting = true; int length = 0; if (value == null) { byteList.AddRange(BitConverter.GetBytes(length)); return; } //Convert the class to bytes byte [] data = value.ToBytes(); //Add the length of the serializable type to the stream length = data.Length; byteList.AddRange(BitConverter.GetBytes(length)); //Add the data to the byte list byteList.AddRange(data); }
public static bool SaveToLocation(ISerializable data, string location, string fileNameAndExtension) { System.IO.Directory.CreateDirectory(location); using (System.IO.FileStream _FileStream = new System.IO.FileStream(location + "/" + fileNameAndExtension, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { try { byte[] dataBytes = data.ToBytes(); _FileStream.Write(dataBytes, 0, dataBytes.Length); _FileStream.Close(); return(true); } catch (Exception _Exception) { Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); return(false); } } }