/// <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); }
/// <summary> /// Serialize any object to a stream, will write a type manifest as well /// </summary> public static void Serialize <T>(BinaryWriter bw, T obj) where T : class, new() { if (obj == null) { throw new ArgumentNullException("object being serialized can not be null"); } Type type = obj.GetType(); bw.Write(type.FullName); long startPos = bw.BaseStream.Position; MethodInfo method; // Build in versioning data here... if (Persistable.TryGetBinaryWrite(type, out method)) { if (method.IsStatic) { method.Invoke(null, new object[] { bw, obj }); } else { method.Invoke(bw, new object[] { obj }); } } else { if (typeof(T) == type) { GenericSerializer <T> .Serialize(obj, bw); } else { // slower Serializer.GetSerializer(type).SerializeInternal(obj, bw); } } // write the length so we can validate. if (!SkipValidatingSize(type)) { bw.Write(bw.BaseStream.Position - startPos); } }
/// <summary> /// Create an instance of the passed object /// </summary> /// <param name="type"></param> /// <returns></returns> public static T Instantiate <T>(string aType) where T : class, new() { Type type = GetCachedType(aType); if (type == null) { return(null); //old type or from plugin that is not installed } T instance; MethodInfo method; if (Persistable.TryGetBinaryRead(type, out method)) { if (method.IsStatic) { instance = (T)method.Invoke(null, new object[] { aType }); } else { instance = (T)method.Invoke(aType, null); } } else { if (typeof(T) == type) { instance = GenericSerializer <T> .Instantiate(aType); } else { instance = (T)Serializer.GetSerializer(type).InstantiateInternal(aType); } } return(instance); }