Esempio n. 1
0
        /// <summary>
        /// Writes a single object to the output binary, using the specified type hint and writer worker.
        /// </summary>
        /// <typeparam name="T">The type of value.</typeparam>
        /// <param name="value">The value to write.</param>
        /// <param name="typeWriter">The content type writer.</param>
        /// <remarks>The type hint should be retrieved from the Initialize method of the ContentTypeWriter
        /// that is calling WriteObject, by calling GetTypeWriter and passing it the type of the field used
        /// to hold the value being serialized. If the hint type is a sealed value type (which cannot be
        /// null or hold a polymorphic object instance) this method skips writing the usual type identifier.</remarks>
        public void WriteObject <T>(T value, ContentTypeWriter typeWriter)
        {
            if (typeWriter == null)
            {
                throw new ArgumentNullException("typeWriter");
            }

            if (value == null)
            {
                // Zero means a null object
                Write7BitEncodedInt(0);
            }
            else
            {
                if (value.GetType().IsValueType)
                {
                    // Value types do not have the type identifier written to the file
                    typeWriter.Write(this, value);
                }
                else
                {
                    int index = typeWriterMap[typeWriter];
                    // Because zero means null object, we add one to the index before writing it to the file
                    Write7BitEncodedInt(index + 1);
                    typeWriter.Write(this, value);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Writes a single object to the output binary using the specified writer worker.
        /// </summary>
        /// <typeparam name="T">The type of value.</typeparam>
        /// <param name="value">The value to write.</param>
        /// <param name="typeWriter">The writer worker. This should be looked up from the Initialize method
        /// of the ContentTypeWriter that is calling WriteRawObject, by calling GetTypeWriter.</param>
        /// <remarks>WriteRawObject does not write any type identifier, so it cannot support null or polymorphic
        /// values, and the reader must specify an identical type while loading the compiled data.</remarks>
        public void WriteRawObject <T>(T value, ContentTypeWriter typeWriter)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (typeWriter == null)
            {
                throw new ArgumentNullException("typeWriter");
            }

            typeWriter.Write(this, value);
        }
Esempio n. 3
0
        /// <summary>
        /// Writes a single object to the output binary, using the specified type hint and writer worker.
        /// </summary>
        /// <typeparam name="T">The type of value.</typeparam>
        /// <param name="value">The value to write.</param>
        /// <param name="typeWriter">The content type writer.</param>
        /// <remarks>The type hint should be retrieved from the Initialize method of the ContentTypeWriter
        /// that is calling WriteObject, by calling GetTypeWriter and passing it the type of the field used
        /// to hold the value being serialized.
        /// </remarks>
        public void WriteObject <T>(T value, ContentTypeWriter typeWriter)
        {
            if (typeWriter == null)
            {
                throw new ArgumentNullException("typeWriter");
            }

            if (typeWriter.TargetType.IsValueType)
            {
                typeWriter.Write(this, value);
            }
            else
            {
                WriteObject(value);
            }
        }
Esempio n. 4
0
        protected internal override void Write(ContentWriter output, object value)
        {
            if (_baseTypeWriter != null)
            {
                _baseTypeWriter.Write(output, value);
            }

            foreach (var property in _properties)
            {
                Write(value, output, property);
            }

            foreach (var field in _fields)
            {
                Write(value, output, field);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Writes a single object to the output binary, using the specified type hint and writer worker.
        /// </summary>
        /// <typeparam name="T">The type of value.</typeparam>
        /// <param name="value">The value to write.</param>
        /// <param name="typeWriter">The content type writer.</param>
        /// <remarks>The type hint should be retrieved from the Initialize method of the ContentTypeWriter
        /// that is calling WriteObject, by calling GetTypeWriter and passing it the type of the field used
        /// to hold the value being serialized.
        /// </remarks>
        public void WriteObject <T>(T value, ContentTypeWriter typeWriter)
        {
            if (typeWriter == null)
            {
                throw new ArgumentNullException("typeWriter");
            }

            if (value == null)
            {
                // Zero means a null object
                Write7BitEncodedInt(0);
            }
            else
            {
                Type objectType = typeof(T);
                if (!objectType.IsValueType)
                {
                    var index = typeWriterMap[typeWriter.GetType()];
                    // Because zero means null object, we add one to the index before writing it to the file
                    Write7BitEncodedInt(index + 1);
                }
                typeWriter.Write(this, value);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, T value)
 {
     elementWriter.Write(output, Convert.ChangeType(value, underlyingType));
 }