Is there any way to put contracts on automatically implemented properties in .NET? (And how if the answer is 'Yes')?
(I assume using .NET code contracts from DevLabs)
Thanks Porges.
My mistake was that I actually used ReleaseRequires
option, which, indeed, deals only with generic version of the method, Requires<T>
.
Invariant which is put on an auto-implemented property is really turned into a Requires
precondition, but it's not generic - that's why it didn't work using this option.
What to do:
VARIANT 1. Consider using code snippets and lovely Requires<T>
instead of auto-implemented properties - that enables us to use exceptions of desired type.
VARIANT 2. Change the option ReleaseRequires
to Preconditions
in the Code Contracts' options and feel free to write invariants on auto-properties - the rewriter tool will automatically change them into the Requires
. However, they will be non-generic - that means, in case of contract broken, a ContractException
will be thrown and there is no way to change this behaviour.
Thanks everyone for the help!