title: NHibernate "Invalid Cast (check your mapping for property type mismatches); setter of System.Object"
body: <p>I've been upgrading my NHibernate installation (was still on 1.2!). All has been good until I tried to setup an Interceptor. If I add an Interceptor to the OpenSession() call, then I get this error when trying to load an entity from the DB: </p>
  
  <p>"Invalid Cast (check your mapping for property type mismatches); setter of System.Object"</p>
  
  <p>If there is no Interceptor, this error does not occur.</p>
  
  <p>The Interceptor is very simple, and in fact I pretty much commented out everything in order to test:</p>
  
  <pre><code>public class Interceptor : EmptyInterceptor
  {
      public override void AfterTransactionBegin(ITransaction tx)
      {            
      }
  
      public override void AfterTransactionCompletion(ITransaction tx)
      {            
      }
  
      public override void BeforeTransactionCompletion(ITransaction tx)
      {            
      }
  
      //public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
      //{            
      //}
  
      //public override object GetEntity(string entityName, object id)
      //{            
      //}
  
      //public override string GetEntityName(object entity)
      //{            
      //}
  
      public override object Instantiate(string entityName, EntityMode entityMode, object id)
      {
          //((EntityBase)entity).Instantiate(entityName, entityMode, id)
          return new object();
      }
  
      //public override bool? IsTransient(object entity)
      //{            
      //}
  
      public override void OnCollectionRecreate(object collection, object key)
      {            
      }
  
      public override void OnCollectionRemove(object collection, object key)
      {            
      }
  
      public override void OnCollectionUpdate(object collection, object key)
      {            
      }
  
      public override void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types)
      {
          //((EntityBase)entity).OnDelete(entity, id, state, propertyNames, types);
      }
  
      //public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
      //{            
      //}
  
      public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
      {
          //if (entity.GetType().FullName.Equals("EntityBase"))
          //    return ((EntityBase)entity).OnLoad(entity, id, state, propertyNames, types);
          //else
              return false;
      }
  
      //public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
      //{            
      //}
  
      public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
      {
          //return ((EntityBase)entity).OnSave(entity, id, state, propertyNames, types);
          return true;
      }
  
      public override void PostFlush(System.Collections.ICollection entities)
      {            
      }
  
      public override void PreFlush(System.Collections.ICollection entities)
      {            
      }
  
      public override void SetSession(ISession session)
      {
      }
  }
  </code></pre>
  
  <p>As such I'm totally baffled. Like I said, with the Interceptor it errors on loading and hydrating entities (after the OnLoad event fires), without the Interceptor it's fine. I'm using nHibernate 3.2.0.2001 with .Net4 against SQL Server 2008.</p>
  
  <p>Any help appreciated! Ben</p>
  
  <hr>
  
  <p>Unfortunately I've been unable to progress this issue, I had to switch to Linq-SQL because of this specific problem, and that I couldn't find a solution in the timeframe required to use these templates. Therefore I can't provide any more information at this time. </p>