我正在使用 MS Code Contracts,并且在使用接口继承和 ContractClassFor 属性时遇到了麻烦。
给定这些接口和合约类:
[ContractClass(typeof(IOneContract))]
interface IOne { }
[ContractClass(typeof(ITwoContract))]
interface ITwo : IOne { }
[ContractClassFor(typeof(IOne))]
abstract class IOneContract : IOne { }
[ContractClassFor(typeof(ITwo))]
abstract class ITwoContract : IOneContract, ITwo { }
假设 IOne 和 ITwo 是实体接口。因此 IOneContract 将包含大量代码以进行必要的检查。
我不想在 IOne 接口的 ITwoContract 中复制所有这些内容。我只想为 Itwo 接口添加新合同。从另一个合同类继承一个合同类似乎是重用该代码的可能方式。但是我收到以下错误:
EXEC : warning CC1066: Class 'ITwoContract' is annotated as being the contract for the interface 'ITwo' and cannot have an explicit base class other than System.Object.
这是代码合同的限制还是我做错了?我们的项目中有很多接口继承,如果我不知道如何解决这个问题,这感觉就像代码合同的交易破坏者。