This doesn't exactly match your ideal, but something like this might work for you:
public static class Extensions { public static string Format(this object data, string format) { var values = new List<object>(); var type = data.GetType(); format = Regex.Replace(format, @"(^|[^{])\{([^{}]+)\}([^}]|$)", x => { var keyValues = Regex.Split(x.Groups[2].Value, "^([^:]+):?(.*)$") .Where(y => !string.IsNullOrEmpty(y)); var key = keyValues.ElementAt(0); var valueFormat = keyValues.Count() > 1 ? ":" + keyValues.ElementAt(1) : string.Empty; var value = GetValue(key, data, type); values.Add(value); return string.Format("{0}{{{1}{2}}}{3}", x.Groups[1].Value, values.Count - 1, valueFormat, x.Groups[3].Value); }); return string.Format(format, values.ToArray()); } private static object GetValue(string name, object data, Type type) { var info = type.GetProperty(name); return info.GetValue(data, new object[0]); } }
This should allow you to do this formatting on any object:
new {Person = "Me", Location = "On holiday"} .Format("{Person} is currently {Location}");
It will also allow you to add formatting:
new {Person = "Me", Until = new DateTime(2013,8,1)} .Format("{Person} is away until {Until:yyyy-MM-dd});
How is that for you? I'm sure the code can be improved in terms of efficiency, but it works!
source share