开发者

Exclude FxCop DoNotPassLiteralsAsLocalizedParameters violations from exception instantiation or localize exception messages

I get the DoNotPassLiteralsAsLocalizedParameters FxCop violation for both exception throwing lines in the below method code:

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;
}

It means:

fxcop Globalization#CA1303 String literals that are embedded in source code are difficult to localize. Avoid passing string literals as arguments in circumstances where a localized string is generally expected. Most localized applications, for example, should localize string arguments that are passed to exception constructors. When creating an Exception instance, therefore, a string argument retrieved from a string table is more appropriate than a string literal.

Reasoning:

I do not wish to localize the exception message. Having only English is fine. Even though we are building an API, English is known by all developers. And the exception message should not be shown to the visitor on a production server anyway.

Questions:

  • Do you disagree with my reasoning about exception message localization? Why?
  • Is there a way to exclude this FxCop warning from all exception instantiation only? We do localize other parts of the API. Those parts which will have text visible to the end user. So we get a 开发者_运维技巧value from keeping the warning in those cases.
  • How do you think I should deal with this?


I think your reasoning is good, I hate it when I have localized exception in Visual Studio and can't find help on it because the lingua franca for programming is English.

More generally, you shouldn't try to conform to every fxcop rules, this can quickly be a burden. It is better to concentrate on a subset of rules.

I don't think that you can exclude warning in a particular exception, but you can exclude detection using SuppressMessage attribute :

[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;
}

Another way, would be to write a custom fxcop rule to add this behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜