code-contracts

Implementation of non-void interface Code Contracts - default(T) vs throw NotImplementedException

倾然丶 夕夏残阳落幕 提交于 2019-12-12 14:14:55
问题 This may be mostly a question of style but when defining code contracts for non-void interface members, which approach is best: Interface: [ContractClass(typeof(IFooContract))] public interface IFoo { object Bar(); } Contract option 1: [ContractClassFor(typeof(IFoo))] public abstract class IFooContract : IFoo { object IFoo.Bar() { Contract.Ensures(Contract.Result<object>() != null); throw new NotImplementedException(); } } Contract option 2: [ContractClassFor(typeof(IFoo))] public abstract

Code Contracts: Ensure unproven on string method

我们两清 提交于 2019-12-12 05:28:08
问题 I'm playing around with code contracts and got a simple method inserting a given count of space characters between each character. e.g. Hello -> H e l l o World -> W o r l d The method InsertSpaceBetweenLetters is implemented within a class that offers some string-property S, the string that should be returned after modification. Here's the code public string InsertSpaceBetweenLetters(int spaceWidth) { Contract.Requires(spaceWidth > 0); Contract.Requires(S.Length > 0); Contract.Ensures

Code contract inheritance

随声附和 提交于 2019-12-12 02:29:59
问题 Found a bit of an oddity with code contracts and i was wondering if anyone knew the cause ... ok so time for some code samples: Assembly 1: [ContractClass(typeof(IServiceCodeContract<>))] public interface IService<T> where T : class { ... } [ContractClassFor(typeof(IService<>))] public abstract class IServiceCodeContract<T> : IService<T> where T : class { ... } public abstract class ServiceBase<T> : IService<T> where T : class { ... } Assembly 2: [ContractClass(typeof

How to Log error while using Code Contracts

隐身守侯 提交于 2019-12-11 10:52:43
问题 I need to log exception (even in release version) in my application. I am using a static method Log(Exception ex) of a class CLog to log the exception. I am also using Code Contracts and using both static and runtime checking. Please consider the following code: public void editCommit(CEmployee emp) { Contract.Requires<ArgumentNullException>(null != emp, "Null argument, Parameter name : emp"); this.employee.Name = emp.Name; this.employee.Age = emp.Age; } Now If I don't correct all the

Throw specific exception for a violation of a contract on an automatic property

ぃ、小莉子 提交于 2019-12-11 10:21:35
问题 I can specify a contract for an automatic property like this (example taken from the CC documentation): public int MyProperty { get; set ; } [ContractInvariantMethod] private void ObjectInvariant () { Contract. Invariant ( this .MyProperty >= 0 ); ... } When runtime-checking is turned on, and an attempt is made to assign an invalid value to MyProperty, the setter throws System.Diagnostics.Contracts.__ContractsRuntime+ContractException. Is there a way to make it throw a specific type of

CodeContracts: Reuse of assumptions/asserts?

隐身守侯 提交于 2019-12-11 07:43:44
问题 I've posted this on the CodeContracts forum at the MSDN but apparently no one knows or bothers to look into this issue. I've tried to reduce the repetitive asserts and make it more reusable but unfortunately this doesn't work can you explain why? [ContractVerification(false)] public static class Assert { [Conditional("DEBUG")] public static void GreaterThan<T>(T value, T lowerBound) where T : IComparable<T> { Contract.Ensures(value.CompareTo(lowerBound) > 0); } [Conditional("DEBUG")] public

Using code contracts to make a generic to be of type enum

吃可爱长大的小学妹 提交于 2019-12-11 02:47:51
问题 A few days ago I asked a question titled How to constraint a generic to be of type enum?. To summarize the problem is the following code: class MyClass<T> where T : enum // Not possible in C# { } I was introduced to code contracts and that I could produce a compile time warning for the problem, which is all I want (to be informed at compile time that T should be an enum ). I tried the follwing code (Full source). class MyClass<T> { public MyClass() { Contract.Requires(typeof(System.Enum)

C# Code Contracts userMessage Parameter

不想你离开。 提交于 2019-12-10 21:47:13
问题 I am using Code Contracts in C# but I am a bit curious what I should be typing in for the userMessage parameter. I will give a short example. I have the following assertion in my code: Contract.Assert(IsValidReferenceData(refData)); This message will never be displayed to the user, but it would be nice to have an English message description in the exception for myself and other developers/maintainers of the software. Initially I thought Contract.Assert(IsValidReferenceData(refData), "Payment

Code Contracts Ensures for ReSharper ExternalAnnotations

六月ゝ 毕业季﹏ 提交于 2019-12-10 19:01:43
问题 Does anyone know how to add Code Contracts Ensures in ReSharper ExternalAnnotations? It's not there in the last v7.1.3 nor in the latest v8 EAP, and neither in any of the custom xmls floating around. Specifically it should detect if a method does not return a null: Contract.Ensures(Contract.Result<T>() != null); 回答1: If you're attempting to simply appease the analysis engine, the simplest thing to use is [NotNull] in front of the method declaration. The Contract Annotations to which you

How to specify Ensures for out parameter?

半腔热情 提交于 2019-12-10 18:28:08
问题 I have a method with an out parameter and I want to specify using Contract.Ensures() that when the method returns, the parameter will not be null . Basically, this: void M(out object o) { Contract.Ensures(o != null); o = new object(); } This does not compile, because it looks like the parameter will be read before it's assigned: error CS0269: Use of unassigned out parameter 'o' I thought I could fix this by switching the lines around, but that does not work either: warning CC1005: