问题标签 [fxcop]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
4076 浏览

c# - StyleCop 和 FxCop 规则相互排斥

我正在使用 StyleCop 和 FxCop 工具来改进我的代码,但我遇到了一个有两条规则的地方,一条在 StyleCop 中,一条在 FxCop 中,它们相互排斥。如果我修复我的代码以匹配来自 StyleCop 的规则,则 FxCop 验证失败,反之亦然。

第一条规则是来自 StyleCop 的 SA1200,它说所有 using 指令都必须放在命名空间内。

所有 using 指令都必须放在命名空间内。

所以我做了这样的事情

StyleCop 没问题,没有更多警告。现在我运行 FxCop 验证,它告诉我违反了 CA1014:

用 CLSCompliant(true) 标记“MyApp.dll”,因为它公开了外部可见的类型。

为了解决这个问题,我应该这样做:

但现在我无法构建我的项目,因为ClsCompliant无法识别属性(因为它来自System我包含在MyNamespace. 因此,如果我将using System; 指令移到MyNamespace声明之外。这将使我的代码编译,但它会再次违反 StyleCop 的规则。

除了禁用 StyleCop 或 FxCop 中的规则之一,还有什么办法可以解决这个问题?如果这不可能,我应该禁用哪些规则?哪个不太重要?

0 投票
2 回答
2453 浏览

c# - 当属性 getter/setter 需要 LinkDemand 时,我如何安抚 FxCop?

我在类 Bar 上有一个属性 Foo:

两者GetFooSetFoo装饰有:

结果,FxCop 正确地抱怨 Foo 属性(或者更确切地说它的隐式 getter 和 setter 方法)没有相同的 LinkDemand:

CA2122:Microsoft.Security:“Bar.Foo.get()”调用具有 LinkDemand 的“Bar.GetFoo()”。通过进行此调用,“Bar.GetFoo()”间接暴露给用户代码。查看以下可能公开绕过安全保护的方法的调用堆栈:

但是,当我尝试将相同SecurityPermission的属性应用于属性以修复此警告时,结果证明属性不是该属性的有效目标。

如何正确修复此 FxCop 警告?


编辑:回应 Eric Lippert 的评论“为什么是 LinkDemand”?

  1. 我使用 Marshal.GetIUnknownForObject 编写了一个函数,该函数具有 LinkDemand 非托管代码权限。
  2. 我跑了 FxCop,它抱怨 CA2122
  3. 我在 CA2122 上搜索了有关错误含义以及如何解决它的提示
  4. 在第一个谷歌点击中,我看到了 Micheal Fanning 使用 LinkDemand 解决错误的建议

在阅读了您似乎质疑我的理智的反应后,我很快猜到范宁的建议不适用于我的情况。我现在查看了Demand 与 LinkDemand文章,并将尝试改用 Demand。

0 投票
2 回答
78 浏览

fxcop - Why don't analysis tools apply refactorings?

I am using fxCop and NDepend a lot at the moment, and I keep seeing the items their reports generate which are "wrong"1 and wondering to myself, why can't these tools just go and make those fixes they are suggesting?

I get some are very hard to work out, but something like the fields should be marked readonly can very easily be applied with the information the tool has. However for me it means going to the tool, finding the item then placing the code in etc... Takes considerable time even for the smallest items.

I would even be happy if I had to confirm each change, in a similar to how CodeRush Xpress does with some it's refactorings.

So is there a reason why these tools do not do this?


1 Wrong is relative here, since something like the 1700 class of fxCop errors which are all about naming aren't bad code, but do make it harder for new developers to grapple the code.

0 投票
2 回答
3531 浏览

c# - How to specify CodeAnalysisRules in MSBuild via commandline

I want to be able to specify the Code AnalysisRules in commandline MSBuild (for Code Analysis / FXCOP). The project file would have something like this in it:

<CodeAnalysisRules>-Microsoft.Globalization#CA1301;-Microsoft.Globalization#CA1302</CodeAnalysisRules>

So I would assume that I use something like this:

MSBuild.exe /property:RunCodeAnalysis=true /property:CodeAnalysisRules=-Microsoft.Globalization#CA1301

Which works fine, but when I want to add another rule, it does not like the semi colon:

MSBuild.exe /property:RunCodeAnalysis=true /property:CodeAnalysisRules=-Microsoft.Globalization#CA1301;-Microsoft.Globalization#CA1302

MSBUILD : error MSB1006: Property is not valid. Switch: -Microsoft.Globalization#CA1302

How can I specify more than one rule?

I am happy to reference a file, but I don't want to just change the project files.

Backgroud: I want to create a set of rules for a continuous integration server (Hudson in my case).

Note: I am running Visual Studio 2005

0 投票
2 回答
907 浏览

visual-studio - 直接通过VS工程文件运行FxCop

我有兴趣将 FxCop 设置为在开发环境中的每个 VS .csproj 文件上运行,方法是在 .csproj 文件本身中将其作为目标调用,而无需为项目创建单独的 MSBuild 文件。这是

有可能以一种或另一种方式工作吗?包含在我的 .csproj 文件的末尾?

0 投票
2 回答
634 浏览

.net - 通过 FxCop 规则文件配置团队系统代码分析

是否有在 Visual Studio Team System 中配置代码分析规则以匹配 FxCop 配置文件中的规则并自动保持同步?

并非团队中的所有开发人员都有 TS,因此需要将我们当前运行的规则保留在 FxCop 文件中,这样每个人都可以运行相同的集合,但如果能够在 IDE 中运行它们,那就太好了。我们正在向现有项目引入静态分析,因此现在打开所有内容并不是一个有用的选项。(我们没有使用 Foundation Server 进行源代码控制,如果这有什么不同的话。)

0 投票
3 回答
2263 浏览

.net - FxCop 命令行和带空格的路径

当我指定带有空格的路径时,我无法让FxCop命令行正常工作。是的,我正确使用了引号。

FxCop 抱怨传递给 /r 开关的路径。我根据 FxCop 指南引用路径。当我通过 /d 开关传递路径时也会发生这种情况。有没有其他人有类似的问题?我使用 cmd 还是PowerShell都没关系。

0 投票
1 回答
528 浏览

vb.net - 自动生成项目时如何在源中排除 FxCop?

例如:

显示消息“不要在成员签名中嵌套泛型类型”。在 CurrentChangedEvent 上,它是保存事件委托的变量,VB 自动生成它。

如何禁止该项目的规则?

0 投票
1 回答
234 浏览

c# - 用 fxcop 看不到我的自定义规则

我正在尝试为 fxcop 开发自定义规则。

我有这个代码:

我有这个 xml(命名为 Rules.xml 并将构建操作设置为嵌入式资源)

我用这个 dll 编译、运行 fxcop 并添加规则。

我可以看到“我的规则”文件夹,但其中没有规则......

我错过了什么?

感谢您的帮助,最好的问候

0 投票
4 回答
959 浏览

c# - 单元测试自定义 FxCop 规则

我想测试我的自定义 fxrules。

我看过这篇文章:http ://weblogs.asp.net/rosherove/archive/2007/02/24/writing-real-unit-tests-for-your-custom-fxcop-rules.aspx

但它不适用于最新版本的 fxcop。Microsoft.Cci.Method.GetMethod 不存在,我找不到替代方法。

你知道如何获取 Microsoft.FxCop.Sdk.Method 对象吗?

提前感谢您的帮助。最好的祝福,