能让代码分析理解代码契约吗?
结合使用代码分析和代码契约时,我收到很多警告,例如
CA1062< /a>: Microsoft.Design :在外部可见的方法“Foo.Bar(Log)”中,在使用参数“log”之前验证它。
在 Foo.Bar 中,我有一个验证 log
的合约。
public Bar(Log log)
{
Contract.Requires(log != null);
log.Lines.Add(...);
// ...
}
有没有办法让 FxCop 理解代码契约?
When using Code Analysis and Code Contracts in combination, I get a lot of warnings like
CA1062: Microsoft.Design : In externally visible method 'Foo.Bar(Log)', validate parameter 'log' before using it.
In Foo.Bar, I have a contract that validates log
.
public Bar(Log log)
{
Contract.Requires(log != null);
log.Lines.Add(...);
// ...
}
Is there a way to make FxCop understand code contracts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,我认为在当前版本中这是不可能的,因为合约重写器生成的代码不会产生 FxCop 正在寻找的标准模式。
通常,我在使用代码合约时会禁用此特定的 FxCop 规则。我发现静态验证器足以弥补这条规则的损失,因为它会比 FxCop 更积极地抱怨缺乏检查。我建议您采用相同的方法来解决这个问题。
No I do not think it's possible in the current build as the code generated by the contracts rewriter does not produce the standard pattern that FxCop is looking for.
Typically though I disable this particular FxCop rule when using code contracts. I find the static verifier more than makes up for the loss of this rule as it will yell about a lack of checking much more aggressively than FxCop. I would suggest the same approach here which will fix this problem for you.
是的,正如我的回答中所述,从版本 4.5.2 开始框架(可能是 4.5)可以告知正在执行的代码合同的代码分析。扩展方法和标记属性类必须如下定义:
其他详细信息在我的其他答案中。
Yes, as noted in my answer here, as of version 4.5.2 of the framework (possibly 4.5) it is possible to inform Code Analysis of the Code Contracts being enforced. An extension method and a marker attribute class must be defined like this:
Additional details are in my other answer.
像这样指定 ArgumentNullException 异常:
Fxcop 希望抛出 ArgumentNullException 异常...
Specify the ArgumentNullException exception like this:
Fxcop expects to to throws the ArgumentNullException exception...