Skip to content

Instantly share code, notes, and snippets.

@allude
Created March 24, 2018 05:18
Show Gist options
  • Save allude/b12e31ec66d4e53fa5bc39072a549849 to your computer and use it in GitHub Desktop.
Save allude/b12e31ec66d4e53fa5bc39072a549849 to your computer and use it in GitHub Desktop.
XmlSerializer Cache Class
// Based on https://codereview.stackexchange.com/questions/24861/caching-xmlserializer-in-appdomain
public static class XmlSerializerCache
{
private AppDomain cache {
get {
return AppDomain.CurrentDomain;
}
}
public static XmlSerializer GetXmlSerializer(Type type, XmlRootAttribute xmlRoot)
{
var key = String.Format(CultureInfo.InvariantCulture, "CachedXmlSerializer:{0}:{1}", type, xmlRoot.ElementName);
var serializer = cache.GetData(key) as XmlSerializer;
if (serializer == null)
{
serializer = new XmlSerializer(type, xmlRoot);
cache.SetData(key, serializer);
}
return serializer;
}
public static XmlSerializer GetXmlSerializer(Type type)
{
var key = String.Format(CultureInfo.InvariantCulture, "CachedXmlSerializer:{0}", type);
var serializer = cache.GetData(key) as XmlSerializer;
if (serializer == null)
{
serializer = new XmlSerializer(type);
cache.SetData(key, serializer);
}
return serializer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment