Esempio n. 1
0
        private static TObject BytesToObject(byte[] data)
        {
            var inObject = new TObject();

            //Get the header and set its data
            var type   = SerializationHelper.GetFieldOrPropertyType(FieldSizeProvider);
            var header = Miscellaneous.BytesToStruct(type, data, 0);

            SerializationHelper.SetValue(FieldSizeProvider, inObject, header);

            //Get the payload
            int size    = data.Length - Marshal.SizeOf(type);
            var payload = new byte[size];

            Array.ConstrainedCopy(data, data.Length - size, payload, 0, size);

            int offset = 0;

            foreach (var valueOrdinal in FieldValueOrdinals)
            {
                MemberInfo sizeOrdinal;

                if (!FieldSizeOrdinals.TryGetValue(valueOrdinal.Key, out sizeOrdinal))
                {
                    throw new V4ObjectSerializationException("Failed to deserialize object.");
                }

                var length     = Convert.ToInt32(SerializationHelper.GetValue(sizeOrdinal, header));
                var targetType = SerializationHelper.GetFieldOrPropertyType(valueOrdinal.Value);


                if (targetType == null)
                {
                    throw new V4ObjectSerializationException("Failed to deserialize object.");
                }



                if (targetType == typeof(string))
                {
                    var asString = Encoding.UTF8.GetString(payload, offset, length);
                    SerializationHelper.SetValue(valueOrdinal.Value, inObject, asString);
                }
                else if (targetType.GetInterface("ICollection") != null)
                {
                    var genericArguments = targetType.GetGenericArguments();
                    if (genericArguments.Length != 1)
                    {
                        throw new V4ObjectSerializationException("Failed to deserialize object.");
                    }



                    var structCollection = Activator.CreateInstance(targetType) as IList;
                    if (structCollection == null)
                    {
                        throw new V4ObjectSerializationException("Failed to deserialize object.");
                    }



                    var structType     = genericArguments[0];
                    int structSize     = Marshal.SizeOf(structType) + 1;
                    int totalStructs   = length / structSize;
                    int structPosition = offset;

                    for (int i = 0; i < totalStructs; ++i)
                    {
                        structCollection.Add(Miscellaneous.BytesToStruct(structType, payload, structPosition));
                        structPosition += structSize;
                    }

                    SerializationHelper.SetValue(valueOrdinal.Value, inObject, structCollection);
                }

                offset += length;
            }

            return(inObject);
        }