Skip to content

Instantly share code, notes, and snippets.

@adamchester
Last active May 27, 2016 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamchester/b8f15afee113b3b4375b49c78d6018d6 to your computer and use it in GitHub Desktop.
Save adamchester/b8f15afee113b3b4375b49c78d6018d6 to your computer and use it in GitHub Desktop.
MessageTemplates (C#) Samples
void Main() {
var fr = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
var enAu = System.Globalization.CultureInfo.CreateSpecificCulture("en-AU");
var zhCn = System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN");
var invariant = System.Globalization.CultureInfo.InstalledUICulture;
var chairSitTemplate = MessageTemplate.Parse("I sat at {@Chair} on {SitDate,10:dd-MMM-yyyy}");
chairSitTemplate.Format(fr, new Chair(), DateTimeOffset.UtcNow)
.Dump("Format using fr-FR culture uses comma decimal separator and french month names");
chairSitTemplate.Format(zhCn, new Chair(), DateTimeOffset.UtcNow)
.Dump("Format using zh-CN culture uses dot decimal separator and chinese month names");
chairSitTemplate.Format(enAu, new Chair())
.Dump("Format en-AU, only providing the first argument, leaving 'SitTime' empty");
MessageTemplate.Capture(chairSitTemplate.Text, new InfinitelyDeepChair(), DateTimeOffset.UtcNow)
.Dump("Capturing will stop destructuring at 10 levels deep by default");
MessageTemplate.CaptureWith(2, null, null, chairSitTemplate,
new object[] { new InfinitelyDeepChair(), DateTime.UtcNow })
.Dump("Capturing can be configuring to any maximum depth");
MessageTemplate.Capture("I sat at {Chair} on {SitTime}", new Chair(), DateTime.UtcNow)
.Dump("Capture a chair without destructuring will use 'ToString'");
MessageTemplate.Capture("I sat at {Chair} on {SitTime}").Dump("Capture but provide no values");
}
class InfinitelyDeepChair {
public DateTimeOffset Value { get { return DateTimeOffset.UtcNow; } }
public InfinitelyDeepChair Child {
get { return new InfinitelyDeepChair(); }
}
}
class Chair {
public decimal ABigNum { get { return 123456.1122351M; } }
public string Back { get { return "straight"; } }
public int[] Legs { get { return new[] { 1, 2, 3, 4 }; } }
public override string ToString() { return "a chair"; }
}
Format using fr-FR culture uses comma decimal separator and french month names
I sat at Chair { ABigNum: 123456,1122351, Back: "straight", Legs: [1, 2, 3, 4] } on 27-mai-2016
Format using zh-CN culture uses dot decimal separator and chinese month names
I sat at Chair { ABigNum: 123456.1122351, Back: "straight", Legs: [1, 2, 3, 4] } on 27-5月-2016
Format en-AU, only providing the first argument, leaving 'SitTime' empty
I sat at Chair { ABigNum: 123456.1122351, Back: "straight", Legs: [1, 2, 3, 4] } on {SitDate,10:dd-MMM-yyyy}
Capturing will stop destructuring at 10 levels deep by default
TemplateProperty[] (2 items)
NameValue
ChairInfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: null, Child: null } } } } } } } } } }
SitDate05/27/2016 23:02:55 +00:00
Capturing can be configuring to any maximum depth
TemplateProperty[] (2 items)
NameValue
ChairInfinitelyDeepChair { Value: 05/27/2016 23:02:55 +00:00, Child: InfinitelyDeepChair { Value: null, Child: null } }
SitDate05/27/2016 23:02:55
Capture a chair without destructuring will use 'ToString'
TemplateProperty[] (2 items)
NameValue
Chair"a chair"
SitTime05/27/2016 23:02:55
Capture but provide no values
(0 items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment