13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CautionThe ma<strong>in</strong> purpose of the assembly cache is to save you fromrepeatedly re-creat<strong>in</strong>g the assembly <strong>for</strong> the same type <strong>in</strong> thesame application session. Un<strong>for</strong>tunately, this seems to work onlyif you use the simplest XmlSerializer constructor, as shown here:XmlSerializer ser = new XmlSerializer(type);All other constructors—that is, those that can acceptnamespaces, type mapp<strong>in</strong>g, and attribute overrides—never look<strong>in</strong>to the cache to f<strong>in</strong>d match<strong>in</strong>g assemblies. The net effect of thisbehavior is that if you use, say, attribute overrides, as we didearlier, the assembly <strong>for</strong> the type is generated each time youcreate the constructor, even if the type is always the same.To work around this, use global <strong>in</strong>stances of the XmlSerializerclass, one <strong>for</strong> each type you plan to work on. This workaround isnot strictly required if you use the simple constructor, but us<strong>in</strong>g aglobal serializer <strong>for</strong> each type results <strong>in</strong> slightly more efficientcode because you avoid any access to the cache, and not do<strong>in</strong>gso will certa<strong>in</strong>ly result <strong>in</strong> significantly slower code. Generat<strong>in</strong>gthe assembly pays <strong>for</strong> itself <strong>in</strong> a s<strong>in</strong>gle serializ<strong>in</strong>g or deserializ<strong>in</strong>goperation.Assembly CreationThe assembly is created from dynamically generated C# source code. The codeconta<strong>in</strong>s two classes whose names are hard-coded as XmlSerializationReader1 andXmlSerializationWriter1. The <strong>for</strong>mer class works like a tailor-made reader <strong>for</strong> the typebe<strong>in</strong>g deserialized. The latter class is an ad hoc writer that dumps out to <strong>XML</strong> thecontents of the specified object <strong>in</strong>stance. The classes are generated <strong>in</strong> the<strong>Microsoft</strong>.Xml.Serialization.GeneratedAssembly namespace.The serializer's constructor uses an <strong>in</strong>ternal code-writer object to trans<strong>for</strong>m all type<strong>in</strong><strong>for</strong>mation stored <strong>in</strong> XmlTypeMapp<strong>in</strong>g <strong>in</strong>to C# source code. The C# source file, as wellas the assembly, are generated <strong>in</strong> a temporary folder—the path returned byPath.GetTempPath. Normally, the follow<strong>in</strong>g temporary path is used:C:\Documents And Sett<strong>in</strong>gs\[user name]\Local Sett<strong>in</strong>gs\TempIf you monitor this folder with a tool like the one shown <strong>in</strong> Figure 11-5, you'll discoverwhat really happens when you call the XmlSerializer constructor.Figure 11-5: Unveil<strong>in</strong>g the clandest<strong>in</strong>e life of the temporary assembly.419

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!