Created
March 24, 2018 05:18
-
-
Save allude/b12e31ec66d4e53fa5bc39072a549849 to your computer and use it in GitHub Desktop.
XmlSerializer Cache Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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