title: Why am I getting the exception "Consider using a DataContractResolver or add any types not known statically to the list of known types"
body: <p>I'm trying to serialise a object to Xml using the DataContractSerializer.  I have the following classes;</p>
  
  <pre><code>[ActiveRecord(Lazy = true)]
  [KnownType(typeof(RoomType))]
  [DataContract]
  public class Room : ActiveRecordBase&lt;Room&gt;
  {
      [PrimaryKey]
      [DataMember]
      public virtual Int64 RoomId { get; protected set; }
  
      [BelongsTo("RoomTypeId")]
      [DataMember]
      public virtual RoomType RoomType { get; set; }
  
      [Property]
      [DataMember]
      public virtual Int64 HotelId { get; set; }
  
      [Property]
      [DataMember]
      public virtual string Name { get; set; }
  
      [Property]
      [DataMember]
      public virtual string Description { get; set; }
  
      public static Room[] FindByHotelId(Int64 HotelId)
      {
          return (Room[])FindAllByProperty(typeof(Room), "HotelId", HotelId);
      }
  }
  </code></pre>
  
  <p>The RoomType class is</p>
  
  <pre><code>[ActiveRecord(Lazy = true)]
  [DataContract]
  public class RoomType : ActiveRecordBase&lt;RoomType&gt;
  {
      [PrimaryKey]
      [DataMember]
      public virtual int RoomTypeId { get; protected set; }
  
      [Property]
      [DataMember]
      public virtual string Name { get; set; }
  
  }
  </code></pre>
  
  <p>I use the following method to serialise the object</p>
  
  <pre><code>    internal static XElement ObjectToXElement&lt;T&gt;(T source)
      {
          XDocument oXDocument = new XDocument();
  
          try
          {
              using (var writer = oXDocument.CreateWriter())
              {
                  // write xml into the writer
                  var serializer = new DataContractSerializer(source.GetType());
                  serializer.WriteObject(writer, source);
              }
          }
          catch(Exception e)
          {
              using (var writer = oXDocument.CreateWriter())
              {
                  // write xml into the writer
                  var serializer = new DataContractSerializer(oError.GetType());
                  serializer.WriteObject(writer, oError);
              }
          }
  
          return oXDocument.Root;
      }
  </code></pre>
  
  <p>The actual object I'm serialising is;</p>
  
  <pre><code>[KnownType(typeof(List&lt;Room&gt;))]
  [KnownType(typeof(RoomType))]
  [DataContract]
  public class RoomTypeResponse
  {
      [DataMember]
      public int Code { get; set; }
  
      [DataMember]
      public string Message { get; set; }
  
      [DataMember]
      public List&lt;Room&gt; Rooms { get; set; }
  
      public RoomTypeResponse()
      {
          this.Rooms = new List&lt;Room&gt;();
      }
  }
  </code></pre>
  
  <p>But for some reason when I call the method to serialise the object I get the following exception;</p>
  
  <blockquote>
    <p>Type 'Castle.Proxies.RoomTypeProxy' with data contract name
    'RoomTypeProxy:<a href="http://schemas.datacontract.org/2004/07/Castle.Proxies" rel="nofollow">http://schemas.datacontract.org/2004/07/Castle.Proxies</a>'
    is not expected. Consider using a DataContractResolver or add any
    types not known statically to the list of known types - for example,
    by using the KnownTypeAttribute attribute or by adding them to the
    list of known types passed to DataContractSerializer.</p>
  </blockquote>
  
  <p>If I comment out the property in Room class, it works fine</p>
  
  <pre><code>[BelongsTo("RoomTypeId")]
  [DataMember]
  public virtual RoomType RoomType { get; set; }
  </code></pre>
  
  <p>I'm not sure why I am getting the exception, because I've added the knowtype attribute for RoomType ?  What am I missing, that is causing this problem.</p>