【发布时间】:2009-09-06 11:48:41
【问题描述】:
我了解我无法在接口实现上添加先决条件。我必须创建一个合同类,在其中定义界面所见元素的合同。
但是在以下情况下,如何在接口定义级别不知道的实现的内部状态上添加契约?
[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);
<...>
}
}
【问题讨论】: