public static T Deserialise <T>(FileStream fs, ESerialisationFlags format) { Stream fsu; switch (format & ESerialisationFlags._CompressMask) { case ESerialisationFlags.GZip: fsu = new GZipStream(fs, CompressionMode.Decompress); break; case ESerialisationFlags.NoCompression: fsu = fs; break; default: throw new SwitchException(format); } try { switch (format & ESerialisationFlags._SerialiserMask) { case ESerialisationFlags.Binary: BinaryFormatter bf = new BinaryFormatter(); return((T)bf.Deserialize(fsu)); case ESerialisationFlags.Xml: XmlSerializer xs = new XmlSerializer(typeof(T)); return((T)xs.Deserialize(fsu)); case ESerialisationFlags.Ini: return(IniSerialiser.Deserialise <T>(fsu)); default: throw new SwitchException("format", format); } } finally { switch (format & ESerialisationFlags._CompressMask) { case ESerialisationFlags.GZip: fsu.Dispose(); break; case ESerialisationFlags.NoCompression: break; default: throw new SwitchException(format); } } }
private static T _Deserialise <T>(Stream s, SerialisationFormat format, ObjectSerialiser serialiser, ProgressReporter prog) { switch (format) { case SerialisationFormat.MSerialiserBinary: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.Binary, new[] { serialiser }, null)); case SerialisationFormat.MSerialiserText: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.Text, new[] { serialiser }, null)); case SerialisationFormat.MSerialiserCompactBinary: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.CompactBinary, new[] { serialiser }, null)); case SerialisationFormat.MSerialiserFastBinary: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.FastBinary, new[] { serialiser }, null)); case SerialisationFormat.Xml: var xs = _CreateXmlSerialiser <T>(); return((T)xs.Deserialize(s)); case SerialisationFormat.DataContract: var dcs = _CreateDataContactSerialiser <T>(); return((T)dcs.ReadObject(s)); case SerialisationFormat.MsnrbfBinary: var bcs = new BinaryFormatter(); // bcs.Binder = new TypeNameConverter(); - We don't do this anymore, backwards compatibility ends up taking most of the development time. return((T)bcs.Deserialize(s)); case SerialisationFormat.Ini: return(IniSerialiser.Deserialise <T>(s)); default: throw new InvalidOperationException("Invalid switch: " + format); } }