インターフェイスの実装に前提条件を追加できないことを理解しています。インターフェイスから見える要素のコントラクトを定義するコントラクト クラスを作成する必要があります。
しかし、次のケースでは、インターフェイス定義レベルでは不明な実装の内部状態にコントラクトを追加するにはどうすればよいでしょうか?
[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);
<...>
}
}