fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Reflection;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string formatString = "{firstName} {lastName} is awesome at age {age}.";
  11.  
  12. Console.WriteLine(formatString.FormatUnicorn(new {
  13. firstName = "joe",
  14. lastName = "blow",
  15. age = 13
  16. }));
  17. }
  18. }
  19.  
  20. public static class StringExtensions {
  21. public static string FormatUnicorn(this string str, object arguments) {
  22. string output = str;
  23.  
  24. Type type = arguments.GetType();
  25.  
  26. foreach (PropertyInfo property in type.GetProperties())
  27. {
  28. Regex regex = new Regex(@"\{" + property.Name + @"\}");
  29. output = regex.Replace(output, property.GetValue(arguments, null).ToString());
  30. }
  31.  
  32. return output;
  33. }
  34. }
Success #stdin #stdout 0.08s 34208KB
stdin
Standard input is empty
stdout
joe blow is awesome at age 13.