我也错过了配置中更具可读性的字符串的行为。所以我在字符串上创建了一个扩展方法。
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;
}
}
并像使用它
[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");
}
编辑:
对于点符号支持:
学分转到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;
}
}