The documentation for the XmlSerializer.Serialize method states the following:
The
XmlSerializer
cannot serialize the following: arrays ofArrayList
and arrays ofList<T>
.
However if I try with the following code it works (I am use List<int>
and ArrayList
). So is this a documentation defect, a new feature in .NET 4.5 that hasn't made it's way to documentation?
I had suspected that it could be an abbreviated message about how you cannot serialise a List<T>
unless you have all the types in object graph, but that doesn't make sense for ArrayList which is just object
.
private static string Serialise<T>(T o)
{
var serializer = new XmlSerializer(typeof(T));
var memoryStream = new MemoryStream();
serializer.Serialize(memoryStream, o);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
return reader.ReadToEnd();
}
}
Read the documentation again - it says you can't serialize arrays of List<T>
or ArrayList
(i.e. List<T>[]
and ArrayList[]
).
Sure enough, these work:
Serialise(new ArrayList());
Serialise(new List<int>());
These do not:
Serialise(new ArrayList[]{});
Serialise(new List<int>[]{});
The latter throw this exception:
System.InvalidOperationException:
Unable to generate a temporary class(result=1)
.