雖然打約與此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