I was able to serialize a List of objects (List) using this code:
Hide Copy Code
public static string Serialize(object obj)
{
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
However, I'm not able to deserialize using this code:
Hide Copy Code
public static object Deserialize(string xml, Type toType)
{
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(toType);
return deserializer.ReadObject(stream);
}
}
I'm not able to understand the problem. I'm using the last method by calling it with:
Deserialize(SerializedObject, List), but I'm getting an error saying List is a type, which is not valid in the given context
Could anyone help? I'm a bit over my head with this.
Loading
Mr PoganPosted May 5, 2016, 1:01 AM
The problem was that I was trying to assign the output of
`public static object Deserialize(string xml, Type toType)`
to a List<FilesToProcess> generic called listOfFiles, when I should have assigned to an object and then cast to a `List<FilesToProcess>` using
List<FilesToProcess listOfFiles = (List<FilesToProcess)listOfFilesObject;