title: Why does the DataContractSerializer not work?
body: <p>I don't know how to Serialize a object by DataContractSerialize.
  Here is my code:</p>
  
  <pre><code>    public static string DataContractSerialize(object target)
      {
          var formatter = new DataContractSerializer(target.GetType());
          using (var stream = new MemoryStream())
          {
              formatter.WriteObject(stream, target);
              stream.Position = 0;
              return Encoding.UTF8.GetString(stream.ToArray());
          }
      }
  </code></pre>
  
  <p>and entity</p>
  
  <pre><code>[Serializable, DataContract(Namespace = "CommunicationModel.Entity")]
  [KnownType(typeof(Message))]
  [KnownType(typeof(int))]
  [KnownType(typeof(string))]
  [KnownType(typeof(Type))]
  [KnownType(typeof(object))]
  public class Message : IDisposable
  {
      public Message(string stringInfo)
      {
          MessageValue = stringInfo;
          MessageType = typeof (string);
      }
  
      public Message(int intInfo)
      {
          MessageValue = intInfo;
          MessageType = typeof (int);
      }
      [DataMember]
      public Type MessageType { get; private set; }
      [DataMember]
      public object MessageValue { get; private set; }
  
      #region Implementation of IDisposable
  
      public void Dispose()
      {
      }
  
      #endregion
  }
  </code></pre>
  
  <p>When I run the DataContractSerialize like this:</p>
  
  <pre><code>var sData = SerializerHelper.DataContractSerialize(msg);
  </code></pre>
  
  <p>it will throw a exception. What can I do?</p>