コード例 #1
0
        private void DeserializeProperty <T>(PropertyInfo property, ref T instance, byte[] stream, ref int offset)
        {
            Type           instanceType     = property.PropertyType;
            TypeInfo       instanceTypeInfo = instanceType.GetTypeInfo();
            SerializedType type             = stream.ReadSerializedType(ref offset);

            if (!excludedDlls.Any(x => property.PropertyType.AssemblyQualifiedName.Contains(x)))
            {
                if (type == SerializedType.Null)
                {
                    property.SetValue(instance, null);
                }
                else
                {
                    object propertyValue = Activator.CreateInstance(property.PropertyType);
                    property.SetValue(instance, propertyValue);
                    DeserializeObject(stream, ref propertyValue, ref offset);
                }
                return;
            }

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

            if (!property.PropertyType.GetTypeInfo().IsBaseType())
            {
                int typeInfoSize = BitConverter.ToInt32(stream, offset);
                offset += sizeof(int);
                offset += typeInfoSize;
            }

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

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

                var prop         = property;
                var listType     = typeof(List <>);
                var genericArgs  = prop.PropertyType.GenericTypeArguments;
                var concreteType = listType.MakeGenericType(genericArgs);
                data = Activator.CreateInstance(concreteType);
                foreach (var item in preparedData)
                {
                    ((IList)data).Add(item);
                }
            }
            else
            {
                data = converter.DeserializeToObject(stream, ref offset);
            }

            if (instanceTypeInfo.IsValueType && !instanceTypeInfo.IsPrimitive)
            {
                object boxedInstance = instance;
                property.SetValue(boxedInstance, data, property.GetIndexParameters());
                instance = (T)boxedInstance;
            }
            else
            {
                property.SetValue(instance, data, property.GetIndexParameters());
            }
        }
コード例 #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);
            }
        }