fork download
  1. using System;
  2. using System.Reflection;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var result = Translate("Hello {!User!}. Welcome to {!GroupName!}!", new {
  10. User = "John",
  11. GroupName = "The Community"
  12. });
  13. Console.WriteLine(result);
  14. }
  15.  
  16. public static string Translate(string pattern, object context)
  17. {
  18. return Regex.Replace(pattern, @"\{!(\w+)!}", match => {
  19. var tag = match.Groups[1].Value;
  20. if (context != null)
  21. {
  22. PropertyInfo prop = context.GetType().GetProperty(tag);
  23. if (prop != null)
  24. {
  25. object value = prop.GetValue(context);
  26. if (value != null)
  27. {
  28. return value.ToString();
  29. }
  30. }
  31. }
  32. return "";
  33. });
  34. }
  35. }
Success #stdin #stdout 0.04s 134592KB
stdin
Standard input is empty
stdout
Hello John. Welcome to The Community!