code-contracts

.Net 3.5 Implementation of String.IsNullOrWhitespace with Code Contracts

我们两清 提交于 2019-12-10 17:34:52
问题 I'm attempting to use Contracts in my .Net 3.5 (C#) project. I found where I had written something like if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s.Trim())) throw new ArgumentException("Required", "s"); at the beginning of methods. I could keep them there and use legacy contracts. I could change it to Contract.Requires(s!= null && string.IsNullOrEmpty(s.Trim())); . But besides not being very sure about the correctness or performance of the Whitespace part of the condition, those are

Moq and Code Contracts

放肆的年华 提交于 2019-12-10 15:26:26
问题 When using class invariants, Code contracts seems to inject code everywhere. Stuff like this [ContractClassFor(typeof(IX))] interface IXContract { [ClassInvariant] void Invariant() { ... } } [ContractClass(typeof(IXContract))] interface IX { event EventHandler b; } var a = new Mock<IX>(); a.Raise(x => x.b += null); Fails with an error message Could not locate event for attach or detach method Void $InvariantMethod$(). Anyone know of a solution? 回答1: This unit test "passes" when run without

Can Extension Methods Be Called From The Immediate Window

北城余情 提交于 2019-12-10 01:12:12
问题 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

Generate Contracts for REST objects

99封情书 提交于 2019-12-09 15:28:57
问题 I'm new to REST and this sounds like it should be pretty simple. In a .NET app, I can create a reference to a WCF service and the contracts for all the available types will be generated for me. Now I'm trying to consume a REST service in a Windows Phone 7 app. While I can make my call and get back the proper response, is there a simple way to create the classes that each object would be deserialized to? I'm using RestSharp to manage my calls. In some examples I've seen, user's have created

CodeContracts: Possibly calling a method on a null reference

巧了我就是萌 提交于 2019-12-08 21:28:58
问题 I'm having an argument with the CodeContracts static analysis tool. My code: (ASCII version) The tool tells me that instance.bar may be a null reference. I believe the opposite. Who is right? How can I prove it wrong? 回答1: Update : It seems the problem is that invariants are not supported for static fields. 2nd Update: The method outlined below is currently the recommended solution. A possible workaround is to create a property for instance that Ensure s the invariants that you want to hold.

Why is the CC1033 message suppression not applied?

為{幸葍}努か 提交于 2019-12-08 09:46:10
问题 I am implementing the System.IServiceProvider.GetService method and I cannot suppress the warning ... implements interface method 'System.IServiceProvider.GetService(System.Type)', thus cannot add Requires. [SuppressMessage("Microsoft.Contracts","CC1033", Justification="Check to require service type not null cannot be applied")] public object GetService(Type serviceType) { } 回答1: I'm guessing GetService is a method in an interface that you've implemented in a concrete class, and that the

Ensures Unproven via property when implementing interface

半腔热情 提交于 2019-12-08 03:09:45
问题 I'm trying what, to me, seems like some fairly basic code contracts code. I've reduced it down to the following problem. The following fails the static analysis, with the message CodeContracts: ensures unproven: this.Frozen using System; using System.Diagnostics.Contracts; namespace PlayAreaCollection2010 { public class StrippedContract : IBasic { private bool _frozen = false; public void Freeze() { _frozen = true; } public bool Frozen { get { return _frozen; } } } [ContractClass(typeof

Entities used by ORM in combination with CodeContracts - ensure invariants

扶醉桌前 提交于 2019-12-07 11:03:44
问题 I am currently in the process of adding CodeContracts to my existing code base. One thing that proves difficult is the usage of entities that are hydrated by NHibernate. Assume this simple class: public class Post { private Blog _blog; [Obsolete("Required by NHibernate")] protected Post() { } public Post(Blog blog) { Contract.Requires(blog != null); _blog = blog; } public Blog Blog { get { Contract.Ensures(Contract.Result<Blog>() != null); return _blog; } set { Contract.Requires(value != null

Controlling Code Contract references in a .nuspec

有些话、适合烂在心里 提交于 2019-12-07 10:25:02
问题 I'm using Code Contracts to generate satellite assemblies for my project. Basically it creates a MyAssembly.Contracts.dll for the project's MyAssembly.dll. This is supposed to be put beside your assembly but not referenced by any app -- it's used only by the contracts tools. I'm trying to include this in my nuget package, but when I then install that package into another app, the Contracts assembly is included as a reference which creates problems. According to the .nuspec reference, this

Create code contracts for a legacy library

不打扰是莪最后的温柔 提交于 2019-12-07 05:31:52
问题 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