コード例 #1
0
        /** <inheritdoc /> */
        T IBinarySerializerInternal.ReadBinary <T>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos)
        {
            Debug.Assert(_rActions != null);
            Debug.Assert(reader != null);
            Debug.Assert(desc != null);

            var obj = FormatterServices.GetUninitializedObject(desc.Type);

            var ctx = GetStreamingContext(reader);

            _serializableDescriptor.OnDeserializing(obj, ctx);

            DeserializationCallbackProcessor.Push(obj);

            try
            {
                reader.AddHandle(pos, obj);

                foreach (var action in _rActions)
                {
                    action(obj, reader);
                }

                _serializableDescriptor.OnDeserialized(obj, ctx);
            }
            finally
            {
                DeserializationCallbackProcessor.Pop();
            }

            return((T)obj);
        }
コード例 #2
0
        /** <inheritdoc /> */
        T IBinarySerializerInternal.ReadBinary <T>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos,
                                                   Type typeOverride)
        {
            Debug.Assert(_rActions != null);
            Debug.Assert(reader != null);
            Debug.Assert(desc != null);

            var obj = FormatterServices.GetUninitializedObject(typeOverride ?? desc.Type);

            var ctx = GetStreamingContext(reader);

            _serializableDescriptor.OnDeserializing(obj, ctx);

            DeserializationCallbackProcessor.Push(obj);

            try
            {
                reader.AddHandle(pos, obj);

                foreach (var action in _rActions)
                {
                    action(obj, reader);
                }

                _serializableDescriptor.OnDeserialized(obj, ctx);

                DeserializationCallbackProcessor.Pop();
            }
            catch (Exception)
            {
                // Clear callbacks on exception to avoid dangling objects.
                DeserializationCallbackProcessor.Clear();
                throw;
            }

            return((T)obj);
        }
コード例 #3
0
        /** <inheritdoc /> */
        public T ReadBinary <T>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos)
        {
            object res;
            var    ctx            = GetStreamingContext(reader);
            var    callbackPushed = false;

            // Read additional information from raw part, if flag is set.
            IEnumerable <string> fieldNames;
            Type customType = null;
            ICollection <int> dotNetFields = null;

            if (reader.GetCustomTypeDataFlag())
            {
                var oldPos = reader.Stream.Position;
                reader.SeekToRaw();

                fieldNames   = ReadFieldNames(reader, desc);
                customType   = ReadCustomTypeInfo(reader);
                dotNetFields = ReadDotNetFields(reader);

                // Restore stream position.
                reader.Stream.Seek(oldPos, SeekOrigin.Begin);
            }
            else
            {
                fieldNames = GetBinaryTypeFields(reader, desc);
            }

            try
            {
                if (customType != null)
                {
                    // Custom type is present, which returns original type via IObjectReference.
                    var serInfo = ReadSerializationInfo(reader, fieldNames, desc, dotNetFields);

                    res = ReadAsCustomType(customType, serInfo, ctx);

                    // Handle is added after entire object is deserialized,
                    // because handles should not point to a custom type wrapper.
                    reader.AddHandle(pos, res);

                    DeserializationCallbackProcessor.Push(res);
                    callbackPushed = true;
                }
                else
                {
                    res = FormatterServices.GetUninitializedObject(desc.Type);

                    _serializableTypeDesc.OnDeserializing(res, ctx);

                    DeserializationCallbackProcessor.Push(res);
                    callbackPushed = true;

                    reader.AddHandle(pos, res);

                    // Read actual data and call constructor.
                    var serInfo = ReadSerializationInfo(reader, fieldNames, desc, dotNetFields);
                    _serializableTypeDesc.SerializationCtorUninitialized(res, serInfo, ctx);
                }

                _serializableTypeDesc.OnDeserialized(res, ctx);
            }
            finally
            {
                if (callbackPushed)
                {
                    DeserializationCallbackProcessor.Pop();
                }
            }

            return((T)res);
        }