Styling output with custom formatters
As we saw in Figures 14.2 and 14.3, the default formatting for List<Footballer>
is not very helpful in understanding our team at a glance. Thankfully, the Formatter
class gives you a Register
method that allows you to provide your own custom formatter.
The Register
method takes in a specific type that the formatter should format, FormatDelegate
, consisting of an instance of that type, and a writer that allows you to write content to the output. Let’s see it in action by registering custom formatting for any IEnumerable
of Footballer
objects:
Formatter.Register<IEnumerable<Footballer>>( (players, writer) => { writer.WriteLine("<h3>Team Scoring Stats</h3>"); writer.WriteLine("<table>"); writer.WriteLine("<thead><tr>"); writer.WriteLine("<th>Name<...