I am trying to serialize an object that I have created here:
using (MemoryStream memStream = new MemoryStream())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CaseTreatment));
ser.WriteObject(memStream, ct);
}
This is ct here: List<CaseTreatment> ct = new List<CaseTreatment>();
Whenever my code hits ser.WriteObject(memStream, ct); I get the following error:
Type 'System.Collections.Generic.List`1[[FileUpload.Models.CaseTreatment, FileUpload,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name
'ArrayOfCaseTreatment:http://schemas.datacontract.org/2004/07/FileUpload.Models' 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.
My CaseTreatment object looks like this:
[DataContract]
public class CaseTreatment
{
[DataMember]
public Bridge BridgeTreatment;
}
And Bridge looks like this:
[DataContract]
public class Bridge : CaseGeneric
{
[DataMember]
public bool IsBridgeTreatment;
public Bridge(XElement treatment, IEnumerable<XElement> orderDetails)
{
var tn = Util.GetTitle(treatment);
this.Instruction = Util.GetInstruction(tn);
this.Id = 2;
this.Name= "Bridge";
this.Something = Util.GetSomething(tn);
}
public Bridge()
{
}
}
Something is another object:
[DataContract]
public class Something
{
[DataMember]
public string Number;
}
And CaseGeneric has all of this:
[DataContract]
public class CaseGeneric
{
[DataMember]
public string Instruction;
[DataMember]
public int Id;
[DataMember]
public string Name;
[DataMember]
public List<Something> Something;
}
Would anyone be able to help? Also please let me know if you need more information, I will be more than happy to add/edit anything or answer any questions. I thought that I might need to decorate my objects with [KnownType(typeof(blah))] I just dont know where and of which one.