Scott Hanselman

XmlSerializer Madness: The Guts and the Conclusion

December 05, 2003 Comment on this post [0] Posted in XmlSerializer
Sponsored By

Looks like a little bit of wisdom from Kevin Dente has solved the mystery.

He sez:

A little Reflectoring into the XMLSerializer source shows that in .NET 1.1 it caches the temp assembly using both the type and default namespace as the key. However, 1.0 cached only on the type, and if you specified a namespace it ignored the cache. And it looks like even in 1.1 the other constructor overloads ignore the cache. Good to know.

And the reflectoring shows just that: (Note the Monitor.Enter [lock()] around their cache)

public XmlSerializer(Type type, string defaultNamespace){ 
  XmlReflectionImporter importer1;
  TempAssemblyCache cache1;
  this.events = 0;
  base..ctor();
  this.events.sender = this;
  this.tempAssembly = XmlSerializer.cache[defaultNamespace, type];
  if (this.tempAssembly == null){
    cache1 = XmlSerializer.cache;
    Monitor.Enter(XmlSerializer.cache);
    try{
       this.tempAssembly = XmlSerializer.cache[defaultNamespace, type];
       if (this.tempAssembly == null){
          importer1 = new XmlReflectionImporter(defaultNamespace);
          this.tempAssembly = XmlSerializer.GenerateTempAssembly(importer1.ImportTypeMapping(type));
          XmlSerializer.cache.Add(defaultNamespace, type, this.tempAssembly);
     }
}
finally{  Monitor.Exit(cache1); } } }

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.