简体   繁体   English

Visual Studio 2010代码分析CA1305(指定IFormatProvider)规则不适用于TryParse方法

[英]Visual Studio 2010 Code Analysis CA1305 (Specify IFormatProvider) rule doesn't work for TryParse methods

I am wondering why Code Analysis rule CA1305 - Specify IFormatProvider doesn't work for TryParse methods? 我想知道为什么代码分析规则CA1305 - 指定IFormatProvider不适用于TryParse方法?

For example with 'Microsoft All Rules' rule set, Code Analysis gives CA1305 warning for following code: 例如,使用“Microsoft All Rules”规则集,代码分析会针对以下代码提供CA1305警告:

static void Main(string[] args)
{
    string text = args[0];

    double value = double.Parse(text);
}

but doesn't give CA1305 warning for following code: 但是没有给出以下代码的CA1305警告:

static void Main(string[] args)
{
    string text = args[0];

    double value;
    if (!double.TryParse(text, out value))
        value = 0;
}

I found this to be very unfortunate because TryParse methods are the correct way to do parsing if input string is not reliable. 我发现这非常不幸,因为如果输入字符串不可靠,TryParse方法是解析的正确方法。

Does anyone know if CA1305 rule for TryParse methods is implemented in some newer version of Code Analysis tool or by some 3rd party? 有谁知道TryParse方法的CA1305规则是在某些较新版本的代码分析工具中实现还是由某些第三方实现?

这已经在FxCop以及SonarLint等更新的工具中得到修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM