使用代码契约,为什么 ccrewrite 需要访问每个运行时依赖项?

发布于 2024-11-18 20:32:11 字数 158 浏览 3 评论 0 原文

我正在尝试使用代码契约,但遇到了一个阻碍我的问题。将“合同引用程序集”设置为“生成”时,ccrewrite 在尝试访问由直接引用的程序集间接引用的程序集时会出错。构建解决方案不需要这些间接程序集,所以我想知道为什么代码契约需要它们?另外,有没有办法解决这个问题,而不必在构建过程中提供所有运行时依赖项?

I'm trying to use Code Contracts and I'm running into a problem that is blocking me. With Contract Reference Assembly set to Build, ccrewrite is erroring while trying to access assemblies that are referenced indirectly by assemblies that are referenced directly. These indirect assemblies are not needed to build the solution, so I'm wondering why they're required by Code Contracts? Also, is there a way to work around this problem without having to provide all runtime dependencies as part of the build?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

野生奥特曼 2024-11-25 20:32:11

我假设 ccrewrite 正在尝试遍历依赖关系链来分析它的前置/后置条件等。如果程序集由您依次引用的程序集引用,那么您的程序将需要它们运行,因此 ccrewrite 只是在实际运行程序之前执行正常分析。

这是基于使用 JML;我自己才刚刚开始研究 .NET 代码契约。但我相信这两种工具的运作原理大致相同。

I assume ccrewrite is trying to walk the dependency chain to analyze it for pre/postconditions, etc.. If the assemblies are referenced by assemblies which you in turn reference, then they would be required for your program to run, so ccrewrite is just performing normal analysis before you actually run the program.

That's based on using JML; I've only just started looking at the .NET Code Contracts myself. But I believe both tools operate on roughly the same principles.

月亮是我掰弯的 2024-11-25 20:32:11

重写器会查看引用程序集的方法体以提取契约(C# 编译器从不这样做)。因此,重写器通常会比 C# 追逐更多的依赖项,这就是您遇到的问题。

有两种方法可以解决这个问题。

  1. 添加额外的路径到可以找到所需库的目录(在合约库路径选项中)。这是首选方法
  2. 作为最后的手段,您可以将选项 -ignoreMetadataErrors 添加到运行时合约选项中。请注意,这很危险。如果重写器确实需要引用代码的某些方面才能创建正确的 IL,则最终可能会得到不正确的 IL。为了防止这种情况,请对结果位使用 peverify。

希望这有帮助。

The rewriter looks into method bodies of referenced assemblies in order to extract contracts (the C# compiler never does that). As a result, the rewriter often chases more dependencies than C# which is the problem you ran into.

There are two ways to address this.

  1. add extra paths to directories where the desired libraries can be found (in the contract library paths options). This it the preferred method
  2. As a last resort, you can add the option -ignoreMetadataErrors to the runtime contract options. Note that this is dangerous. In the case that the rewriter truly needs some aspect of the referenced code in order to create proper IL, you might end up with incorrect IL. To guard against this, use peverify on the resulting bits.

Hope this helps.

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