Exemple #1
0
        public T Deserialize(byte[] bytes)
        {
            var            stream           = new WorkingStream(bytes);
            SerializedType deserializedType = stream.ReadSerializedType();
            Type           sourceType       = deserializedType.GetBaseType();

            if (sourceType == null)
            {
                byte[] typeInfo = stream.ReadBytesWithSizePrefix();
                sourceType = TypeUtils.FromUTF8Bytes(typeInfo);
            }

            int offset = stream.Offset;

            return(ProcessDeserialize(bytes, sourceType, ref offset));
        }
        protected override object ProcessDeserialize(byte[] bytes, Type sourceType, ref int offset)
        {
            var stream = new WorkingStream(bytes);

            stream.ChangeOffset(offset);

            int  beforeOffset   = stream.Offset;
            Type collectionType = sourceType;

            if (collectionType == typeof(object))
            {
                collectionType = typeof(List <object>);
            }

            var         deserializedCollection             = Activator.CreateInstance(collectionType);
            bool        isDictionary                       = TypeHelper.IsDictionary(deserializedCollection);
            bool        isLinkedList                       = TypeHelper.IsLinkedList(deserializedCollection);
            IList       deserializedCollectionAsList       = null;
            IDictionary deserializedCollectionAsDictionary = null;

            if (isDictionary)
            {
                deserializedCollectionAsDictionary = (IDictionary)deserializedCollection;
            }
            else
            {
                if (isLinkedList)
                {
                    Type listType = typeof(List <>).MakeGenericType(sourceType.GenericTypeArguments);
                    deserializedCollectionAsList = (IList)Activator.CreateInstance(listType);
                }
                else
                {
                    deserializedCollectionAsList = (IList)deserializedCollection;
                }
            }

            if (bytes.Length > 0)
            {
                BinaryConverter converter      = new BinaryConverter();
                int             sizeCollection = stream.ReadInt();

                for (int i = 0; i < sizeCollection; i++)
                {
                    int sizeData = stream.ReadInt();
                    if (sizeData == 0)
                    {
                        continue;
                    }
                    byte[] dataValue = stream.ReadBytes(sizeData);

                    MethodInfo method = typeof(BinaryConverter).GetRuntimeMethod("Deserialize", new[] { typeof(byte[]) });
                    if (sourceType.GenericTypeArguments.Length > 0)
                    {
                        if (isDictionary)
                        {
                            Type elementType = typeof(KeyValuePair <,>).MakeGenericType(sourceType.GenericTypeArguments);
                            method = method.MakeGenericMethod(new System.Type[] { elementType });
                        }
                        else
                        {
                            method = method.MakeGenericMethod(sourceType.GenericTypeArguments);
                        }
                    }
                    else
                    {
                        method = method.MakeGenericMethod(typeof(object));
                    }
                    var deserializeItem = method.Invoke(converter, new object[] { dataValue });

                    if (isDictionary)
                    {
                        KeyValuePair <object, object> keyValuePairFormObject = TypeHelper.CastFrom(deserializeItem);
                        deserializedCollectionAsDictionary.Add(keyValuePairFormObject.Key, keyValuePairFormObject.Value);
                    }
                    else
                    {
                        deserializedCollectionAsList.Add(deserializeItem);
                    }
                }
            }

            Size = stream.Offset - beforeOffset;
            stream.ChangeOffset(beforeOffset);

            if (isLinkedList)
            {
                deserializedCollection = Activator.CreateInstance(collectionType, new object[] { deserializedCollectionAsList });
            }

            return(deserializedCollection);
        }