static FixedByteArray SerializeToFixedByteArray(object o, ISurrogateSelector selector) { if (o == null) { return(new FixedByteArray(new byte[0], 0)); } FixedByteArray array = new FixedByteArray(); BinaryFormatter formatter = new BinaryFormatter(); if (selector != null) { formatter.SurrogateSelector = selector; } MemoryStream s = new MemoryStream(); formatter.Serialize(s, o); array.Length = (int)s.Length; s.Close(); array.Data = s.GetBuffer(); return(array); }
/// <summary> /// Utility method for serializing objects into a base64 format. /// <seealso cref="DeserializeBase64(string, ISurrogateSelector)"/>. Null object references will /// be serialized correctly without causing an exception. /// </summary> static public string SerializeBase64(object o, ISurrogateSelector selector) { if (o == null) { return(NullBase64); } // convert to bytes, without extra padding at the end FixedByteArray array = SerializeToFixedByteArray(o, selector); // convert bytes to text return(Convert.ToBase64String(array.Data, 0, array.Length)); }
static FixedByteArray SerializeToFixedByteArray(object o, ISurrogateSelector selector) { if (o == null) return new FixedByteArray(new byte[0], 0); FixedByteArray array = new FixedByteArray(); BinaryFormatter formatter = new BinaryFormatter(); if (selector != null) formatter.SurrogateSelector = selector; MemoryStream s = new MemoryStream(); formatter.Serialize(s, o); array.Length = (int)s.Length; s.Close(); array.Data = s.GetBuffer(); return array; }