code-contracts

Am I implementing this simple contract incorrectly?

假装没事ソ 提交于 2020-01-22 10:40:21
问题 This is my code: public class RegularPolygon { public int VertexCount; public double SideLength; public RegularPolygon(int vertexCount, double sideLength) { Contract.Requires(vertexCount >= 3); VertexCount = vertexCount; SideLength = sideLength; } [ContractInvariantMethod] private void RegularPolygonInvariants() { Contract.Invariant(VertexCount>=3); } } I have tried with both the Contract.Requires and Contract.Invariant methods to prevent the vertexCount variable from becoming less than or

Code Contracts - Visual Studio Team Service Scripted Build Server Unit Tests Fail

爱⌒轻易说出口 提交于 2020-01-13 19:51:11
问题 I recently added Code Contracts to my solution. After some modifications our build runs without any problems but our unit tests fail because of Code Contracts. Environment: Source control and build server are hosted on Visual Studio Team Service (VSTS) Scripted Build ( formerly VSO ) VS 2013 Premium Code ( now VS 2015 Enterprise ) contracts enabled on configurations Debug and Release Code contract turned off with DoNotBuild flag set for custom configurations BuildServer Visual Studio Team

Contract class should be an abstract class

本秂侑毒 提交于 2020-01-13 08:57:09
问题 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

How to ensure that implementations of an interface have a connection string if they are backed by a database using code contracts

一曲冷凌霜 提交于 2020-01-06 08:49:16
问题 Imagine you've an interface like this: public interface IPersonManager { public void AddPerson(string name); } ...and an implementation which we'll going to call DefaultPersonManager . Let's say we want to be sure that any implementation of IPersonManager won't be able to give a null or empty string as argument of AddPerson(string name) . For that matter, we're going to implement a contract class as follows: [ContractClassFor(typeof(IPersonManager))] public abstract class

Microsoft.Contracts namespace

▼魔方 西西 提交于 2020-01-05 04:58:11
问题 For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts; ? 回答1: For using code contracts. 回答2: You can find the library here C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll 来源: https://stackoverflow.com/questions/1679392/microsoft-contracts-namespace

Contract.Requires throwing pex errors [duplicate]

旧城冷巷雨未停 提交于 2020-01-01 23:12:25
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How Do You Configure Pex to Respect Code Contracts? Currently, when I run a pex exploration, the code contracts I created in my classes are being treated as errors in the pex exploration results. I thought when you ran pex exploration using code contracts the contract failures should be treated as expected behavior. Here is the code causing the exceptions. Test Method: [PexMethod] public void TestEquality(Guid

Code contracts. How suppress warnings on generated code?

混江龙づ霸主 提交于 2020-01-01 15:32:11
问题 How can I turn off static checks on my Linq2Sql code? 回答1: You can suppress checks on static code by marking the class(es) in question with [ContractVerification(false)] . If your generated classes are partial you can create another file with another part(ial) in it and add it there, so it doesn't get overwritten when the code is regenerated. 来源: https://stackoverflow.com/questions/3662293/code-contracts-how-suppress-warnings-on-generated-code

Code Contracts [Type]implements interface method {Interface.Method} thus cannot add requires

旧城冷巷雨未停 提交于 2019-12-29 06:36:28
问题 I have the following scenario: public interface ISomething { void DoStuff(); //... } public class Something : ISomething { private readonly ISomethingElse _somethingElse; //... public Something (ISomethingElse somethingElse) { Contract.Requires(somethingElse != null); _somethingElse = somethingElse; } public void DoStuff() { // *1* Please look at explanation / question below _somethingElse.DoThings(); } } At line 1 and with the static checker on, I'll get a warning saying that _somethingElse

How come you cannot catch Code Contract exceptions?

家住魔仙堡 提交于 2019-12-28 08:11:52
问题 System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself messing around with my shiney new copy of Visual Studio, but I'd like to know what I'm doing wrong. I'm using the professional edition of VS, therefore I do not have static checking. In order to still use code contracts (which I like) I figured the only way my method can work is to catch the exception that is thrown at runtime, but I'm not finding this possible. TestMethod

Validating parameters properties with Code Contracts

夙愿已清 提交于 2019-12-24 12:14:04
问题 I had a discussion with a colleague regarding the usage of Code Contracts to perform prerequisites checks. Let's say we have the following code: namespace Project { using System; using System.Diagnostics.Contracts; public class Thing { public string Foo { get; set; } public int Bar { get; set; } } public class ThingsManipulator { public void AddThing(Thing thing) { Contract.Requires<ArgumentNullException>(thing != null); // Do something } } } If in the // Do something I'm accessing thing.Foo