I have two classes, none of which I can change in any way:
Class 1: Takes a TextWriter
as constructor parameter and uses it as an output stream.
Class 2: Provides a method WriteLine(string)
.
I need an adapter, such that all the output of Class1 is written to Class2. Therefore I started an adapter which extends TextWriter
and buffers incoming text, flushing it to the class2 instance as soon as a new line arrives.
However, there are many and more methods in TextWriter - which should I implement? Output in Class1 is string only.
According to MSDN one should override Write(char) as a minimum, however, this enforces me to do all the \r\n new line handling myself as well...
Q1: Do you know of a better way to reach my goal? Q2: If no, which TextWriter methods should I override to have minimum implementation effort.
Implementing
Write(char)
on yourTextWriter
derived class is all you need to do. If somebody callsWriteLine
on your new class, the base classWriteLine
method is called. It will do the right thing: call yourWrite
method with the individual\r
and\n
characters.Actually,
WriteLine(string)
looks something like this:And
Write(string)
is, in effect:All of the
Write
methods inTextWriter
resolve to something that callsWrite(char)
in a loop.You really don't have to implement anything else. Just override
Write(char)
and plug it in. It will work.You can override those other methods. Doing so will make your class a little more efficient (faster). But it's not required. I say do the simplest thing you can. Then, if you determine after profiling that your custom writer is too slow, override other methods as necessary.
Here's a minimal
TextWriter
descendant:If I then write:
The output is: