PageRenderTime 47ms CodeModel.GetById 27ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/WCFWebApi/src/Microsoft.Runtime.Serialization.Internal/Microsoft/Runtime/Serialization/PrimitiveDataContract.cs

#
C# | 518 lines | 453 code | 62 blank | 3 comment | 4 complexity | d4a17a25076a8610bbc94049b3e82130 MD5 | raw file
  1//-----------------------------------------------------------------------------
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3//-----------------------------------------------------------------------------
  4namespace Microsoft.Runtime.Serialization
  5{
  6    using System;
  7    using System.Collections.Generic;
  8    using System.Diagnostics.CodeAnalysis;
  9    using System.Security;
 10    using System.Xml;
 11    using Microsoft.Server.Common;
 12
 13#if USE_REFEMIT
 14    public abstract class PrimitiveDataContract : DataContract
 15#else
 16    internal abstract class PrimitiveDataContract : DataContract
 17#endif
 18    {
 19        [Fx.Tag.SecurityNote(Critical = "Holds instance of CriticalHelper which keeps state that is cached statically for serialization."
 20            + " Static fields are marked SecurityCritical or readonly to prevent data from being modified or leaked to other components in appdomain.")]
 21        [SecurityCritical]
 22        [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "This is cloned code.")]
 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 override XmlDictionaryString TopLevelElementNamespace
 45        {
 46            get { return DictionaryGlobals.SerializationNamespace; }
 47            set { }
 48        }
 49
 50        internal override bool CanContainReferences
 51        {
 52            get { return false; }
 53        }
 54
 55        internal override bool IsPrimitive
 56        {
 57            get { return true; }
 58        }
 59
 60        internal override bool IsBuiltInDataContract
 61        {
 62            get
 63            {
 64                return true;
 65            }
 66        }
 67
 68        internal override bool Equals(object other, Dictionary<DataContractPairKey, object> checkedContracts)
 69        {
 70            PrimitiveDataContract dataContract = other as PrimitiveDataContract;
 71            if (dataContract != null)
 72            {
 73                Type thisType = this.GetType();
 74                Type otherType = other.GetType();
 75                return (thisType.Equals(otherType) || thisType.IsSubclassOf(otherType) || otherType.IsSubclassOf(thisType));
 76            }
 77            return false;
 78        }
 79
 80        [Fx.Tag.SecurityNote(Critical = "Holds all state used for for (de)serializing primitives."
 81            + " Since the data is cached statically, we lock down access to it.")]
 82        [SecurityCritical]
 83        class PrimitiveDataContractCriticalHelper : DataContract.DataContractCriticalHelper
 84        {
 85            internal PrimitiveDataContractCriticalHelper(Type type, XmlDictionaryString name, XmlDictionaryString ns)
 86                : base(type)
 87            {
 88                SetDataContractName(name, ns);
 89            }
 90        }
 91    }
 92
 93    internal class CharDataContract : PrimitiveDataContract
 94    {
 95        internal CharDataContract()
 96            : this(DictionaryGlobals.CharLocalName, DictionaryGlobals.SerializationNamespace)
 97        {
 98        }
 99
100        internal CharDataContract(XmlDictionaryString name, XmlDictionaryString ns)
101            : base(typeof(char), name, ns)
102        {
103        }
104    }
105
106    internal class AsmxCharDataContract : CharDataContract
107    {
108        internal AsmxCharDataContract() 
109            : base(DictionaryGlobals.CharLocalName, DictionaryGlobals.AsmxTypesNamespace) 
110        {
111        }
112    }
113
114    internal class BooleanDataContract : PrimitiveDataContract
115    {
116        internal BooleanDataContract()
117            : base(typeof(bool), DictionaryGlobals.BooleanLocalName, DictionaryGlobals.SchemaNamespace)
118        {
119        }
120    }
121
122    internal class SignedByteDataContract : PrimitiveDataContract
123    {
124        internal SignedByteDataContract()
125            : base(typeof(sbyte), DictionaryGlobals.SignedByteLocalName, DictionaryGlobals.SchemaNamespace)
126        {
127        }
128    }
129
130    internal class UnsignedByteDataContract : PrimitiveDataContract
131    {
132        internal UnsignedByteDataContract()
133            : base(typeof(byte), DictionaryGlobals.UnsignedByteLocalName, DictionaryGlobals.SchemaNamespace)
134        {
135        }
136    }
137
138    internal class ShortDataContract : PrimitiveDataContract
139    {
140        internal ShortDataContract()
141            : base(typeof(short), DictionaryGlobals.ShortLocalName, DictionaryGlobals.SchemaNamespace)
142        {
143        }
144    }
145
146    internal class UnsignedShortDataContract : PrimitiveDataContract
147    {
148        internal UnsignedShortDataContract()
149            : base(typeof(ushort), DictionaryGlobals.UnsignedShortLocalName, DictionaryGlobals.SchemaNamespace)
150        {
151        }
152    }
153
154    internal class IntDataContract : PrimitiveDataContract
155    {
156        internal IntDataContract()
157            : base(typeof(int), DictionaryGlobals.IntLocalName, DictionaryGlobals.SchemaNamespace)
158        {
159        }
160    }
161
162    internal class UnsignedIntDataContract : PrimitiveDataContract
163    {
164        internal UnsignedIntDataContract()
165            : base(typeof(uint), DictionaryGlobals.UnsignedIntLocalName, DictionaryGlobals.SchemaNamespace)
166        {
167        }
168    }
169
170    internal class LongDataContract : PrimitiveDataContract
171    {
172        internal LongDataContract()
173            : this(DictionaryGlobals.LongLocalName, DictionaryGlobals.SchemaNamespace)
174        {
175        }
176
177        internal LongDataContract(XmlDictionaryString name, XmlDictionaryString ns)
178            : base(typeof(long), name, ns)
179        {
180        }
181    }
182    internal class IntegerDataContract : LongDataContract
183    {
184        internal IntegerDataContract() 
185            : base(DictionaryGlobals.integerLocalName, DictionaryGlobals.SchemaNamespace)
186        { 
187        }
188    }
189    internal class PositiveIntegerDataContract : LongDataContract
190    {
191        internal PositiveIntegerDataContract() 
192            : base(DictionaryGlobals.positiveIntegerLocalName, DictionaryGlobals.SchemaNamespace)
193        {
194        }
195    }
196    internal class NegativeIntegerDataContract : LongDataContract
197    {
198        internal NegativeIntegerDataContract() 
199            : base(DictionaryGlobals.negativeIntegerLocalName, DictionaryGlobals.SchemaNamespace) 
200        {
201        }
202    }
203    internal class NonPositiveIntegerDataContract : LongDataContract
204    {
205        internal NonPositiveIntegerDataContract() 
206            : base(DictionaryGlobals.nonPositiveIntegerLocalName, DictionaryGlobals.SchemaNamespace)
207        {
208        }
209    }
210    internal class NonNegativeIntegerDataContract : LongDataContract
211    {
212        internal NonNegativeIntegerDataContract() 
213            : base(DictionaryGlobals.nonNegativeIntegerLocalName, DictionaryGlobals.SchemaNamespace) 
214        {
215        }
216    }
217
218    internal class UnsignedLongDataContract : PrimitiveDataContract
219    {
220        internal UnsignedLongDataContract()
221            : base(typeof(ulong), DictionaryGlobals.UnsignedLongLocalName, DictionaryGlobals.SchemaNamespace)
222        {
223        }
224    }
225
226    internal class FloatDataContract : PrimitiveDataContract
227    {
228        internal FloatDataContract()
229            : base(typeof(float), DictionaryGlobals.FloatLocalName, DictionaryGlobals.SchemaNamespace)
230        {
231        }
232    }
233
234    internal class DoubleDataContract : PrimitiveDataContract
235    {
236        internal DoubleDataContract()
237            : base(typeof(double), DictionaryGlobals.DoubleLocalName, DictionaryGlobals.SchemaNamespace)
238        {
239        }
240    }
241
242    internal class DecimalDataContract : PrimitiveDataContract
243    {
244        internal DecimalDataContract()
245            : base(typeof(decimal), DictionaryGlobals.DecimalLocalName, DictionaryGlobals.SchemaNamespace)
246        {
247        }
248    }
249
250    internal class DateTimeDataContract : PrimitiveDataContract
251    {
252        internal DateTimeDataContract()
253            : base(typeof(DateTime), DictionaryGlobals.DateTimeLocalName, DictionaryGlobals.SchemaNamespace)
254        {
255        }
256    }
257
258    internal class StringDataContract : PrimitiveDataContract
259    {
260        internal StringDataContract()
261            : this(DictionaryGlobals.StringLocalName, DictionaryGlobals.SchemaNamespace)
262        {
263        }
264
265        internal StringDataContract(XmlDictionaryString name, XmlDictionaryString ns)
266            : base(typeof(string), name, ns)
267        {
268        }
269    }
270    internal class TimeDataContract : StringDataContract
271    {
272        internal TimeDataContract() 
273            : base(DictionaryGlobals.timeLocalName, DictionaryGlobals.SchemaNamespace)
274        {
275        }
276    }
277
278    internal class DateDataContract : StringDataContract
279    {
280        internal DateDataContract() 
281            : base(DictionaryGlobals.dateLocalName, DictionaryGlobals.SchemaNamespace) 
282        { 
283        }
284    }
285
286    internal class HexBinaryDataContract : StringDataContract
287    {
288        internal HexBinaryDataContract()
289            : base(DictionaryGlobals.hexBinaryLocalName, DictionaryGlobals.SchemaNamespace) 
290        {
291        }
292    }
293
294    internal class GYearMonthDataContract : StringDataContract
295    {
296        internal GYearMonthDataContract() 
297            : base(DictionaryGlobals.gYearMonthLocalName, DictionaryGlobals.SchemaNamespace) 
298        {
299        }
300    }
301
302    internal class GYearDataContract : StringDataContract
303    {
304        internal GYearDataContract() 
305            : base(DictionaryGlobals.gYearLocalName, DictionaryGlobals.SchemaNamespace)
306        {
307        }
308    }
309
310    internal class GMonthDayDataContract : StringDataContract
311    {
312        internal GMonthDayDataContract() 
313            : base(DictionaryGlobals.gMonthDayLocalName, DictionaryGlobals.SchemaNamespace) 
314        { 
315        }
316    }
317
318    internal class GDayDataContract : StringDataContract
319    {
320        internal GDayDataContract() 
321            : base(DictionaryGlobals.gDayLocalName, DictionaryGlobals.SchemaNamespace)
322        {
323        }
324    }
325
326    internal class GMonthDataContract : StringDataContract
327    {
328        internal GMonthDataContract() 
329            : base(DictionaryGlobals.gMonthLocalName, DictionaryGlobals.SchemaNamespace)
330        {
331        }
332    }
333
334    internal class NormalizedStringDataContract : StringDataContract
335    {
336        internal NormalizedStringDataContract() 
337            : base(DictionaryGlobals.normalizedStringLocalName, DictionaryGlobals.SchemaNamespace) 
338        {
339        }
340    }
341
342    internal class TokenDataContract : StringDataContract
343    {
344        internal TokenDataContract() 
345            : base(DictionaryGlobals.tokenLocalName, DictionaryGlobals.SchemaNamespace) 
346        {
347        }
348    }
349
350    internal class LanguageDataContract : StringDataContract
351    {
352        internal LanguageDataContract() 
353            : base(DictionaryGlobals.languageLocalName, DictionaryGlobals.SchemaNamespace) 
354        {
355        }
356    }
357
358    internal class NameDataContract : StringDataContract
359    {
360        internal NameDataContract() 
361            : base(DictionaryGlobals.NameLocalName, DictionaryGlobals.SchemaNamespace)
362        { 
363        }
364    }
365
366    internal class NCNameDataContract : StringDataContract
367    {
368        internal NCNameDataContract() : base(DictionaryGlobals.NCNameLocalName, DictionaryGlobals.SchemaNamespace)
369        {
370        }
371    }
372
373    internal class IDDataContract : StringDataContract
374    {
375        internal IDDataContract()
376            : base(DictionaryGlobals.XSDIDLocalName, DictionaryGlobals.SchemaNamespace)
377        {
378        }
379    }
380
381    internal class IDREFDataContract : StringDataContract
382    {
383        internal IDREFDataContract() 
384            : base(DictionaryGlobals.IDREFLocalName, DictionaryGlobals.SchemaNamespace)
385        {
386        }
387    }
388
389    internal class IDREFSDataContract : StringDataContract
390    {
391        internal IDREFSDataContract() 
392            : base(DictionaryGlobals.IDREFSLocalName, DictionaryGlobals.SchemaNamespace)
393        {
394        }
395    }
396
397    internal class ENTITYDataContract : StringDataContract
398    {
399        internal ENTITYDataContract() 
400            : base(DictionaryGlobals.ENTITYLocalName, DictionaryGlobals.SchemaNamespace) 
401        { 
402        }
403    }
404
405    internal class ENTITIESDataContract : StringDataContract
406    {
407        internal ENTITIESDataContract() 
408            : base(DictionaryGlobals.ENTITIESLocalName, DictionaryGlobals.SchemaNamespace) 
409        {
410        }
411    }
412
413    internal class NMTOKENDataContract : StringDataContract
414    {
415        internal NMTOKENDataContract() 
416            : base(DictionaryGlobals.NMTOKENLocalName, DictionaryGlobals.SchemaNamespace) 
417        {
418        }
419    }
420
421    internal class NMTOKENSDataContract : StringDataContract
422    {
423        internal NMTOKENSDataContract() 
424            : base(DictionaryGlobals.NMTOKENSLocalName, DictionaryGlobals.SchemaNamespace) 
425        {
426        }
427    }
428
429    internal class ByteArrayDataContract : PrimitiveDataContract
430    {
431        internal ByteArrayDataContract()
432            : base(typeof(byte[]), DictionaryGlobals.ByteArrayLocalName, DictionaryGlobals.SchemaNamespace)
433        {
434        }
435    }
436
437    internal class ObjectDataContract : PrimitiveDataContract
438    {
439        internal ObjectDataContract()
440            : base(typeof(object), DictionaryGlobals.ObjectLocalName, DictionaryGlobals.SchemaNamespace)
441        {
442        }
443
444        internal override bool CanContainReferences
445        {
446            get { return true; }
447        }
448
449        internal override bool IsPrimitive
450        {
451            get { return false; }
452        }
453
454    }
455
456    internal class TimeSpanDataContract : PrimitiveDataContract
457    {
458        internal TimeSpanDataContract()
459            : this(DictionaryGlobals.TimeSpanLocalName, DictionaryGlobals.SerializationNamespace)
460        {
461        }
462
463        internal TimeSpanDataContract(XmlDictionaryString name, XmlDictionaryString ns)
464            : base(typeof(TimeSpan), name, ns)
465        {
466        }
467    }
468
469    internal class XsDurationDataContract : TimeSpanDataContract
470    {
471        internal XsDurationDataContract() 
472            : base(DictionaryGlobals.TimeSpanLocalName, DictionaryGlobals.SchemaNamespace) 
473        {
474        }
475    }
476
477    internal class GuidDataContract : PrimitiveDataContract
478    {
479        internal GuidDataContract()
480            : this(DictionaryGlobals.GuidLocalName, DictionaryGlobals.SerializationNamespace)
481        {
482        }
483
484        internal GuidDataContract(XmlDictionaryString name, XmlDictionaryString ns)
485            : base(typeof(Guid), name, ns)
486        {
487        }
488    }
489
490    internal class AsmxGuidDataContract : GuidDataContract
491    {
492        internal AsmxGuidDataContract() 
493            : base(DictionaryGlobals.GuidLocalName, DictionaryGlobals.AsmxTypesNamespace) 
494        {
495        }
496    }
497
498    internal class UriDataContract : PrimitiveDataContract
499    {
500        internal UriDataContract() 
501            : base(typeof(Uri), DictionaryGlobals.UriLocalName, DictionaryGlobals.SchemaNamespace)
502        {
503        }
504    }
505
506    internal class QNameDataContract : PrimitiveDataContract
507    {
508        internal QNameDataContract()
509            : base(typeof(XmlQualifiedName), DictionaryGlobals.QNameLocalName, DictionaryGlobals.SchemaNamespace)
510        {
511        }
512
513        internal override bool IsPrimitive
514        {
515            get { return false; }
516        }
517    }
518}