I'll best just show with a code example what I would like to accomplish?
class SomeClass
{
public int SomeProperty;
public void SomeOperation()
{
Contract.Ensures( "SomeProperty's value has not changed." );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// How can I write this post-condition?
}
};
(The string passed to Contract.Ensures()
is of course just a placeholder for the real post-condition expression.)
How can I do this? Would Contract.OldValue<>()
be of any use here?
NET 4.0. Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method.
Code contracts provide a way to specify preconditions, postconditions, and object invariants in . NET Framework code. Preconditions are requirements that must be met when entering a method or property.
Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.
Code contract classes let you specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits.
Contract.OldValue
should be enough:
Contract.Ensures(this.SomeProperty == Contract.OldValue(this.SomePropety));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With