title: how to serialize a System.Type object with XmlSerializer?
body: <p>Consider this type:</p>
  
  <pre><code>  [DataContract]
    public class EntityId
    {
      [DataMember(Order = 1)]
      public string IdAsString { get; set; }
      [DataMember(Order = 2)]
      public Type Type { get; set; }
    }
  </code></pre>
  
  <p>I created an Xml serializer assembly for it. However, trying to serialize it yeilds an exception:</p>
  
  <pre><code>System.InvalidOperationException occurred
    Message=The type NC.DTO.FlowFolder was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
    Source=NC.DTO.XmlSerializers
    StackTrace:
         at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(String n, String ns, Type o, Boolean isNullable, Boolean needType)
    InnerException: 
  </code></pre>
  
  <p>Where <strong>NC.DTO.FlowFolder</strong> is actually the string representation of <code>typeof(NC.DTO.FlowFolder)</code> - the type <code>NC.DTO.FlowFolder</code> is known to the Xml serializer, what it seems to fail to serialize is the <code>Type</code> object itself.</p>
  
  <p>The reflector reveals the failing method contents:</p>
  
  <pre><code>private void Write3_Type(string n, string ns, Type o, bool isNullable, bool needType)
  {
      if (o == null)
      {
          if (isNullable)
          {
              base.WriteNullTagLiteral(n, ns);
          }
      }
      else if (!needType &amp;&amp; (o.GetType() != typeof(Type)))
      {
          throw base.CreateUnknownTypeException(o);
      }
  }
  </code></pre>
  
  <p>Notice the <code>throw</code> statement.</p>
  
  <p>The stack trace is:</p>
  
  <pre><code>NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(string n, string ns, System.Type o, bool isNullable, bool needType) + 0xda bytes    
  NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write11_EntityId(string n, string ns, NC.DTO.EntityId o, bool isNullable, bool needType) + 0x2a8 bytes  
  NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write14_FlowFolder(string n, string ns, NC.DTO.FlowFolder o, bool isNullable, bool needType) + 0x36b bytes  
  NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write42_FlowFolder(object o) + 0xc9 bytes   
  [Native to Managed Transition]  
  System.Xml.dll!System.Xml.Serialization.TempAssembly.InvokeWriter(System.Xml.Serialization.XmlMapping mapping, System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 342 + 0xb9 bytes    C#
  System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 676 + 0xdf bytes   C#
  System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle) Line 646   C#
  System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces) Line 640 C#
  System.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o) Line 616  C#
  </code></pre>
  
  <p>My question is how can I make Xml serializer happy to serialize and deserialize <code>EntityId</code> objects?</p>
  
  <p>Thanks.</p>
  
  <p><strong>P.S.</strong></p>
  
  <p>If someone knows an alternative Xml serialization library, something that would be to XML serialization as Newtonsoft.Json to JSON serialization - please please please share.</p>