加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Roslyn Analyzer规则不会使构建失败

发布时间:2020-12-15 04:31:51 所属栏目:百科 来源:网络整理
导读:继MS的 this教程之后,我为Roslyn创建了一个分析器. 根据该页面,您可以将规则标记为DiagnosticSeverity.Error,这将导致构建中断: In the line declaring the Rule field,you can also update the severity of the diagnostics you’ll be producing to be er
继MS的 this教程之后,我为Roslyn创建了一个分析器.

根据该页面,您可以将规则标记为DiagnosticSeverity.Error,这将导致构建中断:

In the line declaring the Rule field,you can also update the severity of the diagnostics you’ll be producing to be errors rather than warnings. If the regex string doesn’t parse,the Match method will definitely throw an exception at run time,and you should block the build as you would for a C# compiler error. Change the rule’s severity to DiagnosticSeverity.Error:

internal static DiagnosticDescriptor Rule =
new DiagnosticDescriptor(DiagnosticId,Title,MessageFormat,
Category,DiagnosticSeverity.Error,isEnabledByDefault: true,description: Description);

在我的代码中,我已经或多或少地创建了规则,如下所示:

private static readonly DiagnosticDescriptor Rule = 
  new DiagnosticDescriptor(DiagnosticId,Category,true,helpLinkUri: HelpUrl);

这条规则很好.它会抛出红线,它会在错误列表中显示消息.但是,构建成功,我能够成功运行该应用程序.

注意:我已创建此规则以捕获此示例的Thread.Sleep.

是否需要额外的设置来确保规则中断构建?

解决方法

这是从VSIX文件运行的分析器的一项功能.

If the IDE-installed rules ran as part of the in-IDE build,it would result in IDE builds and command line builds having potentially very different outputs. For example,a user with code-cracker installed as a VSIX could end up filing a bug report that an open source project does not build due to an analyzer error (or perhaps a warning when the project uses /warnaserror). They would be forced to either uninstall the analyzer extension or modify the rule set used by the project to disable some rule that only exists on one developer’s machine.

In contrast,rules that are installed via NuGet become part of the project and part of the build. They run the same way across developer machines,and they run the same way in-IDE,on the command line,and in automated build environments.

Source: IDE rules don’t fail builds

为了使规则的构建失败,您需要将分析器作为nuget包添加到项目中.这将确保失败将导致构建按预期失败.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读