title: DataContract Serialization - Can I serialize a Type Property?
body: <p>I am working on providing a generic JSON serialiation/deserialization service as a WCF service.</p>
  
  <p>in order to do this, I have the following DataContract</p>
  
  <pre><code>[DataContract]
  public class SerializationRequest
  {
       [DataMember]
       public Object Data {get;set;}     //Object to be serialized
  
       [DataMember]
       public string Type {get;set;}       //Type of the Data object
  
  }
  </code></pre>
  
  <p>The problem I am experiencing is I am getting the following error:</p>
  
  <blockquote>
    <p>The InnerException message was 'Type 'System.RuntimeType' with data contract name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' 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.'.  Please see InnerException for more details.</p>
  </blockquote>
  
  <p>Which tells me I either need to make the DataContractJSONSerializer aware of the type, or that Type is unable to be serialized.</p>
  
  <p>I have tried registering the type of object and adding it to the Serializers "Known Types", but this doesn't seem to work, which makes me believe that my problem lies in having a parameter of type Type on my DataContract.</p>
  
  <p>How can I accomplish this? I need to be able to call the following code("ish").</p>
  
  <pre><code>        DataContractJsonSerializer serializer = new DataContractJsonSerializer(request.Type);
          MemoryStream ms = new MemoryStream();
          serializer.WriteObject(ms, request.Data);
          string json = Encoding.Default.GetString(ms.ToArray());
          ms.Dispose();
  
          return new JSONSerializationResponse() {Data = json};
  </code></pre>
  
  <p><b>EDIT</b></p>
  
  <p>I have changed the type parameter to be the Full Qualified Name of the type that is to be serialized, but I am still getting the same problem when I call the following code in my Remote WCF Service:</p>
  
  <pre><code>        Type type = Type.GetType(request.Type);
          DataContractJsonSerializer serializer = new DataContractJsonSerializer(type, new Type[]{Type.GetType(request.Type)});
          MemoryStream ms = new MemoryStream();
          serializer.WriteObject(ms, request.Data);
          string json = Encoding.Default.GetString(ms.ToArray());
          ms.Dispose();
  
          return new JSONSerializationResponse() {Data = json};
  </code></pre>
  
  <p>I have tried adding that created type to the KnownTypes in the Constructor of the DataContractJSONSerializer, but that is not working either...any ideas?</p>
  
  <p>Here is an example of a simple class that would be called that needs to be able to be serialized</p>
  
  <pre><code>[DataContract]
  [KnownType(typeof(Person))]
  public class Person
  {
      [DataMember]
      public string Age {get;set;}
  
      [DataMember]
      public strign Name {get;set;}
  }
  </code></pre>
  
  <p>so now I should be able to pass this into my request object with specifying the type name, and then get back a JSON Result of this object.</p>