虽然打约与此question的代码,我也可以通过从VS IDE中运行了StyleCop下降SA1115的犯规。一些摆弄后,这里是觉得StyleCop的最终结果是OK:
public static string Format<T>(string pattern, T template)
{
Dictionary<string, string> cache = new Dictionary<string, string>();
return RegexExpression.Replace(
pattern,
match =>
{
string key = match.Groups[1].Value;
string value;
if (!cache.TryGetValue(key, out value))
{
var prop = typeof(T).GetProperty(key);
if (prop == null)
{
throw new ArgumentException("Not found: " + key, "pattern");
}
value = Convert.ToString(prop.GetValue(template, null));
cache.Add(key, value);
}
return value;
});
}
只是想我会分享它。
< - 这个(问题根源=参数太多的方法) – annakata 2008-12-09 14:27:32
OK:具体实例,然后。我正在做一个Console.WriteLine并以格式化的字符串打印大量不同的信息(约10项)。你如何使这个论证列表更短? (真的,我很想知道!) – 2008-12-09 14:41:24