Sunday, January 15, 2012

.Net code contracts

I knew there is something new in .NET 4 called code contracts and I also saw some simple code snippets about it. Without digging in, I though it was just some fluent code sugar to help you replace assertions like
if (input_int > 0) throw new ArgumentException("some msg");
with
Contracts.Requires<argumentexception>(input_int > 0);
and come on, this fluent sucks, requires an exception? I would have written a better one!

But I was wrong!

I had some free time today and I opened a long bookmarked url and took a look at it. After reading through the first section, I knew I was wrong. There was post compile code weaving and checking. Without it, there is not much virtual inside, but with the post process, everything can happen. And it turns out to be a very much valuable technology than I thought. Kudos!

Some simple googling led me to this blog that provides some analysis I would also do, using Reflector on the generated code, to see the magic there. Also in their home page, there is a video at the bottom(?!) of the page, after some random comments!

No comments:

Post a Comment