代码契约不变未经证实..在每个方法的末尾与Contract.Assert一起使用

时间:2013-12-12 02:16:29

标签: code-contracts design-by-contract

我希望有人可以帮助我..

我有一个C#类,我在其中实现了Code Contracts不变量。我的方法不断抛出“不变的未经证实的”错误,除非我明确断言不变量是真的。

我怀疑问题来自使用void返回类型?

[ContractInvariantMethod]
private void FooInvariant()
{
    Contract.Invariant(!(Foo1.GetState() == "failed") || (Foo2.Get_State() == "off"));
}

//this method works fine
public void FooBar()
{
    Foo1.IncreaseInternalInt();
    Contract.Assert(!(Foo1.GetState() == "failed") || (Foo2.Get_State() == "off"));
}

//As does this method
public void FooBarTwo()
{
    Contract.Assert(!(Foo1.GetState() == "failed") || (Foo2.Get_State() == "off"));
    Foo1.IncreaseInternalInt();
}

//This method doesn't work
public void FooBarThree()
{
    Contract.Requires(!(Foo1.GetState() == "failed") || (Foo2.Get_State() == "off"));
    Foo1.IncreaseInternalInt();
}

//This method doesn't work either
public void FooBarFour()
{
    Contract.Ensures(!(Foo1.GetState() == "failed") || (Foo2.Get_State() == "off"));
    Foo1.IncreaseInternalInt();
}

我希望我没有太过愚蠢。

非常感谢任何帮助!

0 个答案:

没有答案