Bug 8400 - Support for invoking Code Contracts tools
Summary: Support for invoking Code Contracts tools
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Project Model ()
Version: unspecified
Hardware: Macintosh Mac OS
: Low enhancement
Target Milestone: ---
Assignee: Lluis Sanchez
Depends on:
Reported: 2012-11-14 13:31 UTC by Martin Stafford
Modified: 2017-02-03 06:11 UTC (History)
12 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Martin Stafford 2012-11-14 13:31:08 UTC
System.Diagnostic.Contracts.Requires<T> have been implemented, but when called at runtime a 'not implemented' exception is thrown.

Visual Studio has Code Contracts configuration in Project Properties that allow selection of Runtime checking.  If not selected the resulting project output will not contain code contracts.  If the same project is compiled in MonoDevelop the output contains code that will throw a 'not implemented' exception when calling Contracts.Required<T>.

MonoDevelop always seems to produce code that contains Code Contracts but, when called, produce a 'not implemented' exception.

Is there any way that projects in MonoDevelop can be configured to provide Runtime checking in Contracts rather than the 'not implemented' exception, or, if not, can the compile filter out Code Contracts so that the code will run.
Comment 1 Marek Safar 2012-11-14 16:58:15 UTC
IIRC you need to instruct ccrewrite tool to re-write the assembly. Moving to MD for now to get things started.
Comment 2 Mikayla Hutchinson [MSFT] 2012-11-14 19:33:05 UTC
MonoDevelop doesn't have support for invoking ccrewrite or editing the settings that affect it, and we have no ETA for adding this support.

If you need it sooner, you could use a custom postbuild task in your project, or contribute a patch to MonoDevelop to add this functionality in a VS-compatible way.
Comment 3 Nick 2013-05-21 07:57:30 UTC
Did you find a workaround for this? Or is support for this planned anytime soon?
Comment 4 Stefan Fisk 2013-12-02 09:13:14 UTC
I'd really like to be able to do this as well. Any hint on how to approach resolving this would be much appreciated.
Comment 5 Keith Robertson 2014-11-07 12:46:20 UTC
If Requires<T> were marked with [Conditional("CONTRACTS_FULL")], then it could appear (along with the non-generic forms) without forcing you to use ccrewrite, simply by NOT defining that compiler conditional. I submitted a bug and pull request for this 18 months ago, but it was rejected.

We have projects with code intended to be built in VS for Windows and in Xamarin for Mac. We use Contracts for documentation and runtime checking on Windows, and want to simply not use it when building for the Mac. Because the two Requires<T> methods are not marked with the conditional attribute (as they should be), they generate code when building on Xamarin, requiring ccrewrite, which doesn't run.

Our workaround was to bracket our Contract.Requires<Exc>() usages with #if __MonoCS__. We then use different project files (but the same source files) for Xamarin for the Mono/Mac projects, and we define __MonoCS__ within those project files. Unfortunately, this means keeping two project files up-to-date.
Comment 6 Geoffrey Huntley 2015-01-11 17:40:49 UTC
I'd really love to use Code Contracts w/ Xamarin  as well and would retire https://github.com/ghuntley/conditions in a heart beat. Being able to check at compile time and define contracts on interface instead of individual concrete implementations is just so powerful, relaxing and makes maintenance so much easier.
Comment 7 Geoffrey Huntley 2015-01-11 19:39:56 UTC
Got a tip-off by John Galloway that as of a couple days ago CodeContracts has been released by Microsoft under the MIT license and is available at https://github.com/CodeContractsDotNet/CodeContracts . Hopefully that will aid Xamarin with implementing the missing tooling support.
Comment 8 Jason DiOrio 2015-01-13 17:44:16 UTC
I am also looking for this functionality so mac-based Xamarin Studio developers are still abiding by the same contracts the devs in Visual Studio do.
Comment 9 Mikayla Hutchinson [MSFT] 2015-02-05 11:48:21 UTC
Closing for now, since Bugzilla isn't the place to track feature requests. Please vote for this on http://xamarin.uservoice.com if it's important to you.