Created
September 26, 2013 22:32
-
-
Save tomspilman/6721517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Writes a single object preceded by a type identifier to the output binary. | |
| /// </summary> | |
| /// <typeparam name="T">The type of value.</typeparam> | |
| /// <param name="value">The value to write.</param> | |
| /// <remarks>This method can be called recursively with a null value.</remarks> | |
| public void WriteObject<T>(T value) | |
| { | |
| if (value == null) | |
| Write7BitEncodedInt(0); | |
| else | |
| { | |
| var typeWriter = GetTypeWriter(value.GetType()); | |
| int 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); | |
| } | |
| } | |
| /// <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.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); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment