Search code examples
c#listserializationexceptionxmlserializer

How to serialize a List of custom object types


I have the class like this

public class Record
{
    public Int32 TotalTrail { get; set; }
    public TimeSpan MyTimeSpan { get; set; }
    public DateTime MyDateTime { get; set; }
}

And I have a List to hold objects of it:

List<Record> _records;

Then when I want to serialize the list:

serializer.Serialize(stream, _records);

There is a runtime error on the above line:

Cannot assign object of type System.Collections.Generic.List`1[[SimpleGame.Record, SimpleGame, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to an object of type SimpleGame.Record.

Is it because I can't serialize the list? How can I fix this?


Solution

  • You have to create the serializer for the type List<Record> instead of Record