在.NET Core项目上启用Microsoft的代码分析 [英] Enabling Microsoft's Code Analysis on .NET Core Projects
问题描述
如果我们忘记对方法参数进行空检查之类的操作,我们的团队将代码分析功能与自定义规则集一起使用会使我们的构建失败。
Our team uses the Code Analysis feature with a custom ruleset to cause our build to fail if we forget to do things like null checks on method arguments.
但是,现在,当我们创建一个新的.NET Core项目时,看起来代码分析似乎不是这些新项目的功能。在项目属性区域中没有适用于它的UI,并且按照此处的建议向项目中添加自定义规则集仅显示为影响StyleCop分析器( SAxxxx
规则)。
However, now as we create a new .NET Core project, it doesn't look like Code Analysis is a feature of these new projects. There is no UI for it in the Project Properties area, and adding a custom ruleset to the project as recommended here only appears to affect StyleCop Analyzers (the SAxxxx
rules).
是否有任何方法可以启用代码分析(<$ c $ .NET Core项目中的c> CAxxxx )规则?
Is there any way to enable Code Analysis (CAxxxx
) rules in a .NET Core project?
推荐答案
更新
显然,正确的方法是安装 Microsoft.CodeAnalysis。 FxCopAnalyzers NuGet程序包。即使在ASP.NET Core项目上,此方法也很好用,根本不需要< RunCodeAnalysis>
标志。
Update
Apparently the right way to do this is to install the Microsoft.CodeAnalysis.FxCopAnalyzers NuGet package. This works great, even on ASP.NET Core projects, and doesn't require the <RunCodeAnalysis>
flag at all.
我意识到csproj文件中还有另一个标签,实际上可以启用代码分析。我的.csproj文件中的< PropertyGroup>
标记现在看起来像这样:
I realized that there's another tag in the csproj file which actually enables code analysis. The <PropertyGroup>
tag in my .csproj file now looks like this:
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<CodeAnalysisRuleSet>..\MyCompanyCodeAnalysisRules.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
而且效果很好,至少在正常项目中如此。一个ASP.NET Core项目正在产生以下错误:
And it works great, at least on normal projects. An ASP.NET Core project is producing the following errors:
CA0055 : Could not identify platform for 'C:\Source\...\bin\Debug\netcoreapp1.1\....dll'.
CA0052 : No targets were selected.
这篇关于在.NET Core项目上启用Microsoft的代码分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!