Exemplo n.º 1
0
        /// <summary>
        ///     Retrieve internal data directly from a SerializationReader.
        /// </summary>
        /// <param name="reader"> The SerializationReader to use </param>
        /// <param name="context"> Optional context to use as a hint as to what to retrieve (BitVector32 is useful) </param>
        public virtual void DeserializeOwnedData(SerializationReader reader, object context)
        {
            using (Block block = reader.EnterBlock())
            {
                switch (block.Version)
                {
                case 1:
                {
                    object       temp    = (new BinaryFormatter()).Deserialize(reader.BaseStream);
                    MemberInfo[] members = FormatterServices.GetSerializableMembers(GetType());
                    FormatterServices.PopulateObjectMembers(this, members,
                                                            FormatterServices.GetObjectData(temp, members));
                }
                break;

                default:
                    throw new BlockUnsupportedVersionException();
                }
            }
        }
        /// <summary>
        ///     Implementation of serialization, which uses JsonSerializer internally.
        ///     You can specify JsonSerializerOptions as context.
        /// </summary>
        /// <param name="reader"> The SerializationReader to use </param>
        /// <param name="context"> Optional context to use as a hint as to what to retrieve (BitVector32 is useful) </param>
        public virtual void DeserializeOwnedData(SerializationReader reader, object?context)
        {
            using (Block block = reader.EnterBlock())
            {
                switch (block.Version)
                {
                case 1:
                    string s    = reader.ReadString();
                    object?temp = JsonSerializer.Deserialize(s, GetType(), context as JsonSerializerOptions);
                    if (temp is null)
                    {
                        throw new InvalidOperationException();
                    }
                    MemberInfo[] members = FormatterServices.GetSerializableMembers(GetType());
                    FormatterServices.PopulateObjectMembers(this, members,
                                                            FormatterServices.GetObjectData(temp, members));
                    break;

                default:
                    throw new BlockUnsupportedVersionException();
                }
            }
        }