我在下面的方法代码中得到了两个异常抛出行的 DoNotPassLiteralsAsLocalizedParameters FxCop 违规:
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 的其他部分。这些部分将包含最终用户可见的文本。因此,在这些情况下,我们可以通过保留警告来获得价值。
- 你认为我应该如何处理这件事?
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?
发布评论
评论(1)
我认为你的推理很好,我讨厌当我在 Visual Studio 中本地化异常并且无法找到帮助时,因为编程的通用语言是英语。
更一般地说,您不应该尝试遵守所有 fxcop 规则,这很快就会成为一种负担。最好专注于规则的子集。
我不认为您可以排除特定异常中的警告,但您可以使用
SuppressMessage
属性排除检测:另一种方法是编写 自定义 fxcop 规则 添加此行为。
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 :Another way, would be to write a custom fxcop rule to add this behavior.