I also missed the behavior of more readable strings in config. So I create an extension method on string.
public static class StringExtensions
{
public static string Interpolate(this string self, object interpolationContext)
{
var placeholders = Regex.Matches(self, @"\{(.*?)\}");
foreach (Match placeholder in placeholders)
{
var placeholderValue = placeholder.Value;
var placeholderPropertyName = placeholderValue.Replace("{", "").Replace("}", "");
var property = interpolationContext.GetType().GetProperty(placeholderPropertyName);
var value = property?.GetValue(interpolationContext)?.ToString() ?? "";
self = self.Replace(placeholderValue, value);
}
return self;
}
}
And use it like
[Fact]
public void Foo()
{
var world = "World";
var someInt = 42;
var unused = "Not used";
//This is a normal string, it can be retrieved from config
var myString = "Hello {world}, this is {someInt}";
//You need to pass all local values that you may be using in your string interpolation. Pass them all as one single anonymous object.
var result = myString.Interpolate(new {world, someInt, unused});
result.Should().Be("Hello World, this is 42");
}
EDIT:
For dotted notation support:
Credits go to this answer.
public static class StringExtensions
{
public static string Interpolate(this string self, object interpolationContext)
{
var placeholders = Regex.Matches(self, @"\{(.*?)\}");
foreach (Match placeholder in placeholders)
{
var placeholderValue = placeholder.Value;
var placeholderPropertyName = placeholderValue.Replace("{", "").Replace("}", "");
var value = GetPropertyValue(interpolationContext, placeholderPropertyName)?.ToString() ?? "";
self = self.Replace(placeholderValue, value);
}
return self;
}
public static object GetPropertyValue(object src, string propName)
{
if (src == null) throw new ArgumentException("Value cannot be null.", nameof(src));
if (propName == null) throw new ArgumentException("Value cannot be null.", nameof(propName));
if (propName.Contains("."))
{
var temp = propName.Split(new char[] {'.'}, 2);
return GetPropertyValue(GetPropertyValue(src, temp[0]), temp[1]);
}
var prop = src.GetType().GetProperty(propName);
return prop != null ? prop.GetValue(src, null) : null;
}
}