我了解我无法在接口实现上添加先决条件。我必须创建一个合同类,在其中定义界面可见的元素的合同。
但是在以下情况下,如何在实现的内部状态上添加合同,因此在接口定义级别上是未知的?
[ContractClass(typeof(IFooContract))]
interface IFoo
{
void Do(IBar bar);
}
[ContractClassFor(typeof(IFoo))]
sealed class IFooContract : IFoo
{
void IFoo.Do(IBar bar)
{
Contract.Require (bar != null);
// ERROR: unknown property
//Contract.Require (MyState != null);
}
}
class Foo : IFoo
{
// The internal state that must not be null when Do(bar) is called.
public object MyState { get; set; }
void IFoo.Do(IBar bar)
{
// ERROR: cannot add precondition
//Contract.Require (MyState != null);
<...>
}
}