.NET4.0 Code Conditions: Implementing Custom Rewriter

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Runtime behavior of assertions and assumptions are already defined in the original Contract class. However,
their behavior can be modified with the rewriter. My problem is that defined assertions are not designed for
real productive use. Application which does not hold defined conditions should not fail with assert. The application
should rather throw exceptions.

Here is an example of rewriter, which throws exceptions.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SampleApplication {

    public class RequiresException : Exception
   
{
        public RequiresException(string message) { }
    }

    public class EnsuresException : Exception
    {

     public EnsuresException(string message) { }
    }

     public class InvariantException : Exception
    {
        public InvariantException(string message) { }
    }

 
    public class AssertException : Exception
    {
        public AssertException(string message) { }
    }
       

    public class AssumeException : Exception
    {

        public AssumeException(string message) { }
    }

     public static class CustomRewiterMethods
 
   {

        public static void Requires(bool cond, string msg)
        {
            if (!cond) throw new RequiresException(msg);
        }       

       
        public static void Ensures(bool cond, string msg)
        {
            if (!cond) throw new EnsuresException(msg);
        }       


       
public static void Assert(bool cond, string msg)
        {
            if (!cond) throw new AssertException(msg);
        }


       
public static void Assume(bool cond, string msg)
        {
            if (!cond) throw new AssumeException(msg);

        }


       
public static void Invariant(bool cond, string msg)
        {
            if (!cond) throw new InvariantException(msg);
        }
    }
} 

 


Posted Feb 09 2009, 12:22 AM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.