Last post I showed a cool way to parse strings into typed objects. In this one, I want to show the opposite. How to build strings using a template and an object.
The idea is to use a template like {BirthDate:D} My name is {FirstName} {LastName} and I'm cool.
and an object with properties BirthDate
, FirstName
and LastName
to fill in the placeholder values and apply optional formatting. There are several uses for a utility like this but my favorite use is templated email messages.
Kudos to Scott Hanselman for this solution.
The nice part about this solution is that it won’t throw any exceptions if it can’t correctly build the string. It will just leave the bad part of the template in the result. Also, it supports Format Providers which also allows for specific culture targeting:
You can find the code in Scott’s git hub repo
PS: I know that Scott posted about this years ago but I use this so often that I felt like it deserved another blog post…
Happy coding!