合约类应该是一个抽象类
以下代码向我发出警告Contract 类“FooContracts”应该是一个抽象类
。从我在线阅读的所有示例中(例如 http://www.infoq.com/ articles/code-contracts-csharp),这应该可以工作(大概没有编译器警告)。
[ContractClass(typeof(FooContracts))]
public interface IFoo {
void Bar(string foo);
}
[ContractClassFor(typeof(IFoo))]
internal sealed class FooContracts : IFoo {
void IFoo.Bar(string foo) {
Contract.Requires(foo != null);
}
}
我使用的是 Visual Studio 2010,在项目属性的Code Contracts
部分中进行了以下设置:
- 执行运行时合约检查(设置为
Full
) - 执行静态合约检查(在
静态检查
下) - 在后台检查
我还定义了CONTRACTS_FULL
编译符号以使ReSharper关闭。
我是否缺少一些东西来使这个编译没有警告?
The following code gives me the warning Contract class 'FooContracts' should be an abstract class
. From all the examples I've read online (e.g. http://www.infoq.com/articles/code-contracts-csharp), this should work (presumably without compiler warnings).
[ContractClass(typeof(FooContracts))]
public interface IFoo {
void Bar(string foo);
}
[ContractClassFor(typeof(IFoo))]
internal sealed class FooContracts : IFoo {
void IFoo.Bar(string foo) {
Contract.Requires(foo != null);
}
}
I'm in Visual Studio 2010, with the following settings in the Code Contracts
section of the project's properties:
- Perform Runtime Contract Checking (set to
Full
) - Perform Static Contract Checking (under
Static Checking
) - Check in Background
I also defined the CONTRACTS_FULL
compilation symbol to make ReSharper shut up.
Am I missing something to make this compile without warnings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码合同手册第 2.8 节明确指出,应该一个抽象类:
Section 2.8 of the code contracts manual specifically states that it should be an abstract class:
您引用的 InfoQ 文章很可能是不正确的。它基于 C# in Depth 的“早期访问”版本,因此从最初编写章节/文章到 .NET 4 发布之间,代码契约实现可能发生了变化。
以下代码应该可以工作:
合约类必须是抽象的。
Most likely the InfoQ article you are referencing is incorrect. It's based on an "early access" edition of C# in Depth, so the code contracts implementation probably changed between the time the chapter/article was originally written and .NET 4 was released.
The following code should work:
The contract class must be abstract.