基本上,我有一个虚拟方法可以将某些强制性后置条件传播到子类。这是一个简化版本和静态检查器生成的奇怪警告(编辑 - 我的示例不完整。现在):
public abstract class InitializerClass
{
protected bool _initialized
public bool IsInitialized
{
get { return _initialized; }
}
public virtual void Initialize()
{
//Warning CodeContracts: Missing precondition in an externally visible
//method. Consider adding Contract.Requires(this.IsInitialized); for
//parameter validation
Contract.Ensures(IsInitialized);
}
}
这是另一类:
public abstract class OrderingClass
{
protected bool _ordered
public bool IsOrdered
{
get { return _ordered; }
}
public override void Initialize()
{
//Message CodeContracts: Suggested assume: Contract.Assume(this.IsOrdered);
Contract.Ensures(IsOrdered);
}
}
事实上,这两个警告都指向方法的右花括号,在 Contract.Ensure 调用下面的行中。我的代码有什么问题?