我正在尝试使用代码契约,但遇到了一个阻碍我的问题。将“合同引用程序集”设置为“生成”时,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?
发布评论
评论(2)
我假设 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.
重写器会查看引用程序集的方法体以提取契约(C# 编译器从不这样做)。因此,重写器通常会比 C# 追逐更多的依赖项,这就是您遇到的问题。
有两种方法可以解决这个问题。
-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.
-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.