TemplifyWith Extension Method

by Jason Wicker 17. July 2012 15:48

I created a super-simple extension-method for doing class-based token replacement on strings, using reflection. You can just pass a C# object to this function and it will replace tokens matching the object's property values in a straight-forward, easy way.

So, given this class:

class Car 
{
    public string Name { get; set; }
}
var car = new Car { Name = "Mazda" };
var message = "Hello [Name]!".TemplifyWith(car);

The resulting string (message) will be "Hello Mazda!"

Here's the code:

Tags: , , ,

Stack Overflow

Month List