title: How do I serialize a C# anonymous type to a JSON string?
body: <p>I'm attempting to use the following code to serialize an anonymous type to JSON:</p>
  
  <pre><code>var serializer = new DataContractJsonSerializer(thing.GetType());
  var ms = new MemoryStream();
  serializer.WriteObject(ms, thing);
  var json = Encoding.Default.GetString(ms.ToArray()); 
  </code></pre>
  
  <p>However, I get the following exception when this is executed:</p>
  
  <blockquote>
    <p>Type
    '&lt;>f__AnonymousType1`3[System.Int32,System.Int32,System.Object[]]'
    cannot be serialized. Consider marking
    it with the DataContractAttribute
    attribute, and marking all of its
    members you want serialized with the
    DataMemberAttribute attribute.  See
    the Microsoft .NET Framework
    documentation for other supported
    types.</p>
  </blockquote>
  
  <p>I can't apply attributes to an anonymous type (as far as I know).  Is there another way to do this serialization or am I missing something? </p>