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)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Thanks Porges.
My mistake was that I actually used
ReleaseRequiresoption, 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
Requiresprecondition, 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
ReleaseRequirestoPreconditionsin the Code Contracts’ options and feel free to write invariants on auto-properties – the rewriter tool will automatically change them into theRequires. However, they will be non-generic – that means, in case of contract broken, aContractExceptionwill be thrown and there is no way to change this behaviour.Thanks everyone for the help!