code-contracts

Code Contracts: Why are some invariants not considered outside the class?

天大地大妈咪最大 提交于 2019-12-03 11:14:05
Consider this immutable type: public class Settings { public string Path { get; private set; } [ContractInvariantMethod] private void ObjectInvariants() { Contract.Invariant(Path != null); } public Settings(string path) { Contract.Requires(path != null); Path = path; } } Two things to notice here: There is a contract invariant which ensures the Path property can never be null The constructor checks the path argument value to respect the previous contract invariant At this point, a Setting instance can never have a null Path property. Now, look at this type: public class Program { private

What is the best alternative for Code Contracts in Visual Studio 2015?

。_饼干妹妹 提交于 2019-12-03 10:54:45
I am looking to validate method parameters in my code, in the most elegant fashion possible. Code Contracts don't seem to work in 2015. Does anyone use any alternatives? They're working on it: https://github.com/Microsoft/CodeContracts/pull/36 If you cannot wait, there are a few things that you can do to fix this: https://github.com/Microsoft/CodeContracts/issues/18 "The contract editor extensions now have a single VSIX package for all of the Visual Studio versions includign VS2015!" https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970 I have installed

Code Contracts and Asynchrony

给你一囗甜甜゛ 提交于 2019-12-03 10:07:47
What is the recommended way for adding postconditions to async methods which return Task<T> ? I have read the following suggestion: http://social.msdn.microsoft.com/Forums/hu-HU/async/thread/52fc521c-473e-4bb2-a666-6c97a4dd3a39 The post suggests implementing each method as synchronous, contracting it, and then implementing an async counterpart as a simple wrapper. Unfortunately I don't see this as a workable solution (perhaps through my own misunderstanding): The async method, although assumed to be a wrapper for the sync method, is left without any real code contract and can therefore do as

Microsoft Code Contracts without Visual Studio

一个人想着一个人 提交于 2019-12-03 09:45:11
问题 This stack overflow question: Microsoft Code Contracts and CI build server asks how to get code contracts working on a build server without installing Visual Studio 2010. We're trying to do the same. We've followed the steps outlined in the accepted answer, but haven't been able to get it working. CodeContracts will not install on the build server unless Visual Studio is present. So following the suggestion, we've done the following: We copied the contents of %programfiles%\Microsoft

How to properly use Code Contracts in .NET Core

孤者浪人 提交于 2019-12-03 06:42:46
问题 I wonder, how to properly use Code Contracts in .NET Core , so far I tried to add CC to my project, compile and debug. I'm confused by message, which is appearing in each call which uses Contract.Requires , and information found by googling. The message states: An assembly must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires<TException> and CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol

Usefulness of System.Diagnostics.Contracts in question

梦想与她 提交于 2019-12-03 04:49:55
问题 I've been playing with the new System.Diagnostics.Contracts class because it seemed very useful at first. Static methods to check inbound arguments, return values, etc. It was a clean interface and could replace lots of if-then statements and internally built library tools. However, it seems less than useful in most runtime situations. From what I can tell, it doesn't throw an error so I can't catch anything to know if a contract failed. It pops up a dialog box with the error. If I'm running

Code contracts build reference assembly actions

不羁的心 提交于 2019-12-03 04:45:53
问题 I am using code contracts and trying to understand which of the build options shall I use and when. The contract assembly build options are defined in project properties Code Contracts -> Contract Reference Assembly : None Build DoNotBuild Any thoughts or recommendations? 回答1: Yes, the None and DoNotBuild options seem a bit strange. If you select None and reference the library in a Project with contracts, you will get a Warning. If you select DoNotBuild you won't get a warning. And of course

.NET exception caught is unexpectedly null

五迷三道 提交于 2019-12-03 04:15:35
See below for an explanation of what is going on I have a really weird issue where the exception caught is null. The code uses MEF and tries hard to report composition errors. Using the debugger I can see the exception being thrown (an InvalidOperationException ) but when it is caught by the last catch block in the code below the ex variable is null. This is true both in the debugger and when executing the code normally. static T ResolveWithErrorHandling<T>() where T : class { try { IocContainer.Compose(Settings.Default.IocConfiguration); return IocContainer.Resolve<T>(); } catch

Are code contracts guaranteed to be evaluated before chained constructors are called?

孤街醉人 提交于 2019-12-03 03:09:45
Before I started using Code Contracts I sometimes ran into fiddlyness relating to parameter validation when using constructor chaining. This is easiest to explain with a (contrived) example: class Test { public Test(int i) { if (i == 0) throw new ArgumentOutOfRangeException("i", i, "i can't be 0"); } public Test(string s): this(int.Parse(s)) { if (s == null) throw new ArgumentNullException("s"); } } I want the Test(string) constructor to chain the Test(int) constructor, and to do so I use int.Parse() . Of course, int.Parse() doesn't like having a null argument, so if s is null it will throw

Debug.Assert vs Code Contract usage

亡梦爱人 提交于 2019-12-03 03:01:24
问题 When should I debug.assert over code contracts or vice versa? I want to check precondition for a method and I am confused to choose one over the other. I have unit tests where I want to test failure scenarios and expect exceptions. Is it a good practice to use Debug.Assert and Code contract on the same method. If so what would be the order in which the code should be written? Debug.Assert(parameter!= null); Contract.Requires<ArgumentNullException>(parameter != null, "parameter"); or Contract