public static MemoryStream SerializeToStream(object value, byte useCompression, bool altSerialization = USE_ALT) { using (var memory = new MemoryStream()) { if (altSerialization) { memory.WriteByte((byte)'a'); memory.WriteByte(useCompression); } else { if (useCompression > 0) { memory.WriteByte((byte)'c'); memory.WriteByte(useCompression); } else { memory.WriteByte((byte)'b'); } } Stream gzip = null; switch (useCompression) { case 0: gzip = memory; break; case 1: gzip = new GZipStream(memory, CompressionMode.Compress); break; case 2: gzip = new DeflateStream(memory, CompressionMode.Compress); break; //case 3: gzip = new SevenZip.LzmaEncodeStream(memory); break; } using (gzip) { if (altSerialization) #if !SAFE_COPY { lock (AltSerialization.Locker) #endif AltSerialization.Serialize(gzip, value); } else { var bi = new BinaryFormatter(); bi.Serialize(gzip, value); } } return(memory); } }
void SerializeValueFields(object value, AltSerialization serialization, IntPtr ptr) { if (BaseType != null) { BaseType.SerializeValueFields(value, serialization, ptr); } #if SAFE_COPY foreach (var field in ValueFields) { serialization.Serialize(field.GetValue(value), field.FieldType.IsSealed ? field.FieldType : null); } #else if (valueFieldsSize > 0) { //lock (locker) Marshal.Copy(ptr + valueFieldsStart, data, 0, valueFieldsSize); serialization.Writer.Write(data, 0, valueFieldsSize); } #endif }