コード例 #1
0
        /// <summary>
        /// Provides the actual implementation for serializing a value of type <see cref="T" />.
        /// </summary>
        /// <param name="value">The value to serialize.</param>
        /// <param name="writer">The writer to serialize with.</param>
        protected override void SerializeImplementation(ref T value, IDataWriter writer)
        {
            if (SerializableFormatter <T> .ISerializableConstructor != null)
            {
                // Don't have GetType() in the below lines as a strongly typed T value, since
                // people can "override" (shadow) GetType() on derived classes with the "new"
                // operator. By referencing the type as a System.Object, we ensure the correct
                // GetType() method is always called.
                //
                // (Yes, this has actually happened, and this was done to fix it.)

                var serializable = value as ISerializable;
                var info         = new SerializationInfo((value as object).GetType(), writer.Context.FormatterConverter);

                try
                {
                    serializable.GetObjectData(info, writer.Context.StreamingContext);
                }
                catch (Exception ex)
                {
                    writer.Context.Config.DebugContext.LogException(ex);
                }

                this.WriteSerializationInfo(info, writer);
            }
            else
            {
                ReflectionFormatter.Serialize(value, writer);
            }
        }
コード例 #2
0
        /// <summary>
        /// Provides the actual implementation for serializing a value of type <see cref="T" />.
        /// </summary>
        /// <param name="value">The value to serialize.</param>
        /// <param name="writer">The writer to serialize with.</param>
        protected override void SerializeImplementation(ref T value, IDataWriter writer)
        {
            if (SerializableFormatter <T> .ISerializableConstructor != null)
            {
                var serializable = value as ISerializable;
                var info         = new SerializationInfo(value.GetType(), writer.Context.FormatterConverter);

                try
                {
                    serializable.GetObjectData(info, writer.Context.StreamingContext);
                }
                catch (Exception ex)
                {
                    writer.Context.Config.DebugContext.LogException(ex);
                }

                this.WriteSerializationInfo(info, writer);
            }
            else
            {
                ReflectionFormatter.Serialize(value, writer);
            }
        }