Ejemplo n.º 1
0
        private void DeserializeEnum(DeserializationStream stream, ref object propertyValue)
        {
            Type type      = stream.ReadType();
            var  converter = new EnumConverter();

            propertyValue = converter.DeserializeInto(stream, type);
        }
Ejemplo n.º 2
0
        private void DeserializeObject <T>(DeserializationStream stream, ref T instance)
        {
            stream.ReadType();

            foreach (var field in instance.GetType().GetFieldsAccessibleForSerializer())
            {
                DeserializeField(field, ref instance, stream);
                if (stream.HasEnded)
                {
                    return;
                }
            }
        }
Ejemplo n.º 3
0
        public void TypeCanReaded(string typeName, Type expectedType)
        {
            // arrange
            byte[] typeInfo  = Encoding.UTF8.GetBytes(typeName);
            byte[] sizeBytes = BitConverter.GetBytes(typeInfo.Length);
            byte[] data      = new byte[sizeBytes.Length + typeInfo.Length];
            Array.Copy(sizeBytes, 0, data, 0, sizeBytes.Length);
            Array.Copy(typeInfo, 0, data, sizeBytes.Length, typeInfo.Length);

            // Act
            var  stream = new DeserializationStream(data);
            Type type   = stream.ReadType();

            // Assert
            type.Should().Be(expectedType);
            stream.Offset.Should().Be(data.Length);
        }
Ejemplo n.º 4
0
        private void DeserializeField <T>(FieldInfo field, ref T instance, DeserializationStream stream)
        {
            Type           instanceType     = field.FieldType;
            TypeInfo       instanceTypeInfo = instanceType.GetTypeInfo();
            SerializedType type             = stream.ReadSerializedType();

            if (!ExcludedDlls.Any(x => field.FieldType.AssemblyQualifiedName.Contains(x)))
            {
                if (type == SerializedType.Null)
                {
                    field.SetValue(instance, null);
                }
                else if (type == SerializedType.Enum)
                {
                    object propertyValue = Activator.CreateInstance(field.FieldType);
                    DeserializeEnum(stream, ref propertyValue);
                    field.SetValue(instance, propertyValue);
                }
                else
                {
                    object propertyValue = Activator.CreateInstance(field.FieldType);
                    DeserializeObject(stream, ref propertyValue);
                    field.SetValue(instance, propertyValue);
                }
                return;
            }

            if (type == SerializedType.Null)
            {
                field.SetValue(instance, null);
                return;
            }

            if (!field.FieldType.IsBaseTypeSupportedBySerializer())
            {
                stream.ReadType();
            }

            BaseTypeConverter converter = ConvertersSelector.ForSerializedType(type);
            object            data;

            if (type == SerializedType.Null)
            {
                data = null;
            }
            else if (type == SerializedType.IEnumerable)
            {
                var preparedData = converter.Deserialize(stream) as IEnumerable;

                var prop         = field;
                var listType     = typeof(List <>);
                var genericArgs  = prop.FieldType.GenericTypeArguments;
                var concreteType = listType.MakeGenericType(genericArgs);
                data = Activator.CreateInstance(concreteType);
                foreach (var item in preparedData)
                {
                    ((IList)data).Add(item);
                }
            }
            else
            {
                data = converter.Deserialize(stream);
            }

            if (instanceTypeInfo.IsValueType && !instanceTypeInfo.IsPrimitive)
            {
                object boxedInstance = instance;
                field.SetValue(boxedInstance, data);
                instance = (T)boxedInstance;
            }
            else
            {
                field.SetValue(instance, data);
            }
        }