我最好只用一个代码示例来展示我想要实现的目标?
class SomeClass { public int SomeProperty; public void SomeOperation() { Contract.Ensures( "SomeProperty's value has not changed." ); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // How can I write this post-condition? } };
(传递给Contract.Ensures()的字符串当然只是实际后置条件表达式的占位符.)
我怎样才能做到这一点? Contract.OldValue<>()在这里有用吗?
解决方法
Contract.OldValue
应该足够了:
Contract.Ensures(this.SomeProperty == Contract.OldValue(this.SomePropety));