ITemplateText
ITemplateText holds template info and prints template texts.
/// <summary>Template text.</summary>
public interface ITemplateText : ITemplateFormatPrintable
{
/// <summary>Template text in its escaped format, e.g. "Welcome, {{{0}}}.\nYou received {1,15:N0} credit(s).</summary>
string Text { get; set; }
/// <summary>(optional) Description of the format type</summary>
ITemplateFormat? TemplateFormat { get; set; }
/// <summary>Template breakdown</summary>
ITemplateBreakdown Breakdown { get; set; }
}
TemplateText is the default implementation. It requires template text and ITemplateFormat.
ITemplateText templateText = new TemplateText("Error code {0} (0x{0:X4}).", TemplateFormat.Brace).SetReadOnly();
WriteLine(templateText.Print(null, new object?[] { 0x100 })); // "Error code 256 (0x0100)."
FormatText is simple wrapper implementation that adapts string.Format() compatible format string into an ITemplateText.
ITemplateText templateText = new FormatText("Error code {0} (0x{0:X4}).");
string print1 = string.Format(templateText.Text, 0x100);
string print2 = templateText.Print(null, new object?[] { 0x100 });
WriteLine(print2); // "Error code 256 (0x0100)."
ITemplateText.ParameterNames and ITemplateText.Parameters are sorted by index value.
ITemplateText templateText = new FormatText("Error code {2} (0x{0:X4}).");
WriteLine(templateText.ParameterNames[0]); // "0"
WriteLine(templateText.ParameterNames[1]); // ""
WriteLine(templateText.ParameterNames[2]); // "2"
ParameterlessText is light-weight string wrapper for strings that have no parameters.
ITemplateText templateText = new ParameterlessText("Application started.");
Full Example
Full example
using System.Collections.Generic;
using System.Globalization;
using Avalanche.Template;
using Avalanche.Utilities;
using static System.Console;
public class templatetext
{
public static void Run()
{
{
// <01>
ITemplateText templateText = new TemplateText("Error code {0} (0x{0:X4}).", TemplateFormat.Brace).SetReadOnly();
WriteLine(templateText.Print(null, new object?[] { 0x100 })); // "Error code 256 (0x0100)."
// </01>
}
{
// <02>
ITemplateText templateText = new FormatText("Error code {0} (0x{0:X4}).");
string print1 = string.Format(templateText.Text, 0x100);
string print2 = templateText.Print(null, new object?[] { 0x100 });
WriteLine(print2); // "Error code 256 (0x0100)."
// </02>
}
{
// <03>
ITemplateText templateText = new FormatText("Error code {2} (0x{0:X4}).");
WriteLine(templateText.ParameterNames[0]); // "0"
WriteLine(templateText.ParameterNames[1]); // ""
WriteLine(templateText.ParameterNames[2]); // "2"
// </03>
}
{
// <10>
ITemplateText templateText = new ParameterlessText("Application started.");
// </10>
WriteLine(templateText.Print(null));
}
}
}