Пример #1
0
 private void EmitBeginDeserialization(BoundedStream stream, ValueNode child,
                                       LazyBinarySerializationContext lazyContext,
                                       EventShuttle eventShuttle)
 {
     if (eventShuttle != null && eventShuttle.HasDeserializationSubscribers)
     {
         eventShuttle.OnMemberDeserializing(this, child.Name, lazyContext,
                                            stream.GlobalPosition, stream.RelativePosition);
     }
 }
        protected virtual void ObjectDeserializeOverride(BoundedStream stream, EventShuttle eventShuttle)
        {
            // check to see if we are actually supposed to be a custom deserialization.  This is a side-effect of
            // treating all object members as object nodes.  In the case of sub-types we could later discover we
            // are actually a custom node because the specified subtype implements IBinarySerializable.
            var parent = (TypeNode)TypeNode.Parent;

            if (_valueType != null && (TypeNode.SubtypeBinding != null || parent.ItemSubtypeBinding != null))
            {
                var typeNode = (ObjectTypeNode)TypeNode;
                var subType  = typeNode.GetSubTypeNode(_valueType);

                if (subType is CustomTypeNode)
                {
                    var customValueNode = subType.CreateSerializer((ValueNode)Parent);
                    customValueNode.DeserializeOverride(stream, eventShuttle);

                    // this is a cheat, but another side-effect of this weird corner case
                    _cachedValue = customValueNode.Value;
                    return;
                }
            }

            var lazyContext = CreateLazySerializationContext();

            foreach (var child in GetSerializableChildren())
            {
                // report on deserialization start if subscribed
                if (eventShuttle != null && eventShuttle.HasDeserializationSubscribers)
                {
                    eventShuttle.OnMemberDeserializing(this, child.Name, lazyContext,
                                                       stream.GlobalPosition);
                }

                // deserialize child
                child.Deserialize(stream, eventShuttle);

                // report on deserialization complete if subscribed
                if (eventShuttle != null && eventShuttle.HasDeserializationSubscribers)
                {
                    eventShuttle.OnMemberDeserialized(this, child.Name, child.Value, lazyContext,
                                                      stream.GlobalPosition);
                }
            }
        }
        public override void DeserializeOverride(StreamLimiter stream, EventShuttle eventShuttle)
        {
            if (TypeNode.SubtypeBinding == null)
            {
                _valueType = TypeNode.Type;
            }
            else
            {
                var subTypeValue = TypeNode.SubtypeBinding.GetValue(this);

                var matchingAttribute =
                    TypeNode.SubtypeAttributes.SingleOrDefault(
                        attribute =>
                        subTypeValue.Equals(Convert.ChangeType(attribute.Value, subTypeValue.GetType(), null)));

                _valueType = matchingAttribute == null ? null : matchingAttribute.Subtype;
            }

            if (_valueType == null)
            {
                return;
            }

            var typeNode = (ObjectTypeNode)TypeNode;

            var typeChildren = typeNode.GetTypeChildren(_valueType);

            Children = new List <ValueNode>(typeChildren.Select(child => child.CreateSerializer(this)));

            var context = CreateSerializationContext();

            var serializableChildren = GetSerializableChildren().ToArray();

            var fixedSizeRemaining = serializableChildren.Sum(node => node.GetFixedSize());

            foreach (var child in serializableChildren)
            {
                if (eventShuttle != null)
                {
                    eventShuttle.OnMemberDeserializing(this, child.Name, context);
                }

                fixedSizeRemaining -= child.GetFixedSize();
                var limitedStream = stream;
                if (child.TypeNode.FieldOffsetBinding == null && fixedSizeRemaining > 0)
                {
                    limitedStream = new StreamLimiter(stream, stream.Remainder - fixedSizeRemaining);
                }

                if (ShouldTerminate(limitedStream))
                {
                    break;
                }

                child.Deserialize(limitedStream, eventShuttle);

                if (eventShuttle != null)
                {
                    eventShuttle.OnMemberDeserialized(this, child.Name, child.Value, context);
                }
            }
        }