mscorlib(4.0.0.0) API with additions
XmlSerializer.cs
2 using System.Collections;
4 using System.IO;
5 using System.Reflection;
8 using System.Threading;
9 
11 {
13  [global::__DynamicallyInvokable]
14  public class XmlSerializer
15  {
16  private class XmlSerializerMappingKey
17  {
18  public XmlMapping Mapping;
19 
20  public XmlSerializerMappingKey(XmlMapping mapping)
21  {
22  Mapping = mapping;
23  }
24 
25  public override bool Equals(object obj)
26  {
27  XmlSerializerMappingKey xmlSerializerMappingKey = obj as XmlSerializerMappingKey;
28  if (xmlSerializerMappingKey == null)
29  {
30  return false;
31  }
32  if (Mapping.Key != xmlSerializerMappingKey.Mapping.Key)
33  {
34  return false;
35  }
36  if (Mapping.ElementName != xmlSerializerMappingKey.Mapping.ElementName)
37  {
38  return false;
39  }
40  if (Mapping.Namespace != xmlSerializerMappingKey.Mapping.Namespace)
41  {
42  return false;
43  }
44  if (Mapping.IsSoap != xmlSerializerMappingKey.Mapping.IsSoap)
45  {
46  return false;
47  }
48  return true;
49  }
50 
51  public override int GetHashCode()
52  {
53  int num = (!Mapping.IsSoap) ? 1 : 0;
54  if (Mapping.Key != null)
55  {
56  num ^= Mapping.Key.GetHashCode();
57  }
58  if (Mapping.ElementName != null)
59  {
60  num ^= Mapping.ElementName.GetHashCode();
61  }
62  if (Mapping.Namespace != null)
63  {
64  num ^= Mapping.Namespace.GetHashCode();
65  }
66  return num;
67  }
68  }
69 
70  private TempAssembly tempAssembly;
71 
72  private bool typedSerializer;
73 
74  private Type primitiveType;
75 
76  private XmlMapping mapping;
77 
78  private XmlDeserializationEvents events;
79 
80  private static TempAssemblyCache cache = new TempAssemblyCache();
81 
82  private static volatile XmlSerializerNamespaces defaultNamespaces;
83 
84  private static Hashtable xmlSerializerTable = new Hashtable();
85 
86  private static XmlSerializerNamespaces DefaultNamespaces
87  {
88  get
89  {
90  if (defaultNamespaces == null)
91  {
92  XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
93  xmlSerializerNamespaces.AddInternal("xsi", "http://www.w3.org/2001/XMLSchema-instance");
94  xmlSerializerNamespaces.AddInternal("xsd", "http://www.w3.org/2001/XMLSchema");
95  if (defaultNamespaces == null)
96  {
97  defaultNamespaces = xmlSerializerNamespaces;
98  }
99  }
100  return defaultNamespaces;
101  }
102  }
103 
105  public event XmlNodeEventHandler UnknownNode
106  {
107  add
108  {
109  ref XmlDeserializationEvents reference = ref events;
110  reference.OnUnknownNode = (XmlNodeEventHandler)Delegate.Combine(reference.OnUnknownNode, value);
111  }
112  remove
113  {
114  ref XmlDeserializationEvents reference = ref events;
115  reference.OnUnknownNode = (XmlNodeEventHandler)Delegate.Remove(reference.OnUnknownNode, value);
116  }
117  }
118 
121  {
122  add
123  {
124  ref XmlDeserializationEvents reference = ref events;
125  reference.OnUnknownAttribute = (XmlAttributeEventHandler)Delegate.Combine(reference.OnUnknownAttribute, value);
126  }
127  remove
128  {
129  ref XmlDeserializationEvents reference = ref events;
130  reference.OnUnknownAttribute = (XmlAttributeEventHandler)Delegate.Remove(reference.OnUnknownAttribute, value);
131  }
132  }
133 
136  {
137  add
138  {
139  ref XmlDeserializationEvents reference = ref events;
140  reference.OnUnknownElement = (XmlElementEventHandler)Delegate.Combine(reference.OnUnknownElement, value);
141  }
142  remove
143  {
144  ref XmlDeserializationEvents reference = ref events;
145  reference.OnUnknownElement = (XmlElementEventHandler)Delegate.Remove(reference.OnUnknownElement, value);
146  }
147  }
148 
151  {
152  add
153  {
154  ref XmlDeserializationEvents reference = ref events;
155  reference.OnUnreferencedObject = (UnreferencedObjectEventHandler)Delegate.Combine(reference.OnUnreferencedObject, value);
156  }
157  remove
158  {
159  ref XmlDeserializationEvents reference = ref events;
160  reference.OnUnreferencedObject = (UnreferencedObjectEventHandler)Delegate.Remove(reference.OnUnreferencedObject, value);
161  }
162  }
163 
165  [global::__DynamicallyInvokable]
166  protected XmlSerializer()
167  {
168  }
169 
176  [global::__DynamicallyInvokable]
177  public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace)
178  : this(type, overrides, extraTypes, root, defaultNamespace, null)
179  {
180  }
181 
185  [global::__DynamicallyInvokable]
187  : this(type, null, new Type[0], root, null, null)
188  {
189  }
190 
194  [global::__DynamicallyInvokable]
195  public XmlSerializer(Type type, Type[] extraTypes)
196  : this(type, null, extraTypes, null, null, null)
197  {
198  }
199 
203  [global::__DynamicallyInvokable]
204  public XmlSerializer(Type type, XmlAttributeOverrides overrides)
205  : this(type, overrides, new Type[0], null, null, null)
206  {
207  }
208 
211  [global::__DynamicallyInvokable]
212  public XmlSerializer(XmlTypeMapping xmlTypeMapping)
213  {
214  tempAssembly = GenerateTempAssembly(xmlTypeMapping);
215  mapping = xmlTypeMapping;
216  }
217 
220  [global::__DynamicallyInvokable]
221  public XmlSerializer(Type type)
222  : this(type, (string)null)
223  {
224  }
225 
229  [global::__DynamicallyInvokable]
230  public XmlSerializer(Type type, string defaultNamespace)
231  {
232  if (type == null)
233  {
234  throw new ArgumentNullException("type");
235  }
236  mapping = GetKnownMapping(type, defaultNamespace);
237  if (mapping != null)
238  {
239  primitiveType = type;
240  return;
241  }
242  tempAssembly = cache[defaultNamespace, type];
243  if (tempAssembly == null)
244  {
245  lock (cache)
246  {
247  tempAssembly = cache[defaultNamespace, type];
248  if (tempAssembly == null)
249  {
251  Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out contract);
252  if (assembly == null)
253  {
254  XmlReflectionImporter xmlReflectionImporter = new XmlReflectionImporter(defaultNamespace);
255  mapping = xmlReflectionImporter.ImportTypeMapping(type, null, defaultNamespace);
256  tempAssembly = GenerateTempAssembly(mapping, type, defaultNamespace);
257  }
258  else
259  {
260  mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
261  tempAssembly = new TempAssembly(new XmlMapping[1]
262  {
263  mapping
264  }, assembly, contract);
265  }
266  }
267  cache.Add(defaultNamespace, type, tempAssembly);
268  }
269  }
270  if (mapping == null)
271  {
272  mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
273  }
274  }
275 
283  public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location)
284  : this(type, overrides, extraTypes, root, defaultNamespace, location, null)
285  {
286  }
287 
296  [Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use a XmlSerializer constructor overload which does not take an Evidence parameter. See http://go2.microsoft.com/fwlink/?LinkId=131738 for more information.")]
297  public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence)
298  {
299  if (type == null)
300  {
301  throw new ArgumentNullException("type");
302  }
303  XmlReflectionImporter xmlReflectionImporter = new XmlReflectionImporter(overrides, defaultNamespace);
304  if (extraTypes != null)
305  {
306  for (int i = 0; i < extraTypes.Length; i++)
307  {
308  xmlReflectionImporter.IncludeType(extraTypes[i]);
309  }
310  }
311  mapping = xmlReflectionImporter.ImportTypeMapping(type, root, defaultNamespace);
312  if (location != null || evidence != null)
313  {
314  DemandForUserLocationOrEvidence();
315  }
316  tempAssembly = GenerateTempAssembly(mapping, type, defaultNamespace, location, evidence);
317  }
318 
319  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
320  private void DemandForUserLocationOrEvidence()
321  {
322  }
323 
324  internal static TempAssembly GenerateTempAssembly(XmlMapping xmlMapping)
325  {
326  return GenerateTempAssembly(xmlMapping, null, null);
327  }
328 
329  internal static TempAssembly GenerateTempAssembly(XmlMapping xmlMapping, Type type, string defaultNamespace)
330  {
331  if (xmlMapping == null)
332  {
333  throw new ArgumentNullException("xmlMapping");
334  }
335  return new TempAssembly(new XmlMapping[1]
336  {
337  xmlMapping
338  }, new Type[1]
339  {
340  type
341  }, defaultNamespace, null, null);
342  }
343 
344  internal static TempAssembly GenerateTempAssembly(XmlMapping xmlMapping, Type type, string defaultNamespace, string location, Evidence evidence)
345  {
346  return new TempAssembly(new XmlMapping[1]
347  {
348  xmlMapping
349  }, new Type[1]
350  {
351  type
352  }, defaultNamespace, location, evidence);
353  }
354 
358  [global::__DynamicallyInvokable]
359  public void Serialize(TextWriter textWriter, object o)
360  {
361  Serialize(textWriter, o, null);
362  }
363 
369  [global::__DynamicallyInvokable]
370  public void Serialize(TextWriter textWriter, object o, XmlSerializerNamespaces namespaces)
371  {
372  XmlTextWriter xmlTextWriter = new XmlTextWriter(textWriter);
373  xmlTextWriter.Formatting = Formatting.Indented;
374  xmlTextWriter.Indentation = 2;
375  Serialize(xmlTextWriter, o, namespaces);
376  }
377 
382  [global::__DynamicallyInvokable]
383  public void Serialize(Stream stream, object o)
384  {
385  Serialize(stream, o, null);
386  }
387 
393  [global::__DynamicallyInvokable]
394  public void Serialize(Stream stream, object o, XmlSerializerNamespaces namespaces)
395  {
396  XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, null);
397  xmlTextWriter.Formatting = Formatting.Indented;
398  xmlTextWriter.Indentation = 2;
399  Serialize(xmlTextWriter, o, namespaces);
400  }
401 
406  [global::__DynamicallyInvokable]
407  public void Serialize(XmlWriter xmlWriter, object o)
408  {
409  Serialize(xmlWriter, o, null);
410  }
411 
417  [global::__DynamicallyInvokable]
418  public void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces)
419  {
420  Serialize(xmlWriter, o, namespaces, null);
421  }
422 
429  public void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle)
430  {
431  Serialize(xmlWriter, o, namespaces, encodingStyle, null);
432  }
433 
440  public void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle, string id)
441  {
442  try
443  {
444  if (primitiveType != null)
445  {
446  if (encodingStyle != null && encodingStyle.Length > 0)
447  {
448  throw new InvalidOperationException(Res.GetString("XmlInvalidEncodingNotEncoded1", encodingStyle));
449  }
450  SerializePrimitive(xmlWriter, o, namespaces);
451  }
452  else if (tempAssembly == null || typedSerializer)
453  {
454  XmlSerializationWriter xmlSerializationWriter = CreateWriter();
455  xmlSerializationWriter.Init(xmlWriter, (namespaces == null || namespaces.Count == 0) ? DefaultNamespaces : namespaces, encodingStyle, id, tempAssembly);
456  try
457  {
458  Serialize(o, xmlSerializationWriter);
459  }
460  finally
461  {
462  xmlSerializationWriter.Dispose();
463  }
464  }
465  else
466  {
467  tempAssembly.InvokeWriter(mapping, xmlWriter, o, (namespaces == null || namespaces.Count == 0) ? DefaultNamespaces : namespaces, encodingStyle, id);
468  }
469  }
470  catch (Exception ex)
471  {
472  Exception ex2 = ex;
474  {
475  throw;
476  }
477  if (ex2 is TargetInvocationException)
478  {
479  ex2 = ex2.InnerException;
480  }
481  throw new InvalidOperationException(Res.GetString("XmlGenError"), ex2);
482  }
483  xmlWriter.Flush();
484  }
485 
489  [global::__DynamicallyInvokable]
490  public object Deserialize(Stream stream)
491  {
492  XmlTextReader xmlTextReader = new XmlTextReader(stream);
493  xmlTextReader.WhitespaceHandling = WhitespaceHandling.Significant;
494  xmlTextReader.Normalization = true;
495  xmlTextReader.XmlResolver = null;
496  return Deserialize(xmlTextReader, null);
497  }
498 
503  [global::__DynamicallyInvokable]
504  public object Deserialize(TextReader textReader)
505  {
506  XmlTextReader xmlTextReader = new XmlTextReader(textReader);
507  xmlTextReader.WhitespaceHandling = WhitespaceHandling.Significant;
508  xmlTextReader.Normalization = true;
509  xmlTextReader.XmlResolver = null;
510  return Deserialize(xmlTextReader, null);
511  }
512 
517  [global::__DynamicallyInvokable]
518  public object Deserialize(XmlReader xmlReader)
519  {
520  return Deserialize(xmlReader, null);
521  }
522 
527  public object Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
528  {
529  return Deserialize(xmlReader, null, events);
530  }
531 
537  public object Deserialize(XmlReader xmlReader, string encodingStyle)
538  {
539  return Deserialize(xmlReader, encodingStyle, events);
540  }
541 
547  public object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
548  {
549  events.sender = this;
550  try
551  {
552  if (primitiveType != null)
553  {
554  if (encodingStyle != null && encodingStyle.Length > 0)
555  {
556  throw new InvalidOperationException(Res.GetString("XmlInvalidEncodingNotEncoded1", encodingStyle));
557  }
558  return DeserializePrimitive(xmlReader, events);
559  }
560  if (tempAssembly == null || typedSerializer)
561  {
562  XmlSerializationReader xmlSerializationReader = CreateReader();
563  xmlSerializationReader.Init(xmlReader, events, encodingStyle, tempAssembly);
564  try
565  {
566  return Deserialize(xmlSerializationReader);
567  }
568  finally
569  {
570  xmlSerializationReader.Dispose();
571  }
572  }
573  return tempAssembly.InvokeReader(mapping, xmlReader, events, encodingStyle);
574  }
575  catch (Exception ex)
576  {
577  Exception ex2 = ex;
579  {
580  throw;
581  }
582  if (ex2 is TargetInvocationException)
583  {
584  ex2 = ex2.InnerException;
585  }
586  if (xmlReader is IXmlLineInfo)
587  {
588  IXmlLineInfo xmlLineInfo = (IXmlLineInfo)xmlReader;
589  throw new InvalidOperationException(Res.GetString("XmlSerializeErrorDetails", xmlLineInfo.LineNumber.ToString(CultureInfo.InvariantCulture), xmlLineInfo.LinePosition.ToString(CultureInfo.InvariantCulture)), ex2);
590  }
591  throw new InvalidOperationException(Res.GetString("XmlSerializeError"), ex2);
592  }
593  }
594 
599  [global::__DynamicallyInvokable]
600  public virtual bool CanDeserialize(XmlReader xmlReader)
601  {
602  if (primitiveType != null)
603  {
604  TypeDesc typeDesc = (TypeDesc)TypeScope.PrimtiveTypes[primitiveType];
605  return xmlReader.IsStartElement(typeDesc.DataType.Name, string.Empty);
606  }
607  if (tempAssembly != null)
608  {
609  return tempAssembly.CanRead(mapping, xmlReader);
610  }
611  return false;
612  }
613 
617  [global::__DynamicallyInvokable]
618  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
619  public static XmlSerializer[] FromMappings(XmlMapping[] mappings)
620  {
621  return FromMappings(mappings, (Type)null);
622  }
623 
628  [global::__DynamicallyInvokable]
629  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
630  public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Type type)
631  {
632  if (mappings == null || mappings.Length == 0)
633  {
634  return new XmlSerializer[0];
635  }
636  XmlSerializerImplementation contract = null;
637  Assembly left = (type == null) ? null : TempAssembly.LoadGeneratedAssembly(type, null, out contract);
638  TempAssembly tempAssembly = null;
639  if (left == null)
640  {
641  if (XmlMapping.IsShallow(mappings))
642  {
643  return new XmlSerializer[0];
644  }
645  if (type == null)
646  {
647  tempAssembly = new TempAssembly(mappings, new Type[1]
648  {
649  type
650  }, null, null, null);
651  XmlSerializer[] array = new XmlSerializer[mappings.Length];
652  contract = tempAssembly.Contract;
653  for (int i = 0; i < array.Length; i++)
654  {
655  array[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
656  array[i].SetTempAssembly(tempAssembly, mappings[i]);
657  }
658  return array;
659  }
660  return GetSerializersFromCache(mappings, type);
661  }
662  XmlSerializer[] array2 = new XmlSerializer[mappings.Length];
663  for (int j = 0; j < array2.Length; j++)
664  {
665  array2[j] = (XmlSerializer)contract.TypedSerializers[mappings[j].Key];
666  }
667  return array2;
668  }
669 
670  private static XmlSerializer[] GetSerializersFromCache(XmlMapping[] mappings, Type type)
671  {
672  XmlSerializer[] array = new XmlSerializer[mappings.Length];
673  Hashtable hashtable = null;
674  lock (xmlSerializerTable)
675  {
676  hashtable = (xmlSerializerTable[type] as Hashtable);
677  if (hashtable == null)
678  {
679  hashtable = new Hashtable();
680  xmlSerializerTable[type] = hashtable;
681  }
682  }
683  lock (hashtable)
684  {
685  Hashtable hashtable2 = new Hashtable();
686  for (int i = 0; i < mappings.Length; i++)
687  {
688  XmlSerializerMappingKey key = new XmlSerializerMappingKey(mappings[i]);
689  array[i] = (hashtable[key] as XmlSerializer);
690  if (array[i] == null)
691  {
692  hashtable2.Add(key, i);
693  }
694  }
695  if (hashtable2.Count > 0)
696  {
697  XmlMapping[] array2 = new XmlMapping[hashtable2.Count];
698  int num = 0;
699  foreach (XmlSerializerMappingKey key2 in hashtable2.Keys)
700  {
701  array2[num++] = key2.Mapping;
702  }
703  TempAssembly tempAssembly = new TempAssembly(array2, new Type[1]
704  {
705  type
706  }, null, null, null);
707  XmlSerializerImplementation contract = tempAssembly.Contract;
708  {
709  foreach (XmlSerializerMappingKey key3 in hashtable2.Keys)
710  {
711  num = (int)hashtable2[key3];
712  array[num] = (XmlSerializer)contract.TypedSerializers[key3.Mapping.Key];
713  array[num].SetTempAssembly(tempAssembly, key3.Mapping);
714  hashtable[key3] = array[num];
715  }
716  return array;
717  }
718  }
719  return array;
720  }
721  }
722 
727  [Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of FromMappings which does not take an Evidence parameter. See http://go2.microsoft.com/fwlink/?LinkId=131738 for more information.")]
728  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
729  public static XmlSerializer[] FromMappings(XmlMapping[] mappings, Evidence evidence)
730  {
731  if (mappings == null || mappings.Length == 0)
732  {
733  return new XmlSerializer[0];
734  }
735  if (XmlMapping.IsShallow(mappings))
736  {
737  return new XmlSerializer[0];
738  }
739  TempAssembly tempAssembly = new TempAssembly(mappings, new Type[0], null, null, evidence);
740  XmlSerializerImplementation contract = tempAssembly.Contract;
741  XmlSerializer[] array = new XmlSerializer[mappings.Length];
742  for (int i = 0; i < array.Length; i++)
743  {
744  array[i] = (XmlSerializer)contract.TypedSerializers[mappings[i].Key];
745  }
746  return array;
747  }
748 
753  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
754  public static Assembly GenerateSerializer(Type[] types, XmlMapping[] mappings)
755  {
756  CompilerParameters compilerParameters = new CompilerParameters();
757  compilerParameters.TempFiles = new TempFileCollection();
758  compilerParameters.GenerateInMemory = false;
759  compilerParameters.IncludeDebugInformation = false;
760  return GenerateSerializer(types, mappings, compilerParameters);
761  }
762 
768  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
769  public static Assembly GenerateSerializer(Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
770  {
771  if (types == null || types.Length == 0)
772  {
773  return null;
774  }
775  if (mappings == null)
776  {
777  throw new ArgumentNullException("mappings");
778  }
779  if (XmlMapping.IsShallow(mappings))
780  {
781  throw new InvalidOperationException(Res.GetString("XmlMelformMapping"));
782  }
783  Assembly assembly = null;
784  foreach (Type type in types)
785  {
786  if (DynamicAssemblies.IsTypeDynamic(type))
787  {
788  throw new InvalidOperationException(Res.GetString("XmlPregenTypeDynamic", type.FullName));
789  }
790  if (assembly == null)
791  {
792  assembly = type.Assembly;
793  }
794  else if (type.Assembly != assembly)
795  {
796  throw new ArgumentException(Res.GetString("XmlPregenOrphanType", type.FullName, assembly.Location), "types");
797  }
798  }
799  return TempAssembly.GenerateAssembly(mappings, types, null, null, XmlSerializerCompilerParameters.Create(parameters, needTempDirAccess: true), assembly, new Hashtable());
800  }
801 
805  [global::__DynamicallyInvokable]
806  public static XmlSerializer[] FromTypes(Type[] types)
807  {
808  if (types == null)
809  {
810  return new XmlSerializer[0];
811  }
812  XmlReflectionImporter xmlReflectionImporter = new XmlReflectionImporter();
813  XmlTypeMapping[] array = new XmlTypeMapping[types.Length];
814  for (int i = 0; i < types.Length; i++)
815  {
816  array[i] = xmlReflectionImporter.ImportTypeMapping(types[i]);
817  }
818  return FromMappings(array);
819  }
820 
824  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
825  public static string GetXmlSerializerAssemblyName(Type type)
826  {
827  return GetXmlSerializerAssemblyName(type, null);
828  }
829 
834  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
835  public static string GetXmlSerializerAssemblyName(Type type, string defaultNamespace)
836  {
837  if (type == null)
838  {
839  throw new ArgumentNullException("type");
840  }
841  return Compiler.GetTempAssemblyName(type.Assembly.GetName(), defaultNamespace);
842  }
843 
848  {
849  throw new NotImplementedException();
850  }
851 
856  protected virtual object Deserialize(XmlSerializationReader reader)
857  {
858  throw new NotImplementedException();
859  }
860 
865  {
866  throw new NotImplementedException();
867  }
868 
873  protected virtual void Serialize(object o, XmlSerializationWriter writer)
874  {
875  throw new NotImplementedException();
876  }
877 
878  internal void SetTempAssembly(TempAssembly tempAssembly, XmlMapping mapping)
879  {
880  this.tempAssembly = tempAssembly;
881  this.mapping = mapping;
882  typedSerializer = true;
883  }
884 
885  private static XmlTypeMapping GetKnownMapping(Type type, string ns)
886  {
887  if (ns != null && ns != string.Empty)
888  {
889  return null;
890  }
891  TypeDesc typeDesc = (TypeDesc)TypeScope.PrimtiveTypes[type];
892  if (typeDesc == null)
893  {
894  return null;
895  }
896  ElementAccessor elementAccessor = new ElementAccessor();
897  elementAccessor.Name = typeDesc.DataType.Name;
898  XmlTypeMapping xmlTypeMapping = new XmlTypeMapping(null, elementAccessor);
899  xmlTypeMapping.SetKeyInternal(XmlMapping.GenerateKey(type, null, null));
900  return xmlTypeMapping;
901  }
902 
903  private void SerializePrimitive(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces)
904  {
905  XmlSerializationPrimitiveWriter xmlSerializationPrimitiveWriter = new XmlSerializationPrimitiveWriter();
906  xmlSerializationPrimitiveWriter.Init(xmlWriter, namespaces, null, null, null);
907  switch (Type.GetTypeCode(primitiveType))
908  {
909  case TypeCode.String:
910  xmlSerializationPrimitiveWriter.Write_string(o);
911  return;
912  case TypeCode.Int32:
913  xmlSerializationPrimitiveWriter.Write_int(o);
914  return;
915  case TypeCode.Boolean:
916  xmlSerializationPrimitiveWriter.Write_boolean(o);
917  return;
918  case TypeCode.Int16:
919  xmlSerializationPrimitiveWriter.Write_short(o);
920  return;
921  case TypeCode.Int64:
922  xmlSerializationPrimitiveWriter.Write_long(o);
923  return;
924  case TypeCode.Single:
925  xmlSerializationPrimitiveWriter.Write_float(o);
926  return;
927  case TypeCode.Double:
928  xmlSerializationPrimitiveWriter.Write_double(o);
929  return;
930  case TypeCode.Decimal:
931  xmlSerializationPrimitiveWriter.Write_decimal(o);
932  return;
933  case TypeCode.DateTime:
934  xmlSerializationPrimitiveWriter.Write_dateTime(o);
935  return;
936  case TypeCode.Char:
937  xmlSerializationPrimitiveWriter.Write_char(o);
938  return;
939  case TypeCode.Byte:
940  xmlSerializationPrimitiveWriter.Write_unsignedByte(o);
941  return;
942  case TypeCode.SByte:
943  xmlSerializationPrimitiveWriter.Write_byte(o);
944  return;
945  case TypeCode.UInt16:
946  xmlSerializationPrimitiveWriter.Write_unsignedShort(o);
947  return;
948  case TypeCode.UInt32:
949  xmlSerializationPrimitiveWriter.Write_unsignedInt(o);
950  return;
951  case TypeCode.UInt64:
952  xmlSerializationPrimitiveWriter.Write_unsignedLong(o);
953  return;
954  }
955  if (primitiveType == typeof(XmlQualifiedName))
956  {
957  xmlSerializationPrimitiveWriter.Write_QName(o);
958  return;
959  }
960  if (primitiveType == typeof(byte[]))
961  {
962  xmlSerializationPrimitiveWriter.Write_base64Binary(o);
963  return;
964  }
965  if (primitiveType == typeof(Guid))
966  {
967  xmlSerializationPrimitiveWriter.Write_guid(o);
968  return;
969  }
970  if (primitiveType == typeof(TimeSpan))
971  {
972  xmlSerializationPrimitiveWriter.Write_TimeSpan(o);
973  return;
974  }
975  throw new InvalidOperationException(Res.GetString("XmlUnxpectedType", primitiveType.FullName));
976  }
977 
978  private object DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)
979  {
980  XmlSerializationPrimitiveReader xmlSerializationPrimitiveReader = new XmlSerializationPrimitiveReader();
981  xmlSerializationPrimitiveReader.Init(xmlReader, events, null, null);
982  switch (Type.GetTypeCode(primitiveType))
983  {
984  case TypeCode.String:
985  return xmlSerializationPrimitiveReader.Read_string();
986  case TypeCode.Int32:
987  return xmlSerializationPrimitiveReader.Read_int();
988  case TypeCode.Boolean:
989  return xmlSerializationPrimitiveReader.Read_boolean();
990  case TypeCode.Int16:
991  return xmlSerializationPrimitiveReader.Read_short();
992  case TypeCode.Int64:
993  return xmlSerializationPrimitiveReader.Read_long();
994  case TypeCode.Single:
995  return xmlSerializationPrimitiveReader.Read_float();
996  case TypeCode.Double:
997  return xmlSerializationPrimitiveReader.Read_double();
998  case TypeCode.Decimal:
999  return xmlSerializationPrimitiveReader.Read_decimal();
1000  case TypeCode.DateTime:
1001  return xmlSerializationPrimitiveReader.Read_dateTime();
1002  case TypeCode.Char:
1003  return xmlSerializationPrimitiveReader.Read_char();
1004  case TypeCode.Byte:
1005  return xmlSerializationPrimitiveReader.Read_unsignedByte();
1006  case TypeCode.SByte:
1007  return xmlSerializationPrimitiveReader.Read_byte();
1008  case TypeCode.UInt16:
1009  return xmlSerializationPrimitiveReader.Read_unsignedShort();
1010  case TypeCode.UInt32:
1011  return xmlSerializationPrimitiveReader.Read_unsignedInt();
1012  case TypeCode.UInt64:
1013  return xmlSerializationPrimitiveReader.Read_unsignedLong();
1014  default:
1015  if (primitiveType == typeof(XmlQualifiedName))
1016  {
1017  return xmlSerializationPrimitiveReader.Read_QName();
1018  }
1019  if (primitiveType == typeof(byte[]))
1020  {
1021  return xmlSerializationPrimitiveReader.Read_base64Binary();
1022  }
1023  if (primitiveType == typeof(Guid))
1024  {
1025  return xmlSerializationPrimitiveReader.Read_guid();
1026  }
1027  if (primitiveType == typeof(TimeSpan) && System.LocalAppContextSwitches.EnableTimeSpanSerialization)
1028  {
1029  return xmlSerializationPrimitiveReader.Read_TimeSpan();
1030  }
1031  throw new InvalidOperationException(Res.GetString("XmlUnxpectedType", primitiveType.FullName));
1032  }
1033  }
1034  }
1035 }
Provides an interface to enable a class to return line and position information.
Definition: IXmlLineInfo.cs:5
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
XmlResolver XmlResolver
Sets the T:System.Xml.XmlResolver used for resolving DTD references.
delegate void UnreferencedObjectEventHandler(object sender, UnreferencedObjectEventArgs e)
Represents the method that handles the E:System.Xml.Serialization.XmlSerializer.UnreferencedObject ev...
virtual object Deserialize(XmlSerializationReader reader)
Deserializes the XML document contained by the specified T:System.Xml.Serialization....
void Serialize(XmlWriter xmlWriter, object o)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
string Namespace
Gets the namespace of the mapped element.
Definition: XmlMapping.cs:53
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
TempFileCollection TempFiles
Gets or sets the collection that contains the temporary files.
static Assembly GenerateSerializer(Type[] types, XmlMapping[] mappings, CompilerParameters parameters)
Returns an assembly that contains custom-made serializers used to serialize or deserialize the specif...
Contains the XML namespaces and prefixes that the T:System.Xml.Serialization.XmlSerializer uses to ge...
void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle)
Serializes the specified object and writes the XML document to a file using the specified T:System....
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
XmlAttributeEventHandler UnknownAttribute
Occurs when the T:System.Xml.Serialization.XmlSerializer encounters an XML attribute of unknown type ...
int Indentation
Gets or sets how many IndentChars to write for each level in the hierarchy when P:System....
object Deserialize(TextReader textReader)
Deserializes the XML document contained by the specified T:System.IO.TextReader.
The exception that is thrown when the execution stack overflows because it contains too many nested m...
virtual XmlSerializationReader CreateReader()
Returns an object used to read the XML document to be serialized.
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
Definition: Type.cs:153
XmlNodeEventHandler OnUnknownNode
Gets or sets an object that represents the method that handles the E:System.Xml.Serialization....
virtual void Serialize(object o, XmlSerializationWriter writer)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
void Serialize(Stream stream, object o)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
int LinePosition
Gets the current line position.
Definition: IXmlLineInfo.cs:20
void IncludeType(Type type)
Includes mappings for a type for later use when import methods are invoked.
TypeCode
Specifies the type of an object.
Definition: TypeCode.cs:9
object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
Deserializes the object using the data contained by the specified T:System.Xml.XmlReader.
static string GetXmlSerializerAssemblyName(Type type, string defaultNamespace)
Returns the name of the assembly that contains the serializer for the specified type in the specified...
XmlSerializer()
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class.
XmlSerializer(Type type, XmlRootAttribute root)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
virtual AssemblyName GetName()
Gets an T:System.Reflection.AssemblyName for this assembly.
Definition: Assembly.cs:832
object Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
Deserializes an XML document contained by the specified T:System.Xml.XmlReader and allows the overrid...
Definition: __Canon.cs:3
bool IncludeDebugInformation
Gets or sets a value indicating whether to include debug information in the compiled executable.
void Serialize(TextWriter textWriter, object o, XmlSerializerNamespaces namespaces)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
bool Normalization
Gets or sets a value indicating whether to normalize white space and attribute values.
static XmlSerializer [] FromMappings(XmlMapping[] mappings)
Returns an array of T:System.Xml.Serialization.XmlSerializer objects created from an array of T:Syste...
Represents an abstract class used for controlling serialization by the T:System.Xml....
XmlAttributeEventHandler OnUnknownAttribute
Gets or sets an object that represents the method that handles the E:System.Xml.Serialization....
static string GetXmlSerializerAssemblyName(Type type)
Returns the name of the assembly that contains one or more versions of the T:System....
virtual Hashtable TypedSerializers
Gets the collection of typed serializers that is found in the assembly.
static Delegate Remove(Delegate source, Delegate value)
Removes the last occurrence of the invocation list of a delegate from the invocation list of another ...
Definition: Delegate.cs:287
static Delegate Combine(Delegate a, Delegate b)
Concatenates the invocation lists of two delegates.
Definition: Delegate.cs:202
Represents the parameters used to invoke a compiler.
void Serialize(Stream stream, object o, XmlSerializerNamespaces namespaces)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
XmlTypeMapping ImportTypeMapping(Type type)
Generates a mapping to an XML Schema element for a specified .NET Framework type.
WhitespaceHandling
Specifies how white space is handled.
Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files t...
Definition: XmlWriter.cs:12
Represents a reader that provides fast, non-cached, forward-only access to XML data....
Controls deserialization by the T:System.Xml.Serialization.XmlSerializer class.
Represents a reader that provides fast, noncached, forward-only access to XML data....
Definition: XmlReader.cs:15
abstract void Flush()
When overridden in a derived class, flushes whatever is in the buffer to the underlying streams and a...
SecurityAction
Specifies the security actions that can be performed using declarative security.
XmlSerializer(Type type, string defaultNamespace)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
virtual bool CanDeserialize(XmlReader xmlReader)
Gets a value that indicates whether this T:System.Xml.Serialization.XmlSerializer can deserialize a s...
Controls XML serialization of the attribute target as an XML root element.
virtual bool IsStartElement()
Calls M:System.Xml.XmlReader.MoveToContent and tests if the current content node is a start tag or em...
Definition: XmlReader.cs:1515
Supports mappings between .NET Framework types and XML Schema data types.
Definition: XmlMapping.cs:5
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
virtual ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
Definition: Hashtable.cs:617
Exception InnerException
Gets the T:System.Exception instance that caused the current exception.
Definition: Exception.cs:139
Represents a writer that provides a fast, non-cached, forward-only way of generating streams or files...
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
Serializes and deserializes objects into and from XML documents. The T:System.Xml....
abstract Assembly Assembly
Gets the T:System.Reflection.Assembly in which the type is declared. For generic types,...
Definition: Type.cs:131
XmlElementEventHandler UnknownElement
Occurs when the T:System.Xml.Serialization.XmlSerializer encounters an XML element of unknown type du...
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
The exception that is thrown when there is not enough memory to continue the execution of a program.
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
static XmlSerializer [] FromTypes(Type[] types)
Returns an array of T:System.Xml.Serialization.XmlSerializer objects created from an array of types.
int LineNumber
Gets the current line number.
Definition: IXmlLineInfo.cs:11
XmlSerializer(XmlTypeMapping xmlTypeMapping)
Initializes an instance of the T:System.Xml.Serialization.XmlSerializer class using an object that ma...
delegate void XmlAttributeEventHandler(object sender, XmlAttributeEventArgs e)
Represents the method that handles the E:System.Xml.Serialization.XmlSerializer.UnknownAttribute
Generates mappings to XML schema element declarations, including literal XML Schema Definition (XSD) ...
static XmlSerializer [] FromMappings(XmlMapping[] mappings, Evidence evidence)
Returns an instance of the T:System.Xml.Serialization.XmlSerializer class created from mappings of on...
delegate void XmlNodeEventHandler(object sender, XmlNodeEventArgs e)
Represents the method that handles the E:System.Xml.Serialization.XmlSerializer.UnknownNode event of ...
static XmlSerializer [] FromMappings(XmlMapping[] mappings, Type type)
Returns an instance of the T:System.Xml.Serialization.XmlSerializer class from the specified mappings...
Formatting Formatting
Indicates how the output is formatted.
WhitespaceHandling WhitespaceHandling
Gets or sets a value that specifies how white space is handled.
int Count
Gets the number of prefix and namespace pairs in the collection.
XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
static Assembly GenerateSerializer(Type[] types, XmlMapping[] mappings)
Returns an assembly that contains custom-made serializers used to serialize or deserialize the specif...
The exception that is thrown when one of the arguments provided to a method is not valid.
UnreferencedObjectEventHandler OnUnreferencedObject
Gets or sets an object that represents the method that handles the E:System.Xml.Serialization....
object Deserialize(XmlReader xmlReader)
Deserializes the XML document contained by the specified T:System.Xml.XmlReader.
string ElementName
Get the name of the mapped element.
Definition: XmlMapping.cs:29
XmlSerializer(Type type, XmlAttributeOverrides overrides)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
bool GenerateInMemory
Gets or sets a value indicating whether to generate the output in memory.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
XmlSerializer(Type type)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
Defines the reader, writer, and methods for pre-generated, typed serializers.
UnreferencedObjectEventHandler UnreferencedObject
Occurs during deserialization of a SOAP-encoded XML stream, when the T:System.Xml....
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
Defines the set of information that constitutes input to security policy decisions....
Definition: Evidence.cs:17
virtual XmlSerializationWriter CreateWriter()
When overridden in a derived class, returns a writer used to serialize the object.
object Deserialize(Stream stream)
Deserializes the XML document contained by the specified T:System.IO.Stream.
XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
XmlElementEventHandler OnUnknownElement
Gets or sets an object that represents the method that handles the E:System.Xml.Serialization....
Formatting
Specifies formatting options for the T:System.Xml.XmlTextWriter.
Definition: Formatting.cs:4
The exception that is thrown when a method call is invalid for the object's current state.
Allows you to override property, field, and class attributes when you use the T:System....
The exception that is thrown by methods invoked through reflection. This class cannot be inherited.
Contains fields that can be used to pass event delegates to a thread-safe Overload:System....
XmlNodeEventHandler UnknownNode
Occurs when the T:System.Xml.Serialization.XmlSerializer encounters an XML node of unknown type durin...
void Serialize(TextWriter textWriter, object o)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Contains a mapping of one type to another.
The exception that is thrown when a call is made to the M:System.Threading.Thread....
void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
XmlSerializer(Type type, Type[] extraTypes)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
The exception that is thrown when a requested method or operation is not implemented.
Represents a collection of temporary files.
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658
object Deserialize(XmlReader xmlReader, string encodingStyle)
Deserializes the XML document contained by the specified T:System.Xml.XmlReader and encoding style.
delegate void XmlElementEventHandler(object sender, XmlElementEventArgs e)
Represents the method that handles the E:System.Xml.Serialization.XmlSerializer.UnknownElement event ...
XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace)
Initializes a new instance of the T:System.Xml.Serialization.XmlSerializer class that can serialize o...
void Serialize(XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces, string encodingStyle, string id)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16