Method AddMessageWriter
- Namespace
- Ploch.CommandLine.Spectre.Output
- Assembly
- Ploch.CommandLine.Spectre.dll
AddMessageWriter<TMessageType, TWriter>(IServiceCollection)
Registers a message writer for a specific message type in the service collection.
public static IServiceCollection AddMessageWriter<TMessageType, TWriter>(this IServiceCollection service) where TWriter : class, IMessageWriter<TMessageType>
Parameters
serviceIServiceCollectionThe service collection to add the writer to.
Returns
- IServiceCollection
The same service collection instance to enable method chaining.
Type Parameters
TMessageTypeThe type of message that the writer can write.
TWriterThe type of the writer implementation.
Examples
public sealed class QuotingStringWriter(IAnsiConsole console) : TypeMessageWriter<string>
{
public override void Write(string? message, IMessageFormatterProcessor? formatterProcessor = null, IFormatProvider? formatProvider = null) =>
console.Write($"\"{message}\"");
}
AppBuilder.Create(args)
.ConfigureServices(services => services.AddMessageWriter<string, QuotingStringWriter>());
Remarks
A writer registered for a type that already has one replaces it for that exact type, so this is how the
rendering of a string or a FormattableString written through
Write<TMessage>(TMessage, IFormatProvider?) is changed. A writer takes the IAnsiConsole it renders to,
not IOutput, which depends on the writers.