能让代码分析理解代码契约吗?

发布于 2024-09-03 12:55:29 字数 399 浏览 8 评论 0原文

结合使用代码分析和代码契约时,我收到很多警告,例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分分钟 2024-09-10 12:55:29

不,我认为在当前版本中这是不可能的,因为合约重写器生成的代码不会产生 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.

征﹌骨岁月お 2024-09-10 12:55:29

是的,正如我的回答中所述,从版本 4.5.2 开始框架(可能是 4.5)可以告知正在执行的代码合同的代码分析。扩展方法和标记属性类必须如下定义:

  public static class ContractExtensions {
    /// <summary>Throws <c>ContractException{name}</c> if <c>value</c> is null.</summary>
    /// <param name="value">Value to be tested.</param>
    /// <param name="name">Name of the parameter being tested, for use in the exception thrown.</param>
    [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value")]
    [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "name")]
    [ContractAbbreviator] // Requires Assemble Mode = Standard Contract Requires
    public static void ContractedNotNull<T>([ValidatedNotNull]this T value, string name) where T : class {
      Contract.Requires(value != null,name);
    }
  }

/// <summary>Decorator for an incoming parameter that is contractually enforced as NotNull.</summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class ValidatedNotNullAttribute : global::System.Attribute {}

其他详细信息在我的其他答案中。

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:

  public static class ContractExtensions {
    /// <summary>Throws <c>ContractException{name}</c> if <c>value</c> is null.</summary>
    /// <param name="value">Value to be tested.</param>
    /// <param name="name">Name of the parameter being tested, for use in the exception thrown.</param>
    [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value")]
    [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "name")]
    [ContractAbbreviator] // Requires Assemble Mode = Standard Contract Requires
    public static void ContractedNotNull<T>([ValidatedNotNull]this T value, string name) where T : class {
      Contract.Requires(value != null,name);
    }
  }

/// <summary>Decorator for an incoming parameter that is contractually enforced as NotNull.</summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class ValidatedNotNullAttribute : global::System.Attribute {}

Additional details are in my other answer.

鸵鸟症 2024-09-10 12:55:29

像这样指定 ArgumentNullException 异常:

public Bar(Log log)
{
   Contract.Requires<ArgumentNullException>(log != null);
   log.Lines.Add(...);
   // ...
}

Fxcop 希望抛出 ArgumentNullException 异常...

Specify the ArgumentNullException exception like this:

public Bar(Log log)
{
   Contract.Requires<ArgumentNullException>(log != null);
   log.Lines.Add(...);
   // ...
}

Fxcop expects to to throws the ArgumentNullException exception...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文