code-contracts

.NET Guard Class Library?

限于喜欢 提交于 2019-11-28 18:24:17
I'm looking for a library or source code that provides guard methods such as checking for null arguments. Obviously this is rather simple to build, but I'm wondering if there are any out there for .NET already. A basic Google search didn't reveal much. There is CuttingEdge.Conditions . Usage example from the page: public ICollection GetData(Nullable<int> id, string xml, ICollection col) { // Check all preconditions: id.Requires("id") .IsNotNull() // throws ArgumentNullException on failure .IsInRange(1, 999) // ArgumentOutOfRangeException on failure .IsNotEqualTo(128); // throws

Does VS2017 work with CodeContracts?

安稳与你 提交于 2019-11-28 16:49:33
I just installed the newly released Visual Studio 2017 Enterprise (RC). However I'm having trouble getting it to work with Microsoft CodeContracts. I have no problem using CodeContract with VS2015 at all. Am I missing something? As others have noted, Microsoft hasn't prioritized Code Contracts and its long-term support remains unclear (though there has been some ongoing discussion about language-level integration via Roslyn). As of March 11th, 2017 , however, community contributor Yaakov has, at least, updated the source code to include the Visual Studio 2017 build targets (thank you!). This

Using Contract.ForAll in Code Contracts

对着背影说爱祢 提交于 2019-11-28 11:01:22
Okay, I have yet another Code Contracts question. I have a contract on an interface method that looks like this (other methods omitted for clarity): [ContractClassFor(typeof(IUnboundTagGroup))] public abstract class ContractForIUnboundTagGroup : IUnboundTagGroup { public IUnboundTagGroup[] GetAllGroups() { Contract.Ensures(Contract.Result<IUnboundTagGroup[]>() != null); Contract.Ensures(Contract.ForAll(Contract.Result<IUnboundTagGroup[]>(), g => g != null)); return null; } } I have code consuming the interface that looks like this: public void AddRequested(IUnboundTagGroup group) { foreach

Code Contracts: How do I supply a contract class for a generic interface?

断了今生、忘了曾经 提交于 2019-11-28 09:36:52
I'd like to specify a contract for this generic interface, using Code Contracts: interface IRandomWriteAccessible<T> { T this[uint index] { set; } uint Length { get; } } The documentation says to use the ContractClass attribute when specifying a contract for an interface. However, the compiler will complain about this: [ContractClass(typeof(IRandomWriteAccessibleContract<T>))] // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- compiler error interface IRandomWriteAccessible<T> { … } [ContractClassFor(typeof(IRandomWriteAccessible<T>))] // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- compiler error

How come you cannot catch Code Contract exceptions?

给你一囗甜甜゛ 提交于 2019-11-28 04:05:18
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 [TestMethod, ExpectedException(typeof(System.Diagnostics.Contracts.ContractException))] public void

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

人走茶凉 提交于 2019-11-28 03:12:49
问题 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.Requires(weapon != null); // long code return false; } public bool CrushGodzilla(string weapon) { Contract.Requires(weapon != null); // specify contract requirement here // as

Why .net exception is not caught?

◇◆丶佛笑我妖孽 提交于 2019-11-27 23:49:29
问题 Consider the following "Safe" program: internal class Safe { public static void SafeMethodWillNeverThrow() { try { var something = ThrowsNewException(); Func<int, string> x = p => something.ToString(); } catch (Exception) { } } private static object ThrowsNewException() { throw new Exception(); } public static void Main() { SafeMethodWillNeverThrow(); } } It should never complete with an exception. But why it fails when I run it? Why SafeMethodWillNeverThrow() throws the Exception? Before

How does Contract.Ensures work?

空扰寡人 提交于 2019-11-27 17:22:50
问题 I'm starting to use Code Contracts, and whilst Contract.Requires is pretty straight forward, I'm having trouble seeing what Ensures actually does. I've tried creating a simple method like this: static void Main() { DoSomething(); } private static void DoSomething() { Contract.Ensures(false, "wrong"); Console.WriteLine("Something"); } I never see the message "wrong" though, nor does it throw exceptions or anything else. So what does it actually do ? 回答1: It's odd for it to not throw anything -

Throwing an exception vs Contract.Requires<T>?

徘徊边缘 提交于 2019-11-27 13:00:19
问题 I'm wondering whether should I throw exceptions or call Contract.Requires<TException> For example: public static void Function(String str) { if (str == null) throw new ArgumentNullException("str", "Input string cannot be null."); // ... } vs public static void Function(String str) { Contract.Requires<ArgumentNullException>(str != null, "Input string cannot be null."); // ... } Since Contract.Requires<TException> doesn't require the CONTRACTS_FULL symbol I can keep it in my release builds as

ReSharper - Possible Null Assignment when using Microsoft.Contracts

笑着哭i 提交于 2019-11-27 10:19:47
Is there any way to indicate to ReSharper that a null reference won't occur because of Design-by-Contract Requires checking? For example, the following code will raise the warning ( Possible 'null' assignment to entity marked with 'NotNull' attribute ) in ReSharper on lines 7 and 8: private Dictionary<string, string> _Lookup = new Dictionary<string, string>(); public void Foo(string s) { Contract.Requires(!String.IsNullOrEmpty(s)); if (_Lookup.ContainsKey(s)) _Lookup.Remove(s); } What is really odd is that if you remove the Contract.Requires(...) line, the ReSharper message goes away. Update I