예제 #1
0
        public T Deserialize <T>(byte[] bytes)
        {
            var stream = new DeserializationStream(bytes);

            SerializedType deserializedType = stream.ReadSerializedType();

            if (deserializedType == SerializedType.Null)
            {
                return(default);
예제 #2
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);
            }
        }