Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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);
        }