code-contracts

How to use Code Contracts with query?

Hi...I am fairly new to Code Contracts...and I ran into a problem. I have in a method LINQ query that go something like this: MyClass[] fields = (from p in rType.GetProperties() where p.CanRead let fAttr = p.GetCustomAttributes(typeof(MyClassAttribute), true).SingleOrDefault() as MyClassAttribute ...

Enforcing the correct implementation of INotifyPropertyChanged with CodeContracts - "requires unproven"

I'm looking for an easy way to enforce the correct implementation of INotifyPropertyChanged i.e. when PropertyChanged is raised it must reference a property that is actually defined. I tried doing this with the new CodeContract tools from Microsoft, but I keep getting the warning "CodeContracts: requires unproven". Here is my code... pu...

Code Contracts and type conversion

Hello. I've tried to embrace Microsoft DevLabs Code Contracts static analyzer and faced situation when I do not actually know is it me or is it them. So here is the code: public static int GenerateInBetween(int min, int max) { Contract.Requires(min < max); Contract.Requires((long)(max - min) <= (long)(Int32.MaxVa...

Any alternatives to the .Net 4 Code Contracts static analyser?

It seems that the static analyser for use with the .NET 4.0 Code Contracts is only going to be available for Team Suite editions of Visual Studio - this puts it well outside the budget for my team. Are there any alternatives (open source, free or reasonably priced) which offer similar static analysis for design by contract style code (n...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;? ...

How can I use Code Contracts in a C++/CLI project?

I recently stumbled upon Code Contracts and have started using them in my C# projects. However, I also have a number of projects written in C++/CLI. For C# and VB, Code Contracts offer a handy configuration panel in the project properties dialog. For a C++/CLI project, there is no such panel. From the documentation, I got the impressio...

Unit Testing interface contracts in C#

Using the Code Contracts tools available in VS2010 Beta 2, I have defined an interface, a contract class for that interface and two classes that implement the interface. Now when I come to test the code, I want to test the implementation classes so I know that their functionality is correct and I want to test the contract code so I know...

Code Contracts causing exception in VS2010 Beta 2

Running XP-sp2 with VS2010 Beta 2 in a VPC and when I installed Code Contracts from devLabs, the it conflicts with Contracts within mscorlib. However, if I do not install the devLabs install, the Code Contracts tab on the Project Projects page does not appear. Here is the error that is generated. Error 1 The command ""C:\Program File...

Is this a bug in the static contract checker?

If I write this: public sealed class Foo { private int count; private object owner; private void Bar() { Contract.Requires(count > 0); Contract.Ensures(owner == null || count > 0); if (count == 1) owner = null; --count; } } The static contract checker can prove all asser...

Why is this C# Code Contract malformed?

Visual Studio shows an error when I write this contract below. Error 20 Malformed contract section in method '....get_Page' Is the problem with the 'if' block? public int? Page { get { int? result = Contract.Result<int?>(); if (result != null) Contract.Ensures(result >= 0); return default(int?); } } EDIT: Lass...

What do I need to install to make code contracts work with vs 2010

I have vs 2008 and vs 2010 installed on my machine as well as the code contract from devlabs (version 1.2.21023.14). It works fine with vs 2008. However, with vs 2010, there is no "code contract" tab. Is there something I should install? ...

C#: Code Contracts vs. normal parameter validation

Hi, consider the following two pieces of code: public static Time Parse(string value) { string regXExpres = "^([0-9]|[0-1][0-9]|2[0-3]):([0-9]|[0-5][0-9])$|^24:(0|00)$"; Contract.Requires(value != null); Contract.Requires(new Regex(regXExpres).IsMatch(value)); string[] tokens = value.S...

CodeContracts metadata errors during rewrite

I'm trying to use code contracts with .NET 3.5 in a large desktop application project, which also have a mixed mode C++ DLL dependency, written in old managed C++ syntax. The real solution consists of at least 20 projects, but lets assume that there is only A, B, C, and D projects, and the mixed mode M project. C depends on A, B and M. ...

Code Contracts: Is it possible to supply a contract class for a generic interface?

I have just got started using Microsoft's Code Contracts and already ran into a problem that I can't solve. Let's take this interface for which I'd like to specify a contract: public interface IRandomWriteAccessible<T> { T this[uint index] { set; } uint Length { get; } } The documentation says to use the ContractClass attribu...

Code Contracts: How do I state in a post-condition that a field/property's value has not changed?

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 t...

Getting Code Contracts to work in Visual Studio 2010

I have the following code: class Program { static void Main(string[] args) { Console.WriteLine(SqrtRoot(0)); Console.WriteLine(SqrtRoot(10)); Console.WriteLine(SqrtRoot(-10)); Console.ReadKey(); } public static int SqrtRoot(int i) { Contract.Requires(i >= 0); return (i...

Code Contract's properties in Visual Studio 2010 Pro RC

So it seems there should be a "Code Contracts" tab in Visual Studio 2010's properties. I can't find it anywhere. Maybe it's a problem with my Visual Studio installation? I am running the RC version. This is what I have: Anyone knows what might be wrong? Maybe I am looking in the wrong place? Thanks ...

Code Contracts with Interfaces: "Method Invocation skipped. Compiler will generate method invocation because the method is conditional... [...]"

Good evening, I just started playing with Microsoft.Contracts (latest version) and plugging it on top of a sample interface and right now it looks like this: namespace iRMA2.Core.Interfaces { using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Diagnostics.Contracts; ...

Getting up and running with code contracts

Hi, In VS2010 and .NET 4.0, I see the shortcuts in intellisense for adding contracts to my code (Eg cr, crr) but when I tab to add these in, the code (Such as Contract.Requires) does not have the valid assembly so there is no intellisense (The type can't be found basically). How exactly do I get up and running with code contracts? ED...

Code Contracts: Do we have to specify Contract.Requires(...) statements redundantly in delegating methods?

I'm intending to use the new .NET 4 Code Contracts feature for future development. This made me wonder if we have to specify equivalent Contract.Requires(...) statements redundantly in a chain of methods. I think a code example is worth a thousand words: public bool CrushGodzilla(string weapon, int velocity) { Contract....