code-contracts

.NET 4 Code Contracts: “requires unproven: source != null”

╄→尐↘猪︶ㄣ 提交于 2019-12-05 10:10:21
I just started using code contracts in my project. However, I have a problem with my repository implementation, which queries my database using the Entity Framework. I have the following method: public IEnumerable<Organization> GetAllOrganizations() { return _uow.CreateSet<Party>().OfType<Organization>().AsEnumerable(); } The method returns a collection containing all organizations in the database, or an empty collection there's not organizations in the database. However, this is not okay according to CodeContracts, which give me the error: "requires unproven: source != null" What is it trying

Code Contracts doesn't seem to work on VS2012

天涯浪子 提交于 2019-12-05 09:54:12
问题 I'm reading up on Code Contracts, which at first glance seem to be pretty revolutionary, but I can't seem to get them working. I'm running Windows 8 and Visual Studio 2012 Premium (Release versions of both). I then installed Code Contracts from here by clicking on the Download Code Contracts link. I then wrote the following code in a brand new console app: class Program { static void Main(string[] args) { var answer = Add(0, 5); Console.Write(answer); Console.ReadLine(); } static int Add(int

Create code contracts for a legacy library

好久不见. 提交于 2019-12-05 09:09:56
The ultimate goal is to specify contracts for a class that resides in an external assembly that I don't have control over (i.e. I cannot just add contracts to that class directly). What I have tried so far: ContractClassFor attribute. Doesn't work, because the target class must point back to the contract class. Manually construct a contract reference assembly (i.e. MyAsm.Contracts.dll) by reverse-engineering the autogenerated ones. Doesn't work, because right after I compile it, the rewriter kicks in and conveniently strips off the ContractDeclarativeAssembly attribute, which makes the

Contract class should be an abstract class

∥☆過路亽.° 提交于 2019-12-05 05:25:12
The following code gives me the warning Contract class 'FooContracts' should be an abstract class . From all the examples I've read online (e.g. http://www.infoq.com/articles/code-contracts-csharp ), this should work (presumably without compiler warnings). [ContractClass(typeof(FooContracts))] public interface IFoo { void Bar(string foo); } [ContractClassFor(typeof(IFoo))] internal sealed class FooContracts : IFoo { void IFoo.Bar(string foo) { Contract.Requires(foo != null); } } I'm in Visual Studio 2010, with the following settings in the Code Contracts section of the project's properties:

Why can't I add Contract.Requires in an overridden method?

本小妞迷上赌 提交于 2019-12-05 01:19:00
I'm using code contract (actually, learning using this). I'm facing something weird to me... I override a method, defined in a 3rd party assembly. I want to add a Contract.Require statement like this: public class MyClass: MyParentClass { protected override void DoIt(MyParameter param) { Contract.Requires<ArgumentNullException>(param != null); this.ExecuteMyTask(param.Something); } protected void ExecuteMyTask(MyParameter param) { Contract.Requires<ArgumentNullException>(param != null); /* body of the method */ } } However, I'm getting warnings like this: Warning 1 CodeContracts: Method

Can Extension Methods Be Called From The Immediate Window

十年热恋 提交于 2019-12-05 00:01:31
I ask the question because whenever I attempt to call an extension method from the Immediate window in Visual Studio 2010 I get the following error: System.Collections.Generic.IEnumerable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) If the Immediate window doesn't support extension methods, then why is it that when I type my variable (of type IEnumerable<QueryFilter> ) followed by a dot, the IntelliSense lists

Code contracts on auto-implemented properties

孤街醉人 提交于 2019-12-04 23:46:25
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) Yes, this is possible - all that is needed is to add your contract condition to the [ContractInvariantMethod] method in your class, which then adds the equivalent Requires precondition to the automatic set ter, and a post condition Ensures is added to the get . From section 2.3.1 of the Reference As the example illustrates, invariants on auto-properties turn into: A precondition for the setter A postcondition for the getter An

How can I make Code Contracts ignore a specific assembly reference?

自闭症网瘾萝莉.ら 提交于 2019-12-04 22:18:03
I'm making an extension to Visual Studio. Within the code I'm using Code Contracts to make assertions and checks. I set the warning option level to high. What I would like to do is maintain that warning level while ignoring any checks made on EnvDTE references. Consider the following code example: public static string GetAbsoluteOutputFolder(EnvDTE.Project project) { if (project == null) throw new ArgumentNullException("project"); var path = project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString(); //... } With my current settings, CC would require me to

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

戏子无情 提交于 2019-12-04 18:46:45
问题 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

Code Contracts and Asynchrony

不羁的心 提交于 2019-12-04 17:05:30
问题 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