The Labs \ Source Viewer \ SSCLI \ System.Xml.Serialization \ XmlSerializerFactory

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlSerializer.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. // <owner current="true" primary="true">ElenaK</owner>
  15. //------------------------------------------------------------------------------
  16. namespace System.Xml.Serialization
  17. {
  18.    
  19.     using System.Reflection;
  20.     using System.Collections;
  21.     using System.IO;
  22.     using System.Xml.Schema;
  23.     using System;
  24.     using System.Text;
  25.     using System.Threading;
  26.     using System.Globalization;
  27.     using System.Security.Permissions;
  28.     using System.Security.Policy;
  29.     using System.Xml.Serialization.Configuration;
  30.     using System.Diagnostics;
  31.     using System.CodeDom.Compiler;
  32.    
  33.    
  34.     /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory"]/*' />
  35.     /// <devdoc>
  36.     /// <para>[To be supplied.]</para>
  37.     /// </devdoc>
  38.     public class XmlSerializerFactory
  39.     {
  40.         static TempAssemblyCache cache = new TempAssemblyCache();
  41.        
  42.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer"]/*' />
  43.         /// <devdoc>
  44.         /// <para>[To be supplied.]</para>
  45.         /// </devdoc>
  46.         public XmlSerializer CreateSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace)
  47.         {
  48.             return CreateSerializer(type, overrides, extraTypes, root, defaultNamespace, null, null);
  49.         }
  50.        
  51.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer2"]/*' />
  52.         /// <devdoc>
  53.         /// <para>[To be supplied.]</para>
  54.         /// </devdoc>
  55.         public XmlSerializer CreateSerializer(Type type, XmlRootAttribute root)
  56.         {
  57.             return CreateSerializer(type, null, new Type[0], root, null, null, null);
  58.         }
  59.        
  60.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer3"]/*' />
  61.         /// <devdoc>
  62.         /// <para>[To be supplied.]</para>
  63.         /// </devdoc>
  64.         public XmlSerializer CreateSerializer(Type type, Type[] extraTypes)
  65.         {
  66.             return CreateSerializer(type, null, extraTypes, null, null, null, null);
  67.         }
  68.        
  69.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer4"]/*' />
  70.         /// <devdoc>
  71.         /// <para>[To be supplied.]</para>
  72.         /// </devdoc>
  73.         public XmlSerializer CreateSerializer(Type type, XmlAttributeOverrides overrides)
  74.         {
  75.             return CreateSerializer(type, overrides, new Type[0], null, null, null, null);
  76.         }
  77.        
  78.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer5"]/*' />
  79.         /// <devdoc>
  80.         /// <para>[To be supplied.]</para>
  81.         /// </devdoc>
  82.         public XmlSerializer CreateSerializer(XmlTypeMapping xmlTypeMapping)
  83.         {
  84.             TempAssembly tempAssembly = XmlSerializer.GenerateTempAssembly(xmlTypeMapping);
  85.             return (XmlSerializer)tempAssembly.Contract.TypedSerializers[xmlTypeMapping.Key];
  86.         }
  87.        
  88.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer6"]/*' />
  89.         /// <devdoc>
  90.         /// <para>[To be supplied.]</para>
  91.         /// </devdoc>
  92.         public XmlSerializer CreateSerializer(Type type)
  93.         {
  94.             return CreateSerializer(type, (string)null);
  95.         }
  96.        
  97.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer1"]/*' />
  98.         /// <devdoc>
  99.         /// <para>[To be supplied.]</para>
  100.         /// </devdoc>
  101.         public XmlSerializer CreateSerializer(Type type, string defaultNamespace)
  102.         {
  103.             if (type == null)
  104.                 throw new ArgumentNullException("type");
  105.             TempAssembly tempAssembly = cache[defaultNamespace, type];
  106.             XmlTypeMapping mapping = null;
  107.             if (tempAssembly == null) {
  108.                 lock (cache) {
  109.                     tempAssembly = cache[defaultNamespace, type];
  110.                     if (tempAssembly == null) {
  111.                         XmlSerializerImplementation contract;
  112.                         Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out contract);
  113.                         if (assembly == null) {
  114.                             // need to reflect and generate new serialization assembly
  115.                             XmlReflectionImporter importer = new XmlReflectionImporter(defaultNamespace);
  116.                             mapping = importer.ImportTypeMapping(type, null, defaultNamespace);
  117.                             tempAssembly = XmlSerializer.GenerateTempAssembly(mapping, type, defaultNamespace);
  118.                         }
  119.                         else {
  120.                             tempAssembly = new TempAssembly(contract);
  121.                         }
  122.                         cache.Add(defaultNamespace, type, tempAssembly);
  123.                     }
  124.                 }
  125.             }
  126.             if (mapping == null) {
  127.                 mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
  128.             }
  129.             return (XmlSerializer)tempAssembly.Contract.GetSerializer(type);
  130.         }
  131.        
  132.         /// <include file='doc\XmlSerializerFactory.uex' path='docs/doc[@for="XmlSerializerFactory.CreateSerializer7"]/*' />
  133.         /// <devdoc>
  134.         /// <para>[To be supplied.]</para>
  135.         /// </devdoc>
  136.         public XmlSerializer CreateSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence)
  137.         {
  138.             if (type == null)
  139.                 throw new ArgumentNullException("type");
  140.             XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
  141.             for (int i = 0; i < extraTypes.Length; i++)
  142.                 importer.IncludeType(extraTypes[i]);
  143.             XmlTypeMapping mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
  144.             TempAssembly tempAssembly = XmlSerializer.GenerateTempAssembly(mapping, type, defaultNamespace, location, evidence);
  145.             return (XmlSerializer)tempAssembly.Contract.TypedSerializers[mapping.Key];
  146.         }
  147.     }
  148. }

Developer Fusion