My current method for deserialization looks like this:
public static object Deserialize(string xml, Type toType)
{
object result = null;
using (var stream = new MemoryStream())
{
var data = Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
var deserializer = new DataContractSerializer(toType);
result = deserializer.ReadObject(stream);
}
return result;
}
Can I use ServiceStack's deserialization here somehow? Maybe I should be using something other than the FromXml method? The important part to me is that I need to be able to pass in the System.Type
parameter (toType) but have the result be of type object
.
This obviously doesn't work:
public static object Deserialize(string xml, Type toType)
{
object result = null;
result = xml.FromXml<toType>();
return result;
}
Neither does this:
public static object Deserialize(string xml, Type toType)
{
object result = null;
result = xml.FromXml<object>();
return result;
}