Add mobile build support in System.Runtime.Serialization.
[mono.git] / mcs / class / referencesource / System.Runtime.Serialization / System / Runtime / Serialization / PrimitiveDataContract.cs
1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4 namespace System.Runtime.Serialization
5 {
6     using System;
7     using System.Collections;
8     using System.Collections.Generic;
9     using System.Globalization;
10     using System.Reflection;
11     using System.Xml;
12     using System.Security;
13
14 #if USE_REFEMIT
15     public abstract class PrimitiveDataContract : DataContract
16 #else
17     internal abstract class PrimitiveDataContract : DataContract
18 #endif
19     {
20         [Fx.Tag.SecurityNote(Critical = "Holds instance of CriticalHelper which keeps state that is cached statically for serialization."
21             + " Static fields are marked SecurityCritical or readonly to prevent data from being modified or leaked to other components in appdomain.")]
22         [SecurityCritical]
23         PrimitiveDataContractCriticalHelper helper;
24
25         [Fx.Tag.SecurityNote(Critical = "Initializes SecurityCritical field 'helper'.",
26             Safe = "Doesn't leak anything.")]
27         [SecuritySafeCritical]
28         protected PrimitiveDataContract(Type type, XmlDictionaryString name, XmlDictionaryString ns)
29             : base(new PrimitiveDataContractCriticalHelper(type, name, ns))
30         {
31             helper = base.Helper as PrimitiveDataContractCriticalHelper;
32         }
33
34         static internal PrimitiveDataContract GetPrimitiveDataContract(Type type)
35         {
36             return DataContract.GetBuiltInDataContract(type) as PrimitiveDataContract;
37         }
38
39         static internal PrimitiveDataContract GetPrimitiveDataContract(string name, string ns)
40         {
41             return DataContract.GetBuiltInDataContract(name, ns) as PrimitiveDataContract;
42         }
43
44         internal abstract string WriteMethodName { get; }
45         internal abstract string ReadMethodName { get; }
46
47         internal override XmlDictionaryString TopLevelElementNamespace
48         {
49             get { return DictionaryGlobals.SerializationNamespace; }
50             set { }
51         }
52
53         internal override bool CanContainReferences
54         {
55             get { return false; }
56         }
57
58         internal override bool IsPrimitive
59         {
60             get { return true; }
61         }
62
63         internal override bool IsBuiltInDataContract
64         {
65             get
66             {
67                 return true;
68             }
69         }
70
71         internal MethodInfo XmlFormatWriterMethod
72         {
73             [Fx.Tag.SecurityNote(Critical = "Fetches the critical XmlFormatWriterMethod property.",
74                 Safe = "XmlFormatWriterMethod only needs to be protected for write; initialized in getter if null.")]
75             [SecuritySafeCritical]
76             get
77             {
78                 if (helper.XmlFormatWriterMethod == null)
79                 {
80                     if (UnderlyingType.IsValueType)
81                         helper.XmlFormatWriterMethod = typeof(XmlWriterDelegator).GetMethod(WriteMethodName, Globals.ScanAllMembers, null, new Type[] { UnderlyingType, typeof(XmlDictionaryString), typeof(XmlDictionaryString) }, null);
82                     else
83                         helper.XmlFormatWriterMethod = typeof(XmlObjectSerializerWriteContext).GetMethod(WriteMethodName, Globals.ScanAllMembers, null, new Type[] { typeof(XmlWriterDelegator), UnderlyingType, typeof(XmlDictionaryString), typeof(XmlDictionaryString) }, null);
84                 }
85                 return helper.XmlFormatWriterMethod;
86             }
87         }
88
89         internal MethodInfo XmlFormatContentWriterMethod
90         {
91             [Fx.Tag.SecurityNote(Critical = "Fetches the critical XmlFormatContentWriterMethod property.",
92                 Safe = "XmlFormatContentWriterMethod only needs to be protected for write; initialized in getter if null.")]
93             [SecuritySafeCritical]
94             get
95             {
96                 if (helper.XmlFormatContentWriterMethod == null)
97                 {
98                     if (UnderlyingType.IsValueType)
99                         helper.XmlFormatContentWriterMethod = typeof(XmlWriterDelegator).GetMethod(WriteMethodName, Globals.ScanAllMembers, null, new Type[] { UnderlyingType }, null);
100                     else
101                         helper.XmlFormatContentWriterMethod = typeof(XmlObjectSerializerWriteContext).GetMethod(WriteMethodName, Globals.ScanAllMembers, null, new Type[] { typeof(XmlWriterDelegator), UnderlyingType }, null);
102                 }
103                 return helper.XmlFormatContentWriterMethod;
104             }
105         }
106
107         internal MethodInfo XmlFormatReaderMethod
108         {
109             [Fx.Tag.SecurityNote(Critical = "Fetches the critical XmlFormatReaderMethod property.",
110                 Safe = "XmlFormatReaderMethod only needs to be protected for write; initialized in getter if null.")]
111             [SecuritySafeCritical]
112             get
113             {
114                 if (helper.XmlFormatReaderMethod == null)
115                 {
116                     helper.XmlFormatReaderMethod = typeof(XmlReaderDelegator).GetMethod(ReadMethodName, Globals.ScanAllMembers);
117                 }
118                 return helper.XmlFormatReaderMethod;
119             }
120         }
121
122         public override void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
123         {
124             xmlWriter.WriteAnyType(obj);
125         }
126
127         protected object HandleReadValue(object obj, XmlObjectSerializerReadContext context)
128         {
129             context.AddNewObject(obj);
130             return obj;
131         }
132
133         protected bool TryReadNullAtTopLevel(XmlReaderDelegator reader)
134         {
135             Attributes attributes = new Attributes();
136             attributes.Read(reader);
137             if (attributes.Ref != Globals.NewObjectId)
138                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.CannotDeserializeRefAtTopLevel, attributes.Ref)));
139             if (attributes.XsiNil)
140             {
141                 reader.Skip();
142                 return true;
143             }
144             return false;
145         }
146
147         internal override bool Equals(object other, Dictionary<DataContractPairKey, object> checkedContracts)
148         {
149             PrimitiveDataContract dataContract = other as PrimitiveDataContract;
150             if (dataContract != null)
151             {
152                 Type thisType = this.GetType();
153                 Type otherType = other.GetType();
154                 return (thisType.Equals(otherType) || thisType.IsSubclassOf(otherType) || otherType.IsSubclassOf(thisType));
155             }
156             return false;
157         }
158
159         [Fx.Tag.SecurityNote(Critical = "Holds all state used for for (de)serializing primitives."
160             + " Since the data is cached statically, we lock down access to it.")]
161 #if !NO_SECURITY_ATTRIBUTES
162         [SecurityCritical(SecurityCriticalScope.Everything)]
163 #endif
164         class PrimitiveDataContractCriticalHelper : DataContract.DataContractCriticalHelper
165         {
166             MethodInfo xmlFormatWriterMethod;
167             MethodInfo xmlFormatContentWriterMethod;
168             MethodInfo xmlFormatReaderMethod;
169
170             internal PrimitiveDataContractCriticalHelper(Type type, XmlDictionaryString name, XmlDictionaryString ns)
171                 : base(type)
172             {
173                 SetDataContractName(name, ns);
174             }
175
176             internal MethodInfo XmlFormatWriterMethod
177             {
178                 get { return xmlFormatWriterMethod; }
179                 set { xmlFormatWriterMethod = value; }
180             }
181
182             internal MethodInfo XmlFormatContentWriterMethod
183             {
184                 get { return xmlFormatContentWriterMethod; }
185                 set { xmlFormatContentWriterMethod = value; }
186             }
187
188             internal MethodInfo XmlFormatReaderMethod
189             {
190                 get { return xmlFormatReaderMethod; }
191                 set { xmlFormatReaderMethod = value; }
192             }
193         }
194
195     }
196
197     internal class CharDataContract : PrimitiveDataContract
198     {
199         internal CharDataContract()
200             : this(DictionaryGlobals.CharLocalName, DictionaryGlobals.SerializationNamespace)
201         {
202         }
203
204         internal CharDataContract(XmlDictionaryString name, XmlDictionaryString ns)
205             : base(typeof(char), name, ns)
206         {
207         }
208
209         internal override string WriteMethodName { get { return "WriteChar"; } }
210         internal override string ReadMethodName { get { return "ReadElementContentAsChar"; } }
211
212         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
213         {
214             writer.WriteChar((char)obj);
215         }
216
217         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
218         {
219             return (context == null) ? reader.ReadElementContentAsChar()
220                 : HandleReadValue(reader.ReadElementContentAsChar(), context);
221         }
222     }
223
224     internal class AsmxCharDataContract : CharDataContract
225     {
226         internal AsmxCharDataContract() : base(DictionaryGlobals.CharLocalName, DictionaryGlobals.AsmxTypesNamespace) { }
227     }
228
229     internal class BooleanDataContract : PrimitiveDataContract
230     {
231         internal BooleanDataContract()
232             : base(typeof(bool), DictionaryGlobals.BooleanLocalName, DictionaryGlobals.SchemaNamespace)
233         {
234         }
235
236         internal override string WriteMethodName { get { return "WriteBoolean"; } }
237         internal override string ReadMethodName { get { return "ReadElementContentAsBoolean"; } }
238
239         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
240         {
241             writer.WriteBoolean((bool)obj);
242         }
243
244         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
245         {
246             return (context == null) ? reader.ReadElementContentAsBoolean()
247                 : HandleReadValue(reader.ReadElementContentAsBoolean(), context);
248         }
249     }
250
251     internal class SignedByteDataContract : PrimitiveDataContract
252     {
253         internal SignedByteDataContract()
254             : base(typeof(sbyte), DictionaryGlobals.SignedByteLocalName, DictionaryGlobals.SchemaNamespace)
255         {
256         }
257
258         internal override string WriteMethodName { get { return "WriteSignedByte"; } }
259         internal override string ReadMethodName { get { return "ReadElementContentAsSignedByte"; } }
260
261         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
262         {
263             writer.WriteSignedByte((sbyte)obj);
264         }
265
266         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
267         {
268             return (context == null) ? reader.ReadElementContentAsSignedByte()
269                 : HandleReadValue(reader.ReadElementContentAsSignedByte(), context);
270         }
271     }
272
273     internal class UnsignedByteDataContract : PrimitiveDataContract
274     {
275         internal UnsignedByteDataContract()
276             : base(typeof(byte), DictionaryGlobals.UnsignedByteLocalName, DictionaryGlobals.SchemaNamespace)
277         {
278         }
279
280         internal override string WriteMethodName { get { return "WriteUnsignedByte"; } }
281         internal override string ReadMethodName { get { return "ReadElementContentAsUnsignedByte"; } }
282
283         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
284         {
285             writer.WriteUnsignedByte((byte)obj);
286         }
287
288         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
289         {
290             return (context == null) ? reader.ReadElementContentAsUnsignedByte()
291                 : HandleReadValue(reader.ReadElementContentAsUnsignedByte(), context);
292         }
293     }
294
295     internal class ShortDataContract : PrimitiveDataContract
296     {
297         internal ShortDataContract()
298             : base(typeof(short), DictionaryGlobals.ShortLocalName, DictionaryGlobals.SchemaNamespace)
299         {
300         }
301
302         internal override string WriteMethodName { get { return "WriteShort"; } }
303         internal override string ReadMethodName { get { return "ReadElementContentAsShort"; } }
304
305         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
306         {
307             writer.WriteShort((short)obj);
308         }
309
310         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
311         {
312             return (context == null) ? reader.ReadElementContentAsShort()
313                 : HandleReadValue(reader.ReadElementContentAsShort(), context);
314         }
315     }
316
317     internal class UnsignedShortDataContract : PrimitiveDataContract
318     {
319         internal UnsignedShortDataContract()
320             : base(typeof(ushort), DictionaryGlobals.UnsignedShortLocalName, DictionaryGlobals.SchemaNamespace)
321         {
322         }
323
324         internal override string WriteMethodName { get { return "WriteUnsignedShort"; } }
325         internal override string ReadMethodName { get { return "ReadElementContentAsUnsignedShort"; } }
326
327         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
328         {
329             writer.WriteUnsignedShort((ushort)obj);
330         }
331
332         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
333         {
334             return (context == null) ? reader.ReadElementContentAsUnsignedShort()
335                 : HandleReadValue(reader.ReadElementContentAsUnsignedShort(), context);
336         }
337     }
338
339     internal class IntDataContract : PrimitiveDataContract
340     {
341         internal IntDataContract()
342             : base(typeof(int), DictionaryGlobals.IntLocalName, DictionaryGlobals.SchemaNamespace)
343         {
344         }
345
346         internal override string WriteMethodName { get { return "WriteInt"; } }
347         internal override string ReadMethodName { get { return "ReadElementContentAsInt"; } }
348
349         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
350         {
351             writer.WriteInt((int)obj);
352         }
353
354         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
355         {
356             return (context == null) ? reader.ReadElementContentAsInt()
357                 : HandleReadValue(reader.ReadElementContentAsInt(), context);
358         }
359     }
360
361     internal class UnsignedIntDataContract : PrimitiveDataContract
362     {
363         internal UnsignedIntDataContract()
364             : base(typeof(uint), DictionaryGlobals.UnsignedIntLocalName, DictionaryGlobals.SchemaNamespace)
365         {
366         }
367
368         internal override string WriteMethodName { get { return "WriteUnsignedInt"; } }
369         internal override string ReadMethodName { get { return "ReadElementContentAsUnsignedInt"; } }
370
371         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
372         {
373             writer.WriteUnsignedInt((uint)obj);
374         }
375
376         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
377         {
378             return (context == null) ? reader.ReadElementContentAsUnsignedInt()
379                 : HandleReadValue(reader.ReadElementContentAsUnsignedInt(), context);
380         }
381     }
382
383     internal class LongDataContract : PrimitiveDataContract
384     {
385         internal LongDataContract()
386             : this(DictionaryGlobals.LongLocalName, DictionaryGlobals.SchemaNamespace)
387         {
388         }
389
390         internal LongDataContract(XmlDictionaryString name, XmlDictionaryString ns)
391             : base(typeof(long), name, ns)
392         {
393         }
394
395         internal override string WriteMethodName { get { return "WriteLong"; } }
396         internal override string ReadMethodName { get { return "ReadElementContentAsLong"; } }
397
398         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
399         {
400             writer.WriteLong((long)obj);
401         }
402
403         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
404         {
405             return (context == null) ? reader.ReadElementContentAsLong()
406                 : HandleReadValue(reader.ReadElementContentAsLong(), context);
407         }
408     }
409     internal class IntegerDataContract : LongDataContract
410     {
411         internal IntegerDataContract() : base(DictionaryGlobals.integerLocalName, DictionaryGlobals.SchemaNamespace) { }
412     }
413     internal class PositiveIntegerDataContract : LongDataContract
414     {
415         internal PositiveIntegerDataContract() : base(DictionaryGlobals.positiveIntegerLocalName, DictionaryGlobals.SchemaNamespace) { }
416     }
417     internal class NegativeIntegerDataContract : LongDataContract
418     {
419         internal NegativeIntegerDataContract() : base(DictionaryGlobals.negativeIntegerLocalName, DictionaryGlobals.SchemaNamespace) { }
420     }
421     internal class NonPositiveIntegerDataContract : LongDataContract
422     {
423         internal NonPositiveIntegerDataContract() : base(DictionaryGlobals.nonPositiveIntegerLocalName, DictionaryGlobals.SchemaNamespace) { }
424     }
425     internal class NonNegativeIntegerDataContract : LongDataContract
426     {
427         internal NonNegativeIntegerDataContract() : base(DictionaryGlobals.nonNegativeIntegerLocalName, DictionaryGlobals.SchemaNamespace) { }
428     }
429
430     internal class UnsignedLongDataContract : PrimitiveDataContract
431     {
432         internal UnsignedLongDataContract()
433             : base(typeof(ulong), DictionaryGlobals.UnsignedLongLocalName, DictionaryGlobals.SchemaNamespace)
434         {
435         }
436
437         internal override string WriteMethodName { get { return "WriteUnsignedLong"; } }
438         internal override string ReadMethodName { get { return "ReadElementContentAsUnsignedLong"; } }
439
440         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
441         {
442             writer.WriteUnsignedLong((ulong)obj);
443         }
444
445         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
446         {
447             return (context == null) ? reader.ReadElementContentAsUnsignedLong()
448                 : HandleReadValue(reader.ReadElementContentAsUnsignedLong(), context);
449         }
450     }
451
452     internal class FloatDataContract : PrimitiveDataContract
453     {
454         internal FloatDataContract()
455             : base(typeof(float), DictionaryGlobals.FloatLocalName, DictionaryGlobals.SchemaNamespace)
456         {
457         }
458
459         internal override string WriteMethodName { get { return "WriteFloat"; } }
460         internal override string ReadMethodName { get { return "ReadElementContentAsFloat"; } }
461
462         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
463         {
464             writer.WriteFloat((float)obj);
465         }
466
467         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
468         {
469             return (context == null) ? reader.ReadElementContentAsFloat()
470                 : HandleReadValue(reader.ReadElementContentAsFloat(), context);
471         }
472     }
473
474     internal class DoubleDataContract : PrimitiveDataContract
475     {
476         internal DoubleDataContract()
477             : base(typeof(double), DictionaryGlobals.DoubleLocalName, DictionaryGlobals.SchemaNamespace)
478         {
479         }
480
481         internal override string WriteMethodName { get { return "WriteDouble"; } }
482         internal override string ReadMethodName { get { return "ReadElementContentAsDouble"; } }
483
484         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
485         {
486             writer.WriteDouble((double)obj);
487         }
488
489         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
490         {
491             return (context == null) ? reader.ReadElementContentAsDouble()
492                 : HandleReadValue(reader.ReadElementContentAsDouble(), context);
493         }
494     }
495
496     internal class DecimalDataContract : PrimitiveDataContract
497     {
498         internal DecimalDataContract()
499             : base(typeof(decimal), DictionaryGlobals.DecimalLocalName, DictionaryGlobals.SchemaNamespace)
500         {
501         }
502
503         internal override string WriteMethodName { get { return "WriteDecimal"; } }
504         internal override string ReadMethodName { get { return "ReadElementContentAsDecimal"; } }
505
506         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
507         {
508             writer.WriteDecimal((decimal)obj);
509         }
510
511         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
512         {
513             return (context == null) ? reader.ReadElementContentAsDecimal()
514                 : HandleReadValue(reader.ReadElementContentAsDecimal(), context);
515         }
516     }
517
518     internal class DateTimeDataContract : PrimitiveDataContract
519     {
520         internal DateTimeDataContract()
521             : base(typeof(DateTime), DictionaryGlobals.DateTimeLocalName, DictionaryGlobals.SchemaNamespace)
522         {
523         }
524
525         internal override string WriteMethodName { get { return "WriteDateTime"; } }
526         internal override string ReadMethodName { get { return "ReadElementContentAsDateTime"; } }
527
528         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
529         {
530             writer.WriteDateTime((DateTime)obj);
531         }
532
533         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
534         {
535             return (context == null) ? reader.ReadElementContentAsDateTime()
536                 : HandleReadValue(reader.ReadElementContentAsDateTime(), context);
537         }
538     }
539
540     internal class StringDataContract : PrimitiveDataContract
541     {
542         internal StringDataContract()
543             : this(DictionaryGlobals.StringLocalName, DictionaryGlobals.SchemaNamespace)
544         {
545         }
546
547         internal StringDataContract(XmlDictionaryString name, XmlDictionaryString ns)
548             : base(typeof(string), name, ns)
549         {
550         }
551
552         internal override string WriteMethodName { get { return "WriteString"; } }
553         internal override string ReadMethodName { get { return "ReadElementContentAsString"; } }
554
555         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
556         {
557             writer.WriteString((string)obj);
558         }
559
560         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
561         {
562             if (context == null)
563             {
564                 return TryReadNullAtTopLevel(reader) ? null : reader.ReadElementContentAsString();
565             }
566             else
567             {
568                 return HandleReadValue(reader.ReadElementContentAsString(), context);
569             }
570         }
571     }
572     internal class TimeDataContract : StringDataContract
573     {
574         internal TimeDataContract() : base(DictionaryGlobals.timeLocalName, DictionaryGlobals.SchemaNamespace) { }
575     }
576     internal class DateDataContract : StringDataContract
577     {
578         internal DateDataContract() : base(DictionaryGlobals.dateLocalName, DictionaryGlobals.SchemaNamespace) { }
579     }
580     internal class HexBinaryDataContract : StringDataContract
581     {
582         internal HexBinaryDataContract() : base(DictionaryGlobals.hexBinaryLocalName, DictionaryGlobals.SchemaNamespace) { }
583     }
584     internal class GYearMonthDataContract : StringDataContract
585     {
586         internal GYearMonthDataContract() : base(DictionaryGlobals.gYearMonthLocalName, DictionaryGlobals.SchemaNamespace) { }
587     }
588     internal class GYearDataContract : StringDataContract
589     {
590         internal GYearDataContract() : base(DictionaryGlobals.gYearLocalName, DictionaryGlobals.SchemaNamespace) { }
591     }
592     internal class GMonthDayDataContract : StringDataContract
593     {
594         internal GMonthDayDataContract() : base(DictionaryGlobals.gMonthDayLocalName, DictionaryGlobals.SchemaNamespace) { }
595     }
596     internal class GDayDataContract : StringDataContract
597     {
598         internal GDayDataContract() : base(DictionaryGlobals.gDayLocalName, DictionaryGlobals.SchemaNamespace) { }
599     }
600     internal class GMonthDataContract : StringDataContract
601     {
602         internal GMonthDataContract() : base(DictionaryGlobals.gMonthLocalName, DictionaryGlobals.SchemaNamespace) { }
603     }
604     internal class NormalizedStringDataContract : StringDataContract
605     {
606         internal NormalizedStringDataContract() : base(DictionaryGlobals.normalizedStringLocalName, DictionaryGlobals.SchemaNamespace) { }
607     }
608     internal class TokenDataContract : StringDataContract
609     {
610         internal TokenDataContract() : base(DictionaryGlobals.tokenLocalName, DictionaryGlobals.SchemaNamespace) { }
611     }
612     internal class LanguageDataContract : StringDataContract
613     {
614         internal LanguageDataContract() : base(DictionaryGlobals.languageLocalName, DictionaryGlobals.SchemaNamespace) { }
615     }
616     internal class NameDataContract : StringDataContract
617     {
618         internal NameDataContract() : base(DictionaryGlobals.NameLocalName, DictionaryGlobals.SchemaNamespace) { }
619     }
620     internal class NCNameDataContract : StringDataContract
621     {
622         internal NCNameDataContract() : base(DictionaryGlobals.NCNameLocalName, DictionaryGlobals.SchemaNamespace) { }
623     }
624     internal class IDDataContract : StringDataContract
625     {
626         internal IDDataContract() : base(DictionaryGlobals.XSDIDLocalName, DictionaryGlobals.SchemaNamespace) { }
627     }
628     internal class IDREFDataContract : StringDataContract
629     {
630         internal IDREFDataContract() : base(DictionaryGlobals.IDREFLocalName, DictionaryGlobals.SchemaNamespace) { }
631     }
632     internal class IDREFSDataContract : StringDataContract
633     {
634         internal IDREFSDataContract() : base(DictionaryGlobals.IDREFSLocalName, DictionaryGlobals.SchemaNamespace) { }
635     }
636     internal class ENTITYDataContract : StringDataContract
637     {
638         internal ENTITYDataContract() : base(DictionaryGlobals.ENTITYLocalName, DictionaryGlobals.SchemaNamespace) { }
639     }
640     internal class ENTITIESDataContract : StringDataContract
641     {
642         internal ENTITIESDataContract() : base(DictionaryGlobals.ENTITIESLocalName, DictionaryGlobals.SchemaNamespace) { }
643     }
644     internal class NMTOKENDataContract : StringDataContract
645     {
646         internal NMTOKENDataContract() : base(DictionaryGlobals.NMTOKENLocalName, DictionaryGlobals.SchemaNamespace) { }
647     }
648     internal class NMTOKENSDataContract : StringDataContract
649     {
650         internal NMTOKENSDataContract() : base(DictionaryGlobals.NMTOKENSLocalName, DictionaryGlobals.SchemaNamespace) { }
651     }
652
653     internal class ByteArrayDataContract : PrimitiveDataContract
654     {
655         internal ByteArrayDataContract()
656             : base(typeof(byte[]), DictionaryGlobals.ByteArrayLocalName, DictionaryGlobals.SchemaNamespace)
657         {
658         }
659
660         internal override string WriteMethodName { get { return "WriteBase64"; } }
661         internal override string ReadMethodName { get { return "ReadElementContentAsBase64"; } }
662
663         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
664         {
665             writer.WriteBase64((byte[])obj);
666         }
667
668         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
669         {
670             if (context == null)
671             {
672                 return TryReadNullAtTopLevel(reader) ? null : reader.ReadElementContentAsBase64();
673             }
674             else
675             {
676                 return HandleReadValue(reader.ReadElementContentAsBase64(), context);
677             }
678         }
679     }
680
681     internal class ObjectDataContract : PrimitiveDataContract
682     {
683         internal ObjectDataContract()
684             : base(typeof(object), DictionaryGlobals.ObjectLocalName, DictionaryGlobals.SchemaNamespace)
685         {
686         }
687
688         internal override string WriteMethodName { get { return "WriteAnyType"; } }
689         internal override string ReadMethodName { get { return "ReadElementContentAsAnyType"; } }
690
691         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
692         {
693             // write nothing
694         }
695
696         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
697         {
698             object obj;
699             if (reader.IsEmptyElement)
700             {
701                 reader.Skip();
702                 obj = new object();
703             }
704             else
705             {
706                 string localName = reader.LocalName;
707                 string ns = reader.NamespaceURI;
708                 reader.Read();
709                 try
710                 {
711                     reader.ReadEndElement();
712                     obj = new object();
713                 }
714                 catch (XmlException xes)
715                 {
716                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.XmlForObjectCannotHaveContent, localName, ns), xes));
717                 }
718             }
719             return (context == null) ? obj : HandleReadValue(obj, context);
720         }
721
722         internal override bool CanContainReferences
723         {
724             get { return true; }
725         }
726
727         internal override bool IsPrimitive
728         {
729             get { return false; }
730         }
731
732     }
733
734     internal class TimeSpanDataContract : PrimitiveDataContract
735     {
736         internal TimeSpanDataContract()
737             : this(DictionaryGlobals.TimeSpanLocalName, DictionaryGlobals.SerializationNamespace)
738         {
739         }
740
741         internal TimeSpanDataContract(XmlDictionaryString name, XmlDictionaryString ns)
742             : base(typeof(TimeSpan), name, ns)
743         {
744         }
745
746         internal override string WriteMethodName { get { return "WriteTimeSpan"; } }
747         internal override string ReadMethodName { get { return "ReadElementContentAsTimeSpan"; } }
748
749         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
750         {
751             writer.WriteTimeSpan((TimeSpan)obj);
752         }
753
754         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
755         {
756             return (context == null) ? reader.ReadElementContentAsTimeSpan()
757                 : HandleReadValue(reader.ReadElementContentAsTimeSpan(), context);
758         }
759     }
760
761     internal class XsDurationDataContract : TimeSpanDataContract
762     {
763         internal XsDurationDataContract() : base(DictionaryGlobals.TimeSpanLocalName, DictionaryGlobals.SchemaNamespace) { }
764     }
765
766     internal class GuidDataContract : PrimitiveDataContract
767     {
768         internal GuidDataContract()
769             : this(DictionaryGlobals.GuidLocalName, DictionaryGlobals.SerializationNamespace)
770         {
771         }
772
773         internal GuidDataContract(XmlDictionaryString name, XmlDictionaryString ns)
774             : base(typeof(Guid), name, ns)
775         {
776         }
777
778         internal override string WriteMethodName { get { return "WriteGuid"; } }
779         internal override string ReadMethodName { get { return "ReadElementContentAsGuid"; } }
780
781         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
782         {
783             writer.WriteGuid((Guid)obj);
784         }
785
786         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
787         {
788             return (context == null) ? reader.ReadElementContentAsGuid()
789                 : HandleReadValue(reader.ReadElementContentAsGuid(), context);
790         }
791     }
792
793     internal class AsmxGuidDataContract : GuidDataContract
794     {
795         internal AsmxGuidDataContract() : base(DictionaryGlobals.GuidLocalName, DictionaryGlobals.AsmxTypesNamespace) { }
796     }
797
798     internal class UriDataContract : PrimitiveDataContract
799     {
800         internal UriDataContract()
801             : base(typeof(Uri), DictionaryGlobals.UriLocalName, DictionaryGlobals.SchemaNamespace)
802         {
803         }
804
805         internal override string WriteMethodName { get { return "WriteUri"; } }
806         internal override string ReadMethodName { get { return "ReadElementContentAsUri"; } }
807
808         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
809         {
810             writer.WriteUri((Uri)obj);
811         }
812
813         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
814         {
815             if (context == null)
816             {
817                 return TryReadNullAtTopLevel(reader) ? null : reader.ReadElementContentAsUri();
818             }
819             else
820             {
821                 return HandleReadValue(reader.ReadElementContentAsUri(), context);
822             }
823         }
824     }
825
826     internal class QNameDataContract : PrimitiveDataContract
827     {
828         internal QNameDataContract()
829             : base(typeof(XmlQualifiedName), DictionaryGlobals.QNameLocalName, DictionaryGlobals.SchemaNamespace)
830         {
831         }
832
833         internal override string WriteMethodName { get { return "WriteQName"; } }
834         internal override string ReadMethodName { get { return "ReadElementContentAsQName"; } }
835
836         internal override bool IsPrimitive
837         {
838             get { return false; }
839         }
840
841         public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context)
842         {
843             writer.WriteQName((XmlQualifiedName)obj);
844         }
845
846         public override object ReadXmlValue(XmlReaderDelegator reader, XmlObjectSerializerReadContext context)
847         {
848             if (context == null)
849             {
850                 return TryReadNullAtTopLevel(reader) ? null : reader.ReadElementContentAsQName();
851             }
852             else
853             {
854                 return HandleReadValue(reader.ReadElementContentAsQName(), context);
855             }
856         }
857
858         internal override void WriteRootElement(XmlWriterDelegator writer, XmlDictionaryString name, XmlDictionaryString ns)
859         {
860             if (object.ReferenceEquals(ns, DictionaryGlobals.SerializationNamespace))
861                 writer.WriteStartElement(Globals.SerPrefix, name, ns);
862             else if (ns != null && ns.Value != null && ns.Value.Length > 0)
863                 writer.WriteStartElement(Globals.ElementPrefix, name, ns);
864             else
865                 writer.WriteStartElement(name, ns);
866         }
867     }
868 }