Creating Your Own Extension - axuno/SmartFormat GitHub Wiki
A Hello World Example
Implement IFormatter
public class HelloFormatter : IFormatter
{
public string Name { get; set; } = "hello";
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
var iCanHandleThisInput = formattingInfo.CurrentValue is bool;
if (!iCanHandleThisInput)
return false;
formattingInfo.Write("HELLO ");
if ((bool) formattingInfo.CurrentValue)
formattingInfo.Write(formattingInfo.FormatterOptions);
else
formattingInfo.Write(formattingInfo.Format.GetLiteralText());
return true;
}
}
Example usage:
Smart.Default.AddExtensions(new HelloFormatter());
Smart.Format("{value:hello(world):earth}", new { value = true });
// Outputs: "HELLO world"
Smart.Format("{value:hello(world):earth}", new { value = false });
// Outputs: "HELLO earth"