public static TextWriter Serialize([NotNull] this TextWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } ISerialTextWriter stw = writer as ISerialTextWriter; return(stw != null ? writer : new SerialTextWriter(writer)); }
/// <summary> /// Initializes a new instance of the <see cref="SerialTextWriter" /> class. /// </summary> /// <param name="writer">The writer.</param> internal SerialTextWriter([NotNull] TextWriter writer) : base(writer.FormatProvider) { if (writer == null) { throw new ArgumentNullException("writer"); } // Sanity check, we shouldn't normally create a serial text writer on a serialized text writer, // but it can happen (for example when initializing ConsoleTextWriter to use TraceTextWriter). ISerialTextWriter stw = writer as ISerialTextWriter; if (stw != null) { // Use the underlying writer and context from the original serialized writer. _writer = stw.Writer; _context = stw.Context; } else { // This writer needs a new serializing context. _writer = writer; _context = new SerializingSynchronizationContext(); } }