C# 从异常实例化或本地化异常消息中排除FxCop DoNotPassLiteralSALocalizedParameters冲突
我在下面的方法代码中获得了两个异常引发行的DoNotPassLiteralSALocalizedParameters FxCop冲突:C# 从异常实例化或本地化异常消息中排除FxCop DoNotPassLiteralSALocalizedParameters冲突,c#,C#,我在下面的方法代码中获得了两个异常引发行的DoNotPassLiteralSALocalizedParameters 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 do
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的其他部分。具有最终用户可见文本的部件。因此,在这些情况下,我们从保持警告中得到一个值
- 你认为我应该怎么处理这件事
- 我认为你的推理很好,我讨厌在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;
}
另一种方法是编写一个脚本来添加此行为