Isn't it good idea to make contracts description like in MS CodeContracts?

Nov 8, 2009 at 4:50 PM
Edited Nov 9, 2009 at 7:03 AM

For example: Contracts.Requires<ArgumentNullException>

So it will be easier to go from light to heavy contracts and vice versa?

And did You consider something related to expression trees and  lambdas, I mean:

Requires( () = > id != null && id > 99) instead of just Requires(id != null && id > 99)

May be it is possible to get some data from it ?


Nov 9, 2009 at 10:23 PM

LightContracts has a fluent API which should allow copying assertions directly into method comments without further editing. The goal was not imitating Microsofts CodeContracts but instead provide a lightweight alternative which is easy to use. One aspect of easy usage is the fluent API.

Adding an additional method to provide support for throwing ArgumentExceptions is possible. Until now I have not found this usefull enough to implement that :-). 

The idea with lambda expressions is quite good. We can add a method like:

public static void That(Func<bool> boolExpression)

With such method you can use reflection to get some information about the boolExpression:

         byte[] itermediateLanguageBytes = boolExpression.Method.GetMethodBody().GetILAsByteArray();

The problem is that it is not trivial to create C# or VB code from the byte array. The Reflector tool does that but until now I've not tried to implement that myself.