2010-09-01 38 views
3

我得到的DoNotPassLiteralsAsLocalizedParameters的FxCop侵犯两个例外在下面的方法代码投掷线:排除的FxCop DoNotPassLiteralsAsLocalizedParameters违法行为异常实例化或本地化异常消息

public bool IsPageAccessible(string url, string documentId) { 
    if (url == null) { 
     throw new ArgumentNullException("url", @"url must not be null, use string.Empty if you don't care what the url is."); 
    } 

    if (documentId == null) { 
     throw new ArgumentNullException("documentId", "documentId must not be null, use string.Empty if you don't care what the documentId is."); 
    } 
    return true; 
} 

这意味着:

的FxCop全球化# CA1303字符串 嵌入源 代码中的文字难以本地化。避免 在通常需要本地化的 字符串的情况下将字符串文字作为参数 传递。大多数 本地化应用程序,例如, 应本地化字符串参数是 传递给异常的构造函数。 当创建一个异常实例, 因此,字符串参数检索 从一个字符串表比字符串文字更 合适。

推理:

我不想本地化异常消息。只有英文很好。尽管我们正在构建一个API,但所有开发人员都知道英语。不管怎样,不应该在生产服务器上向访问者显示异常消息。

问题:

  • 你与我的异常消息本地化推理不同意?为什么?
  • 有没有办法从所有异常实例化中排除这个FxCop警告?我们确实本地化API的其他部分。这些部分将对最终用户具有可见的文本。所以我们从这些情况下保留警告的价值。
  • 您认为我应该如何处理这个问题?

回答

1

我觉得你的推理很好,我讨厌它,当我在Visual Studio中有本地化的异常,并且因为编程的通用语言是英语而无法找到帮助。

更一般地,你不应该试图迎合每一个的FxCop规则,这样可以迅速成为一种负担。最好专注于一部分规则。

我不认为你可以在一个特定的异常排除警告,但可以使用SuppressMessage属性排除检测:

[SuppressMessage("Microsoft.Globalization", 
       "CA1303:DoNotPassLiteralsAsLocalizedParameters", 
       Justification="Exception are not localized")] 
public bool IsPageAccessible(string url, string documentId) { 
    if (url == null) { 
    throw new ArgumentNullException("url", @"url must not be null, use string.Empty if you don't care what the url is."); 
    } 

    if (documentId == null) { 
    throw new ArgumentNullException("documentId", "documentId must not be null, use string.Empty if you don't care what the documentId is."); 
    } 
    return true; 
} 

另一种方式,是写一个custom fxcop rule添加此行为。