/// <summary> /// Deserialize an object that was serialized using the Serialize method, /// has robustness to version changes /// </summary> /// <param name="stream"></param> /// <returns></returns> public static T Deserialize <T>(BinaryReader reader) where T : class, new() { Type type = GetCachedType(reader.ReadString()); T deserialized; MethodInfo method; // versioning goes here if (Persistable.TryGetBinaryRead(type, out method)) { if (method.IsStatic) { deserialized = (T)method.Invoke(null, new object[] { reader }); } else { deserialized = (T)method.Invoke(reader, null); } } else { if (typeof(T) == type) { deserialized = GenericSerializer <T> .Deserialize(reader); } else { deserialized = (T)Serializer.GetSerializer(type).DeserializeInternal(reader); } } return(deserialized); }
/// <summary> /// Deserialize an object that was serialized using the Serialize method, /// has robustness to version changes /// </summary> /// <param name="stream"></param> /// <returns></returns> public static T Deserialize <T>(BinaryReader reader) where T : class, new() { Type type = GetCachedType(reader.ReadString()); if (type == null) { return(null); //probably an old type or from plugin that is no longer installed } long startPos = reader.BaseStream.Position; T deserialized; MethodInfo method; // versioning goes here if (Persistable.TryGetBinaryRead(type, out method)) { if (method.IsStatic) { deserialized = (T)method.Invoke(null, new object[] { reader }); } else { deserialized = (T)method.Invoke(reader, null); } } else { if (typeof(T) == type) { deserialized = GenericSerializer <T> .Deserialize(reader); } else { deserialized = (T)Serializer.GetSerializer(type).DeserializeInternal(reader); } } if (!SkipValidatingSize(type)) { if ((reader.BaseStream.Position - startPos) != reader.ReadInt64()) { // its corrupt throw new SerializationException("Corrupt item in cache"); } } return(deserialized); }