title: Customising Code Contract Exception Messages
body: <p>I have a code contract expresses as this - it validates that entity to be stored is not null and is valid for persistence. It works. Fab.</p>
  
  <pre><code>[ContractClassFor(typeof(IRepository&lt;,&gt;))]
  internal abstract class ContractsForIRepository&lt;T, TId&gt; : IRepository&lt;T, TId&gt; where T : IEntity
  {
      private ContractsForIRepository()
      {
  
      } 
  
      public T Persist(T entity)
      {
          Contract.Requires&lt;InvalidEntityException&gt;(entity != null, "Entity is null");
          Contract.Requires&lt;InvalidEntityException&gt;(entity.IsValidForPersistence(), "Entity not valid for persistence");
          return default(T);
      }
  
  }
  </code></pre>
  
  <p>However, I would like the exception to be more useful - as anyone receiving the message will want to know what entity was invalid and what is looked like. All of the entities override ToString(), so I wanted to include this in the error message:</p>
  
  <pre><code>Contract.Requires&lt;InvalidEntityException&gt;(entity.IsValidForPersistence(), "Entity not valid for persistence " + entity.ToString());
  </code></pre>
  
  <p>I have included ToString to be explicit - it would be called implicitly if I omitted it, but I think it makes my question clearer.</p>
  
  <p>The problem is, this isn't allowed by code contracts and I get the following message.</p>
  
  <blockquote>
    <p>User message to contract call can only be string literal, or a static field, or static property that is at least internally visible.</p>
  </blockquote>
  
  <p>Is there any way to include specific data in the exception message?</p>