People tend to recommend to cache XmlSerializer instances. Is this still actual? (if no, when has it become so?).
Also I came up with the following code and ask to review it.
- Does it have any drawbacks?
- Will it use many threads\cores for different types?
- How can I improve it?
The code:
public static class SerializerFactory<T>
{
public static readonly XmlSerializer Serializer = new XmlSerializer(typeof(T));
}
and usage:
var obj = (T)XmlSerializerFactory<T>.Serializer.Deserialize(input);
When you use the
XmlSerializer(Type)orXmlSerializer(Type,String)constructors then the serialization assembly will be cached so there is very little overhead in creating a new serializer instance (source).So as your factory would use this constructor, there’s no real point to it. You may as well just use the constructor directly.