Contract class should be an abstract class

∥☆過路亽.° 提交于 2019-12-05 05:25:12

Section 2.8 of the code contracts manual specifically states that it should be an abstract class:

The tools expect that the contract class is abstract and implements the interface it is providing contracts for.

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:

[ContractClass(typeof(FooContracts))] 
public interface IFoo { 
  void Bar(string foo); 
} 

[ContractClassFor(typeof(IFoo))] 
internal abstract class FooContracts : IFoo { 
  void IFoo.Bar(string foo) { 
    Contract.Requires(foo != null); 
  } 
}

The contract class must be abstract.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!