using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;
public class Test
{
public static void Main()
{
string formatString = "{firstName} {lastName} is awesome at age {age}.";
Console.WriteLine(formatString.FormatUnicorn(new {
firstName = "joe",
lastName = "blow",
age = 13
}));
}
}
public static class StringExtensions {
public static string FormatUnicorn(this string str, object arguments) {
string output = str;
Type type = arguments.GetType();
foreach (PropertyInfo property in type.GetProperties())
{
Regex regex = new Regex(@"\{" + property.Name + @"\}");
output = regex.Replace(output, property.GetValue(arguments, null).ToString());
}
return output;
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLlRleHQuUmVndWxhckV4cHJlc3Npb25zOwp1c2luZyBTeXN0ZW0uUmVmbGVjdGlvbjsKCnB1YmxpYyBjbGFzcyBUZXN0CnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKCXsKCQlzdHJpbmcgZm9ybWF0U3RyaW5nID0gIntmaXJzdE5hbWV9IHtsYXN0TmFtZX0gaXMgYXdlc29tZSBhdCBhZ2Uge2FnZX0uIjsKCQkKCQlDb25zb2xlLldyaXRlTGluZShmb3JtYXRTdHJpbmcuRm9ybWF0VW5pY29ybihuZXcgewoJCQlmaXJzdE5hbWUgPSAiam9lIiwKCQkJbGFzdE5hbWUgPSAiYmxvdyIsCgkJCWFnZSA9IDEzCgkJfSkpOwoJfQp9CgpwdWJsaWMgc3RhdGljIGNsYXNzIFN0cmluZ0V4dGVuc2lvbnMgewoJcHVibGljIHN0YXRpYyBzdHJpbmcgRm9ybWF0VW5pY29ybih0aGlzIHN0cmluZyBzdHIsIG9iamVjdCBhcmd1bWVudHMpIHsKCQlzdHJpbmcgb3V0cHV0ID0gc3RyOwoKICAgICAgICBUeXBlIHR5cGUgPSBhcmd1bWVudHMuR2V0VHlwZSgpOwoKICAgICAgICBmb3JlYWNoIChQcm9wZXJ0eUluZm8gcHJvcGVydHkgaW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