Только что изменил приведенный выше ответ на следующий:
string data = "This is a {template1} that is {template2}.";
Dictionary<string, string> replacements = new Dictionary<string, string>(){
{"{template1}", "car"},
{"{template2}", "red"},
};
data.Parse(replacements);
Метод расширения:
public static class Parser
{
public static string Parse(this string template, Dictionary<string, string> replacements)
{
if (replacements.Count > 0)
{
template = replacements.Keys
.Aggregate(template, (current, key) => current.Replace(key, replacements[key]));
}
return template;
}
}
Надеюсь, это поможет ..:)