Friday, 15 May 2015

c# - How do I deserialize a List as another List instead of a readonly T[]? -



c# - How do I deserialize a List<T> as another List<T> instead of a readonly T[]? -

we have code makes re-create of list<t> of objects. problem code returning readonly array of t[] instead of list<t>.

why that, , how can prepare deserialization code?

public interface itestobject { } [knowntype(typeof(itestobject))] [datacontract(name = "testobject")] public class testobject : itestobject { [datamember] public int id { get; set; } [datamember] public string value { get; set; } } public class testapp { public void run() { ilist<itestobject> = new list<itestobject>(); a.add(new testobject() { id = 1, value = "a" }); a.add(new testobject() { id = 2, value = "b" }); ilist<itestobject> b = copy(a); // list<t> while b readonly t[] debug.writeline(a.gettype().fullname); debug.writeline(b.gettype().fullname); } public ilist<itestobject> copy(ilist<itestobject> list) { datacontractserializer serializer = new datacontractserializer(typeof(ilist<itestobject>), new type[] { typeof(testobject) }); using (stream stream = new memorystream()) { serializer.writeobject(stream, list); stream.seek(0, seekorigin.begin); ilist<itestobject> clone = (ilist<itestobject>)serializer.readobject(stream); stream.close(); homecoming clone; } } }

i'm guessing it's fact list<t> isn't listed knowntype

try using datacontractserializer(typeof(list<itestobject>) knows concrete type

c# datacontractserializer

No comments:

Post a Comment