在Visual Studio DNX项目(.xproj)中使用代码分析

编程入门 行业动态 更新时间:2024-10-28 20:31:45
本文介绍了在Visual Studio DNX项目(.xproj)中使用代码分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在普通 Visual Studio项目(.csproj)的属性中,可以说在构建中启用代码分析(以前称为FxCop)。

Within the properties of an "ordinary" Visual Studio project (.csproj) it's possible to say Enable Code Analysis on Build (formerly known as FxCop).

自从我开始使用新的DNX项目(.xproj)以来,我一直在寻找类似的东西。我知道可能没有构建输出,因此旧方法可能不太适合此方法,但是我很确定代码分析/ FxCop规则仍然适用。此外,应该有一些方法可以在新的实际项目文件(project.json)中注册自定义规则集(.ruleset)文件。

Since I've started playing around with new DNX projects (.xproj) I'm searching for something similar. I know there may be no build output, so the old approach may not really fit into this, but I'm quite sure the Code Analysis / FxCop rules still apply. Furthermore there should be some way to register a custom rule set (.ruleset) file within the new "actual" project file (project.json).

也许我在可以忽略基于Roslyn之类的更现代的东西?

Maybe I'm overlooking something more modern based on Roslyn or the like?

推荐答案

最后找到了一种解决方法,该解决方法应该一直执行到他们将(希望)在下一个版本的.NET Core和Visual Studio中对其进行修复。诀窍是对经典的.NET Framework构建执行好旧的 FxCop ,这对于运行老式的代码分析是必需的。

Finally found a workaround, which should do it until they'll (hopefully) fix it with the next release of .NET Core and Visual Studio. The trick is to execute the "good old" FxCop for the classic .NET Framework build, which is necessary to run an old fashioned code analysis.

用于库的 project.json 应该包含以下内容:

The project.json for libraries should contain something like that:

{ "frameworks": { "net46": { "buildOptions": { "define": [ "CODE_ANALYSIS" ] } }, "netstandard1.3": { "dependencies": { "NETStandard.Library": "1.6.0" } } }, "scripts": { "postcompile": "../../CodeAnalysis.cmd %compile:TargetFramework% %compile:Configuration% %compile:OutputFile% %compile:CompilerExitCode%" } }

apps的> project.json 应该包括实际运行时间:

The project.json for "apps" should include the actual runtime:

{ "scripts": { "postcompile": "../../CodeAnalysis.cmd %compile:TargetFramework% %compile:Configuration% %compile:OutputFile% %compile:CompilerExitCode% %compile:RuntimeOutputDir%" } }

因此,使用 postcompile 事件可以运行某种批处理脚本来执行经典的 FxCop (需要Visual Studio!)。我当前正在使用带有三个文件的设置:

Thus, using the postcompile event makes it possible to run some kind of batch script to execute the classic FxCop (Visual Studio required!). I'm currently using a setup with three files:

  • CodeAnalysis.cmd
  • CodeAnalysis.ruleset
  • CodeAnalysis.xml(词典)

批处理文件支持当前的.NET Framework 4.6版本,如下所示:

The batch file "supports" the current .NET Framework 4.6 versions and looks like this:

@echo off if not [%4]==[0] ( goto :eof ) if not [%2]==[Release] ( goto :eof ) if [%1]==[net46] ( set VERSION=v4.6 ) else if [%1]==[net461] ( set VERSION=v4.6.1 ) else if [%1]==[net462] ( set VERSION=v4.6.2 ) else ( goto :eof ) if not [%5]==[] ( set FILE=%5\%~nx3 ) else ( set FILE=%3 ) set PLATFORM=%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\%VERSION% set DIRECTORY=%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\%VERSION%\Facades set FXCOP=%VS140COMNTOOLS:Common7\Tools=Team Tools\Static Analysis Tools\FxCop%FxCopCmd.exe set RULES=%VS140COMNTOOLS:Common7\Tools=Team Tools\Static Analysis Tools\FxCop%Rules "%FXCOP%" /platform:"%PLATFORM%" /directory:"%DIRECTORY%" /rule:"-%RULES%" /ruleset:"=%~dp0CodeAnalysis.ruleset" /dictionary:"%~dp0CodeAnalysis.xml" /file:"%FILE%" /ignoregeneratedcode /console /culture:de-DE

它不像普通的内置东西那样方便,但是 FxCop 的错误/警告出现在使用 Visual Studio 时的错误列表(有时需要第二个版本)。但是,它们不会导致构建失败(也许还有另一个窍门...)。

It's not that handy like the ordinary built-in stuff, but the errors / warnings of FxCop appear within the Error List when using Visual Studio (sometimes a second build is necessary). They don't lead to a failed build though (maybe there's another trick...).

CodeAnalysis.ruleset示例:

<?xml version="1.0" encoding="utf-8"?> <RuleSet Name="All Rules, except a few ones" ToolsVersion="14.0"> <IncludeAll Action="Error" /> <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed"> <!-- CLS compliant --> <Rule Id="CA1014" Action="None" /> <!-- COM visibility --> <Rule Id="CA1017" Action="None" /> </Rules> </RuleSet>

更多推荐

在Visual Studio DNX项目(.xproj)中使用代码分析

本文发布于:2023-11-06 22:15:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   项目   Studio   Visual   xproj

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!