An implementation of named string replacements, which allows formatting, parsing, and capturing properties. MessageTemplates is compatible with the Message Templates Standard. The C# implementation was extracted from Serilog.
There is also a F# implementation, see the messagetemplates-fsharp repository.
class Chair {
public string Back { get { return "straight"; } }
public int[] Legs { get { return new[] { 1, 2, 3, 4 }; } }
public override string ToString() { return "a chair"; }
}
Assert.Equal(
"I sat at Chair { Back: \"straight\", Legs: [1, 2, 3, 4] }",
MessageTemplate.Format("I sat at {@Chair}", new Chair()));
Message templates are a superset of standard .NET format strings, so any format string acceptable to string.Format()
will also be correctly processed by MessageTemplates
.
- Property names are written between
{
and}
brackets - Brackets can be escaped by doubling them, e.g.
{{
will be rendered as{
- Formats that use numeric property names, like
{0}
and{1}
exclusively, will be matched with theFormat
method's parameters by treating the property names as indexes; this is identical tostring.Format()
's behaviour - If any of the property names are non-numeric, then all property names will be matched from left-to-right with the
Format
method's parameters - Property names may be prefixed with an optional operator,
@
or$
, to control how the property is serialised- The destructuring operator (
@
) in front of will serialize the object passed in, rather than convert it usingToString()
. - the stringification operator (
$
) will convert the property value to a string before any other processing takes place, regardless of its type or implemented interfaces.
- The destructuring operator (
- Property names may be suffixed with an optional format, e.g.
:000
, to control how the property is rendered; these format strings behave exactly as their counterparts within thestring.Format()
syntax
Opening MessageTemplates.sln requires VS2015 Update 3 with the .NET Core tooling to be installed.
See: https://www.microsoft.com/net/core#windows