Handle ENETDOWN error if defined.
[mono.git] / mcs / class / System.Runtime.Serialization / System.Runtime.Serialization / KnownTypeCollection.cs
1 //
2 // KnownTypeCollection.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if NET_2_0
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
33 using System.Linq;
34 using System.Reflection;
35 using System.Xml;
36 using System.Xml.Schema;
37
38 using QName = System.Xml.XmlQualifiedName;
39 using System.Xml.Serialization;
40
41 namespace System.Runtime.Serialization
42 {
43 /*
44         XmlFormatter implementation design inference:
45
46         type definitions:
47         - No XML Schema types are directly used. There are some maps from
48           xs:blahType to ms:blahType where the namespaceURI for prefix "ms" is
49           "http://schemas.microsoft.com/2003/10/Serialization/" .
50
51         serializable types:
52         - An object being serialized 1) must be of type System.Object, or
53           2) must be null, or 3) must have either a [DataContract] attribute
54           or a [Serializable] attribute to be serializable.
55         - When the object is either of type System.Object or null, then the
56           XML type is "anyType".
57         - When the object is [Serializable], then the runtime-serialization
58           compatible object graph is written.
59         - Otherwise the serialization is based on contract attributes.
60           ([Serializable] takes precedence).
61
62         type derivation:
63         - For type A to be serializable, the base type B of A must be
64           serializable.
65         - If a type which is [Serializable] and whose base type has a
66           [DataContract], then for base type members [DataContract] is taken.
67         - It is vice versa i.e. if the base type is [Serializable] and the
68           derived type has a [DataContract], then [Serializable] takes place
69           for base members.
70
71         known type collection:
72         - It internally manages mapping store keyed by contract QNames.
73           KnownTypeCollection.Add() checks if the same QName contract already
74           exists (and raises InvalidOperationException if required).
75
76 */
77
78         internal static class TypeExtensions
79         {
80 #if !NET_4_5
81                 public static T GetCustomAttribute<T> (this MemberInfo type, bool inherit)
82                 {
83                         var arr = type.GetCustomAttributes (typeof (T), inherit);
84                         return arr != null && arr.Length == 1 ? (T) arr [0] : default (T);
85                 }
86 #endif
87                 public static IEnumerable<Type> GetInterfacesOrSelfInterface (this Type type)
88                 {
89                         if (type.IsInterface)
90                                 yield return type;
91                         foreach (var t in type.GetInterfaces ())
92                                 yield return t;
93                 }
94
95                 public static bool ImplementsInterface (this Type type, Type iface)
96                 {
97                         foreach (var t in type.GetInterfacesOrSelfInterface ()) {
98                                 if (t == iface)
99                                         return true;
100                         }
101
102                         var baseType = type.BaseType;
103                         if (baseType != null)
104                                 return baseType.ImplementsInterface (iface);
105                         
106                         return false;
107                 }
108         }
109
110         internal sealed class KnownTypeCollection : Collection<Type>
111         {
112                 internal const string MSSimpleNamespace =
113                         "http://schemas.microsoft.com/2003/10/Serialization/";
114                 internal const string MSArraysNamespace =
115                         "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
116                 internal const string DefaultClrNamespaceBase =
117                         "http://schemas.datacontract.org/2004/07/";
118                 internal const string DefaultClrNamespaceSystem =
119                         "http://schemas.datacontract.org/2004/07/System";
120
121
122                 static QName any_type, bool_type,
123                         byte_type, date_type, decimal_type, double_type,
124                         float_type, string_type,
125                         short_type, int_type, long_type,
126                         ubyte_type, ushort_type, uint_type, ulong_type,
127                         // non-TypeCode
128                         any_uri_type, base64_type, duration_type, qname_type,
129                         // custom in ms nsURI schema
130                         char_type, guid_type,
131                         // not in ms nsURI schema
132                         dbnull_type, date_time_offset_type;
133
134                 // XmlSchemaType.GetBuiltInPrimitiveType() does not exist in moonlight, so I had to explicitly add them. And now that we have it, it does not make much sense to use #if MOONLIGHT ... #endif for XmlSchemaType anymore :-(
135                 static Dictionary<string,Type> xs_predefined_types = new Dictionary<string,Type> ();
136
137                 static KnownTypeCollection ()
138                 {
139                         string s = MSSimpleNamespace;
140                         any_type = new QName ("anyType", s);
141                         any_uri_type = new QName ("anyURI", s);
142                         bool_type = new QName ("boolean", s);
143                         base64_type = new QName ("base64Binary", s);
144                         date_type = new QName ("dateTime", s);
145                         duration_type = new QName ("duration", s);
146                         qname_type = new QName ("QName", s);
147                         decimal_type = new QName ("decimal", s);
148                         double_type = new QName ("double", s);
149                         float_type = new QName ("float", s);
150                         byte_type = new QName ("byte", s);
151                         short_type = new QName ("short", s);
152                         int_type = new QName ("int", s);
153                         long_type = new QName ("long", s);
154                         ubyte_type = new QName ("unsignedByte", s);
155                         ushort_type = new QName ("unsignedShort", s);
156                         uint_type = new QName ("unsignedInt", s);
157                         ulong_type = new QName ("unsignedLong", s);
158                         string_type = new QName ("string", s);
159                         guid_type = new QName ("guid", s);
160                         char_type = new QName ("char", s);
161
162                         dbnull_type = new QName ("DBNull", DefaultClrNamespaceBase + "System");
163                         date_time_offset_type = new QName ("DateTimeOffset", DefaultClrNamespaceBase + "System");
164
165                         xs_predefined_types.Add ("string", typeof (string));
166                         xs_predefined_types.Add ("boolean", typeof (bool));
167                         xs_predefined_types.Add ("float", typeof (float));
168                         xs_predefined_types.Add ("double", typeof (double));
169                         xs_predefined_types.Add ("decimal", typeof (decimal));
170                         xs_predefined_types.Add ("duration", typeof (TimeSpan));
171                         xs_predefined_types.Add ("dateTime", typeof (DateTime));
172                         xs_predefined_types.Add ("date", typeof (DateTime));
173                         xs_predefined_types.Add ("time", typeof (DateTime));
174                         xs_predefined_types.Add ("gYearMonth", typeof (DateTime));
175                         xs_predefined_types.Add ("gYear", typeof (DateTime));
176                         xs_predefined_types.Add ("gMonthDay", typeof (DateTime));
177                         xs_predefined_types.Add ("gDay", typeof (DateTime));
178                         xs_predefined_types.Add ("gMonth", typeof (DateTime));
179                         xs_predefined_types.Add ("hexBinary", typeof (byte []));
180                         xs_predefined_types.Add ("base64Binary", typeof (byte []));
181                         xs_predefined_types.Add ("anyURI", typeof (Uri));
182                         xs_predefined_types.Add ("QName", typeof (QName));
183                         xs_predefined_types.Add ("NOTATION", typeof (string));
184
185                         xs_predefined_types.Add ("normalizedString", typeof (string));
186                         xs_predefined_types.Add ("token", typeof (string));
187                         xs_predefined_types.Add ("language", typeof (string));
188                         xs_predefined_types.Add ("IDREFS", typeof (string []));
189                         xs_predefined_types.Add ("ENTITIES", typeof (string []));
190                         xs_predefined_types.Add ("NMTOKEN", typeof (string));
191                         xs_predefined_types.Add ("NMTOKENS", typeof (string []));
192                         xs_predefined_types.Add ("Name", typeof (string));
193                         xs_predefined_types.Add ("NCName", typeof (string));
194                         xs_predefined_types.Add ("ID", typeof (string));
195                         xs_predefined_types.Add ("IDREF", typeof (string));
196                         xs_predefined_types.Add ("ENTITY", typeof (string));
197
198                         xs_predefined_types.Add ("integer", typeof (decimal));
199                         xs_predefined_types.Add ("nonPositiveInteger", typeof (int));
200                         xs_predefined_types.Add ("negativeInteger", typeof (int));
201                         xs_predefined_types.Add ("long", typeof (long));
202                         xs_predefined_types.Add ("int", typeof (int));
203                         xs_predefined_types.Add ("short", typeof (short));
204                         xs_predefined_types.Add ("byte", typeof (sbyte));
205                         xs_predefined_types.Add ("nonNegativeInteger", typeof (decimal));
206                         xs_predefined_types.Add ("unsignedLong", typeof (ulong));
207                         xs_predefined_types.Add ("unsignedInt", typeof (uint));
208                         xs_predefined_types.Add ("unsignedShort", typeof (ushort));
209                         xs_predefined_types.Add ("unsignedByte", typeof (byte));
210                         xs_predefined_types.Add ("positiveInteger", typeof (decimal));
211
212                         xs_predefined_types.Add ("anyType", typeof (object));
213                 }
214
215                 // FIXME: find out how QName and guid are processed
216
217                 internal QName GetXmlName (Type type)
218                 {
219                         SerializationMap map = FindUserMap (type);
220                         if (map != null)
221                                 return map.XmlName;
222                         return GetPredefinedTypeName (type);
223                 }
224
225                 internal static QName GetPredefinedTypeName (Type type)
226                 {
227                         QName name = GetPrimitiveTypeName (type);
228                         if (name != QName.Empty)
229                                 return name;
230                         if (type == typeof (DBNull))
231                                 return dbnull_type;
232                         return QName.Empty;
233                 }
234
235                 internal static QName GetPrimitiveTypeName (Type type)
236                 {
237                         if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>))
238                                 return GetPrimitiveTypeName (type.GetGenericArguments () [0]);
239
240                         if (type.IsEnum)
241                                 return QName.Empty;
242
243                         switch (Type.GetTypeCode (type)) {
244                         case TypeCode.Object: // other than System.Object
245                         case TypeCode.DBNull: // it is natively mapped, but not in ms serialization namespace.
246                         case TypeCode.Empty:
247                         default:
248                                 if (type == typeof (object))
249                                         return any_type;
250                                 if (type == typeof (Guid))
251                                         return guid_type;
252                                 if (type == typeof (TimeSpan))
253                                         return duration_type;
254                                 if (type == typeof (byte []))
255                                         return base64_type;
256                                 if (type == typeof (Uri))
257                                         return any_uri_type;
258                                 if (type == typeof (DateTimeOffset))
259                                         return date_time_offset_type;
260                                 return QName.Empty;
261                         case TypeCode.Boolean:
262                                 return bool_type;
263                         case TypeCode.Byte:
264                                 return ubyte_type;
265                         case TypeCode.Char:
266                                 return char_type;
267                         case TypeCode.DateTime:
268                                 return date_type;
269                         case TypeCode.Decimal:
270                                 return decimal_type;
271                         case TypeCode.Double:
272                                 return double_type;
273                         case TypeCode.Int16:
274                                 return short_type;
275                         case TypeCode.Int32:
276                                 return int_type;
277                         case TypeCode.Int64:
278                                 return long_type;
279                         case TypeCode.SByte:
280                                 return byte_type;
281                         case TypeCode.Single:
282                                 return float_type;
283                         case TypeCode.String:
284                                 return string_type;
285                         case TypeCode.UInt16:
286                                 return ushort_type;
287                         case TypeCode.UInt32:
288                                 return uint_type;
289                         case TypeCode.UInt64:
290                                 return ulong_type;
291                         }
292                 }
293
294                 internal static string PredefinedTypeObjectToString (object obj)
295                 {
296                         Type type = obj.GetType ();
297                         switch (Type.GetTypeCode (type)) {
298                         case TypeCode.Object: // other than System.Object
299                         case TypeCode.Empty:
300                         default:
301                                 if (type == typeof (object))
302                                         return String.Empty;
303                                 if (type == typeof (Guid))
304                                         return XmlConvert.ToString ((Guid) obj);
305                                 if (type == typeof (TimeSpan))
306                                         return XmlConvert.ToString ((TimeSpan) obj);
307                                 if (type == typeof (byte []))
308                                         return Convert.ToBase64String ((byte []) obj);
309                                 if (type == typeof (Uri))
310                                         return ((Uri) obj).ToString ();
311                                 throw new Exception ("Internal error: missing predefined type serialization for type " + type.FullName);
312                         case TypeCode.DBNull: // predefined, but not primitive
313                                 return String.Empty;
314                         case TypeCode.Boolean:
315                                 return XmlConvert.ToString ((bool) obj);
316                         case TypeCode.Byte:
317                                 return XmlConvert.ToString ((int)((byte) obj));
318                         case TypeCode.Char:
319                                 return XmlConvert.ToString ((uint) (char) obj);
320                         case TypeCode.DateTime:
321                                 return XmlConvert.ToString ((DateTime) obj, XmlDateTimeSerializationMode.RoundtripKind);
322                         case TypeCode.Decimal:
323                                 return XmlConvert.ToString ((decimal) obj);
324                         case TypeCode.Double:
325                                 return XmlConvert.ToString ((double) obj);
326                         case TypeCode.Int16:
327                                 return XmlConvert.ToString ((short) obj);
328                         case TypeCode.Int32:
329                                 return XmlConvert.ToString ((int) obj);
330                         case TypeCode.Int64:
331                                 return XmlConvert.ToString ((long) obj);
332                         case TypeCode.SByte:
333                                 return XmlConvert.ToString ((sbyte) obj);
334                         case TypeCode.Single:
335                                 return XmlConvert.ToString ((float) obj);
336                         case TypeCode.String:
337                                 return (string) obj;
338                         case TypeCode.UInt16:
339                                 return XmlConvert.ToString ((int) (ushort) obj);
340                         case TypeCode.UInt32:
341                                 return XmlConvert.ToString ((uint) obj);
342                         case TypeCode.UInt64:
343                                 return XmlConvert.ToString ((ulong) obj);
344                         }
345                 }
346
347                 internal static Type GetPrimitiveTypeFromName (QName name)
348                 {
349                         switch (name.Namespace) {
350                         case DefaultClrNamespaceSystem:
351                                 switch (name.Name) {
352                                 case "DBNull":
353                                         return typeof (DBNull);
354                                 case "DateTimeOffset":
355                                         return typeof (DateTimeOffset);
356                                 }
357                                 break;
358                         case XmlSchema.Namespace:
359                                 return xs_predefined_types.FirstOrDefault (p => p.Key == name.Name).Value;
360                         case MSSimpleNamespace:
361                                 switch (name.Name) {
362                                 case "anyURI":
363                                         return typeof (Uri);
364                                 case "boolean":
365                                         return typeof (bool);
366                                 case "base64Binary":
367                                         return typeof (byte []);
368                                 case "dateTime":
369                                         return typeof (DateTime);
370                                 case "duration":
371                                         return typeof (TimeSpan);
372                                 case "QName":
373                                         return typeof (QName);
374                                 case "decimal":
375                                         return typeof (decimal);
376                                 case "double":
377                                         return typeof (double);
378                                 case "float":
379                                         return typeof (float);
380                                 case "byte":
381                                         return typeof (sbyte);
382                                 case "short":
383                                         return typeof (short);
384                                 case "int":
385                                         return typeof (int);
386                                 case "long":
387                                         return typeof (long);
388                                 case "unsignedByte":
389                                         return typeof (byte);
390                                 case "unsignedShort":
391                                         return typeof (ushort);
392                                 case "unsignedInt":
393                                         return typeof (uint);
394                                 case "unsignedLong":
395                                         return typeof (ulong);
396                                 case "string":
397                                         return typeof (string);
398                                 case "anyType":
399                                         return typeof (object);
400                                 case "guid":
401                                         return typeof (Guid);
402                                 case "char":
403                                         return typeof (char);
404                                 }
405                                 break;
406                         }
407                         return null;
408                 }
409
410
411                 internal static object PredefinedTypeStringToObject (string s,
412                         string name, XmlReader reader)
413                 {
414                         switch (name) {
415                         case "anyURI":
416                                 return new Uri(s,UriKind.RelativeOrAbsolute);
417                         case "boolean":
418                                 return XmlConvert.ToBoolean (s);
419                         case "base64Binary":
420                                 return Convert.FromBase64String (s);
421                         case "dateTime":
422                                 return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
423                         case "duration":
424                                 return XmlConvert.ToTimeSpan (s);
425                         case "QName":
426                                 int idx = s.IndexOf (':');
427                                 string l = idx < 0 ? s : s.Substring (idx + 1);
428                                 return idx < 0 ? new QName (l) :
429                                         new QName (l, reader.LookupNamespace (
430                                                 s.Substring (0, idx)));
431                         case "decimal":
432                                 return XmlConvert.ToDecimal (s);
433                         case "double":
434                                 return XmlConvert.ToDouble (s);
435                         case "float":
436                                 return XmlConvert.ToSingle (s);
437                         case "byte":
438                                 return XmlConvert.ToSByte (s);
439                         case "short":
440                                 return XmlConvert.ToInt16 (s);
441                         case "int":
442                                 return XmlConvert.ToInt32 (s);
443                         case "long":
444                                 return XmlConvert.ToInt64 (s);
445                         case "unsignedByte":
446                                 return XmlConvert.ToByte (s);
447                         case "unsignedShort":
448                                 return XmlConvert.ToUInt16 (s);
449                         case "unsignedInt":
450                                 return XmlConvert.ToUInt32 (s);
451                         case "unsignedLong":
452                                 return XmlConvert.ToUInt64 (s);
453                         case "string":
454                                 return s;
455                         case "guid":
456                                 return XmlConvert.ToGuid (s);
457                         case "anyType":
458                                 return s;
459                         case "char":
460                                 return (char) XmlConvert.ToUInt32 (s);
461                         default:
462                                 throw new Exception ("Unanticipated primitive type: " + name);
463                         }
464                 }
465
466                 List<SerializationMap> contracts = new List<SerializationMap> ();
467
468                 public KnownTypeCollection ()
469                 {
470                 }
471
472                 protected override void ClearItems ()
473                 {
474                         base.Clear ();
475                 }
476
477                 protected override void InsertItem (int index, Type type)
478                 {
479                         if (ShouldNotRegister (type))
480                                 return;
481                         if (!Contains (type)) {
482                                 TryRegister (type);
483                                 base.InsertItem (index, type);
484                         }
485                 }
486
487                 // FIXME: it could remove other types' dependencies.
488                 protected override void RemoveItem (int index)
489                 {
490                         lock (this)
491                                 DoRemoveItem (index);
492                 }
493
494                 void DoRemoveItem (int index)
495                 {
496                         Type t = base [index];
497                         List<SerializationMap> l = new List<SerializationMap> ();
498                         foreach (SerializationMap m in contracts) {
499                                 if (m.RuntimeType == t)
500                                         l.Add (m);
501                         }
502                         foreach (SerializationMap m in l) {
503                                 contracts.Remove (m);
504                                 base.RemoveItem (index);
505                         }
506                 }
507
508                 protected override void SetItem (int index, Type type)
509                 {
510                         if (ShouldNotRegister (type))
511                                 return;
512
513                         // Since this collection is not assured to be ordered, it ignores the whole Set operation if the type already exists.
514                         if (Contains (type))
515                                 return;
516
517                         if (index != Count)
518                                 RemoveItem (index);
519                         if (TryRegister (type))
520                                 base.InsertItem (index - 1, type);
521                 }
522
523                 internal SerializationMap FindUserMap (Type type)
524                 {
525                         lock (this) {
526                                 for (int i = 0; i < contracts.Count; i++)
527                                         if (type == contracts [i].RuntimeType)
528                                                 return contracts [i];
529                                 return null;
530                         }
531                 }
532
533                 internal SerializationMap FindUserMap (QName qname)
534                 {
535                         lock (this)
536                                 return contracts.FirstOrDefault (c => c.XmlName == qname);
537                 }
538
539                 internal SerializationMap FindUserMap (QName qname, Type type)
540                 {
541                         lock (this)
542                                 return contracts.FirstOrDefault (c => c.XmlName == qname && c.RuntimeType == type);
543                 }
544
545                 internal Type GetSerializedType (Type type)
546                 {
547                         if (IsPrimitiveNotEnum (type))
548                                 return type;
549                         Type element = GetCollectionElementType (type);
550                         if (element == null)
551                                 return type;
552                         QName name = GetQName (type);
553                         var map = FindUserMap (name, type);
554                         if (map != null)
555                                 return map.RuntimeType;
556                         return type;
557                 }
558
559                 internal QName GetQName (Type type)
560                 {
561                         SerializationMap map = FindUserMap (type);
562                         if (map != null)
563                                 // already mapped.
564                                 return map.XmlName;
565                         return GetStaticQName (type);
566                 }
567
568                 public static QName GetStaticQName (Type type)
569                 {
570                         if (IsPrimitiveNotEnum (type))
571                                 return GetPrimitiveTypeName (type);
572
573                         if (type.IsEnum)
574                                 return GetEnumQName (type);
575
576                         QName qname = GetContractQName (type);
577                         if (qname != null)
578                                 return qname;
579
580                         if (type.GetInterface ("System.Xml.Serialization.IXmlSerializable") != null)
581                                 //FIXME: Reusing GetSerializableQName here, since we just
582                                 //need name of the type..
583                                 return GetSerializableQName (type);
584
585                         qname = GetCollectionContractQName (type);
586                         if (qname != null)
587                                 return qname;
588
589                         Type element = GetCollectionElementType (type);
590                         if (element != null) {
591                                 if (type.IsInterface || IsCustomCollectionType (type, element))
592                                         return GetCollectionQName (element);
593                         }
594
595                         if (GetAttribute<SerializableAttribute> (type) != null)
596                                 return GetSerializableQName (type);
597
598                         // default type map - still uses GetContractQName().
599                         return GetContractQName (type, null, null);
600                 }
601
602                 internal static QName GetContractQName (Type type)
603                 {
604                         var a = GetAttribute<DataContractAttribute> (type);
605                         return a == null ? null : GetContractQName (type, a.Name, a.Namespace);
606                 }
607
608                 static QName GetCollectionContractQName (Type type)
609                 {
610                         var a = GetAttribute<CollectionDataContractAttribute> (type);
611                         return a == null ? null : GetContractQName (type, a.Name, a.Namespace);
612                 }
613
614                 static QName GetContractQName (Type type, string name, string ns)
615                 {
616                         if (name == null)
617                                 name = GetDefaultName (type);
618                         else if (type.IsGenericType) {
619                                 var args = type.GetGenericArguments ();
620                                 for (int i = 0; i < args.Length; i++)
621                                         name = name.Replace ("{" + i + "}", GetStaticQName (args [i]).Name);
622                         }
623
624                         if (ns == null)
625                                 ns = GetDefaultNamespace (type);
626                         return new QName (name, ns);
627                 }
628
629                 static QName GetEnumQName (Type type)
630                 {
631                         string name = null, ns = null;
632
633                         if (!type.IsEnum)
634                                 return null;
635
636                         var dca = GetAttribute<DataContractAttribute> (type);
637
638                         if (dca != null) {
639                                 ns = dca.Namespace;
640                                 name = dca.Name;
641                         }
642
643                         if (ns == null)
644                                 ns = GetDefaultNamespace (type);
645
646                         if (name == null)
647                                 name = type.Namespace == null ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
648
649                         return new QName (name, ns);
650                 }
651
652                 internal static string GetDefaultName (Type type)
653                 {
654                         // FIXME: there could be decent ways to get
655                         // the same result...
656                         string name = type.Namespace == null || type.Namespace.Length == 0 ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
657                         if (type.IsGenericType) {
658                                 name = name.Substring (0, name.IndexOf ('`')) + "Of";
659                                 foreach (var t in type.GetGenericArguments ())
660                                         name += t.Name; // FIXME: check namespaces too
661                         }
662                         return name;
663                 }
664
665                 internal static string GetDefaultNamespace (Type type)
666                 {
667                         foreach (ContractNamespaceAttribute a in type.Assembly.GetCustomAttributes (typeof (ContractNamespaceAttribute), true))
668                                 if (a.ClrNamespace == type.Namespace)
669                                         return a.ContractNamespace;
670                         return DefaultClrNamespaceBase + type.Namespace;
671                 }
672
673                 static QName GetCollectionQName (Type element)
674                 {
675                         QName eqname = GetStaticQName (element);
676
677                         string ns = eqname.Namespace;
678                         if (eqname.Namespace == MSSimpleNamespace)
679                                 //Arrays of Primitive types
680                                 ns = MSArraysNamespace;
681
682                         return new QName (
683                                 "ArrayOf" + XmlConvert.EncodeLocalName (eqname.Name),
684                                 ns);
685                 }
686
687                 static QName GetSerializableQName (Type type)
688                 {
689                         // First, check XmlSchemaProviderAttribute and try GetSchema() to see if it returns a schema in the expected format.
690                         var xpa = type.GetCustomAttribute<XmlSchemaProviderAttribute> (true);
691                         if (xpa != null) {
692                                 var mi = type.GetMethod (xpa.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
693                                 if (mi != null) {
694                                         try {
695                                                 var xss = new XmlSchemaSet ();
696                                                 return (XmlQualifiedName) mi.Invoke (null, new object [] {xss});
697                                         } catch {
698                                                 // ignore.
699                                         }
700                                 }
701                         }
702
703                         string xmlName = type.Name;
704                         if (type.IsGenericType) {
705                                 xmlName = xmlName.Substring (0, xmlName.IndexOf ('`')) + "Of";
706                                 foreach (var t in type.GetGenericArguments ())
707                                         xmlName += GetStaticQName (t).Name; // FIXME: check namespaces too
708                         }
709                         string xmlNamespace = GetDefaultNamespace (type);
710                         var x = GetAttribute<XmlRootAttribute> (type);
711                         if (x != null) {
712                                 xmlName = x.ElementName;
713                                 xmlNamespace = x.Namespace;
714                         }
715                         return new QName (XmlConvert.EncodeLocalName (xmlName), xmlNamespace);
716                 }
717
718                 static bool IsPrimitiveNotEnum (Type type)
719                 {
720                         if (type.IsEnum)
721                                 return false;
722                         if (Type.GetTypeCode (type) != TypeCode.Object) // explicitly primitive
723                                 return true;
724                         if (type == typeof (Guid) || type == typeof (object) || type == typeof(TimeSpan) || type == typeof(byte[]) || type == typeof(Uri) || type == typeof(DateTimeOffset)) // special primitives
725                                 return true;
726                         // DOM nodes
727                         if (type == typeof (XmlElement) || type == typeof (XmlNode []))
728                                 return true;
729                         // nullable
730                         if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>))
731                                 return IsPrimitiveNotEnum (type.GetGenericArguments () [0]);
732                         return false;
733                 }
734
735                 bool ShouldNotRegister (Type type)
736                 {
737                         return IsPrimitiveNotEnum (type);
738                 }
739
740                 internal bool TryRegister (Type type)
741                 {
742                         lock (this) {
743                                 return DoTryRegister (type);
744                         }
745                 }
746
747                 bool DoTryRegister (Type type)
748                 {
749                         // exclude predefined maps
750                         if (ShouldNotRegister (type))
751                                 return false;
752
753                         if (FindUserMap (type) != null)
754                                 return false;
755
756                         if (RegisterEnum (type) != null)
757                                 return true;
758
759                         if (RegisterDictionary (type) != null)
760                                 return true;
761
762                         if (RegisterCollectionContract (type) != null)
763                                 return true;
764
765                         if (RegisterContract (type) != null)
766                                 return true;
767
768                         if (RegisterIXmlSerializable (type) != null)
769                                 return true;
770
771                         if (RegisterCollection (type) != null)
772                                 return true;
773
774                         if (GetAttribute<SerializableAttribute> (type) != null) {
775                                 RegisterSerializable (type);
776                                 return true;
777                         }
778
779                         RegisterDefaultTypeMap (type);
780                         return true;
781                 }
782
783                 static Type GetCollectionElementType (Type type)
784                 {
785                         if (type.IsArray)
786                                 return type.GetElementType ();
787                         var ifaces = type.GetInterfacesOrSelfInterface ();
788                         foreach (Type i in ifaces)
789                                 if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (IEnumerable<>)))
790                                         return i.GetGenericArguments () [0];
791                         foreach (Type i in ifaces)
792                                 if (i == typeof (IEnumerable))
793                                         return typeof (object);
794                         return null;
795                 }
796
797                 internal static T GetAttribute<T> (ICustomAttributeProvider ap) where T : Attribute
798                 {
799                         object [] atts = ap.GetCustomAttributes (typeof (T), false);
800                         return atts.Length == 0 ? null : (T) atts [0];
801                 }
802
803                 private CollectionContractTypeMap RegisterCollectionContract (Type type)
804                 {
805                         var cdca = GetAttribute<CollectionDataContractAttribute> (type);
806                         if (cdca == null)
807                                 return null;
808
809                         Type element = GetCollectionElementType (type);
810                         if (element == null)
811                                 throw new InvalidDataContractException (String.Format ("Type '{0}' is marked as collection contract, but it is not a collection", type));
812                         if (type.GetMethod ("Add", new Type[] { element }) == null)
813                                 throw new InvalidDataContractException (String.Format ("Type '{0}' is marked as collection contract, but missing a public \"Add\" method", type));
814
815                         TryRegister (element); // must be registered before the name conflict check.
816
817                         QName qname = GetCollectionContractQName (type);
818                         CheckStandardQName (qname);
819                         var map = FindUserMap (qname, type);
820                         if (map != null) {
821                                 var cmap = map as CollectionContractTypeMap;
822                                 if (cmap == null) // The runtime type may still differ (between array and other IList; see bug #670560)
823                                         throw new InvalidOperationException (String.Format ("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, qname));
824                         }
825
826                         var ret = new CollectionContractTypeMap (type, cdca, element, qname, this);
827                         contracts.Add (ret);
828                         return ret;
829                 }
830
831                 private CollectionTypeMap RegisterCollection (Type type)
832                 {
833                         Type element = GetCollectionElementType (type);
834                         if (element == null)
835                                 return null;
836
837                         TryRegister (element);
838
839                         /*
840                          * To qualify as a custom collection type, a type must have
841                          * a public parameterless constructor and an "Add" method
842                          * with the correct parameter type in addition to implementing
843                          * one of the collection interfaces.
844                          * 
845                          */
846
847                         if (!type.IsArray && type.IsClass && !IsCustomCollectionType (type, element))
848                                 return null;
849
850                         QName qname = GetCollectionQName (element);
851
852                         var map = FindUserMap (qname, element);
853                         if (map != null) {
854                                 var cmap = map as CollectionTypeMap;
855                                 if (cmap == null) // The runtime type may still differ (between array and other IList; see bug #670560)
856                                         throw new InvalidOperationException (String.Format ("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, qname));
857                                 return cmap;
858                         }
859
860                         CollectionTypeMap ret =
861                                 new CollectionTypeMap (type, element, qname, this);
862                         contracts.Add (ret);
863                         return ret;
864                 }
865
866                 static bool IsCustomCollectionType (Type type, Type elementType)
867                 {
868                         if (!type.IsClass)
869                                 return false;
870                         if (type.GetConstructor (new Type [0]) == null)
871                                 return false;
872                         if (type.GetMethod ("Add", new Type[] { elementType }) == null)
873                                 return false;
874
875                         return true;
876                 }
877
878                 internal static bool IsInterchangeableCollectionType (Type contractType, Type graphType,
879                                                                       out QName collectionQName)
880                 {
881                         collectionQName = null;
882                         if (GetAttribute<CollectionDataContractAttribute> (contractType) != null)
883                                 return false;
884
885                         var type = contractType;
886                         if (type.IsGenericType)
887                                 type = type.GetGenericTypeDefinition ();
888
889                         var elementType = GetCollectionElementType (contractType);
890                         if (elementType == null)
891                                 return false;
892                         
893                         if (contractType.IsArray) {
894                                 if (!graphType.IsArray || !elementType.Equals (graphType.GetElementType ()))
895                                         throw new InvalidCastException (String.Format ("Type '{0}' cannot be converted into '{1}'.", graphType.GetElementType (), elementType));
896                         } else if (!contractType.IsInterface) {
897                                 if (GetAttribute<SerializableAttribute> (contractType) == null)
898                                         return false;
899
900                                 var graphElementType = GetCollectionElementType (graphType);
901                                 if (elementType != graphElementType)
902                                         return false;
903
904                                 if (!IsCustomCollectionType (contractType, elementType))
905                                         return false;
906                         } else if (type.Equals (typeof (IEnumerable)) || type.Equals (typeof (IList)) ||
907                                    type.Equals (typeof (ICollection))) {
908                                 if (!graphType.ImplementsInterface (contractType))
909                                         return false;
910                         } else if (type.Equals (typeof (IEnumerable<>)) || type.Equals (typeof (IList<>)) ||
911                                    type.Equals (typeof (ICollection<>))) {
912                                 var graphElementType = GetCollectionElementType (graphType);
913                                 if (graphElementType != elementType)
914                                         throw new InvalidCastException (String.Format (
915                                                 "Cannot convert type '{0}' into '{1}'.", graphType, contractType));
916
917                                 if (!graphType.ImplementsInterface (contractType))
918                                         return false;
919                         } else {
920                                 return false;
921                         }
922
923                         collectionQName = GetCollectionQName (elementType);
924                         return true;
925                 }
926
927                 static bool ImplementsInterface (Type type, Type iface)
928                 {
929                         foreach (var i in type.GetInterfacesOrSelfInterface ())
930                                 if (iface == i)
931                                         return true;
932                                         
933                         return false;
934                 }
935
936
937                 static bool TypeImplementsIEnumerable (Type type)
938                 {
939                         foreach (var iface in type.GetInterfacesOrSelfInterface ())
940                                 if (iface == typeof (IEnumerable) || (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IEnumerable<>)))
941                                         return true;
942                         
943                         return false;
944                 }
945
946                 static bool TypeImplementsIDictionary (Type type)
947                 {
948                         foreach (var iface in type.GetInterfacesOrSelfInterface ())
949                                 if (iface == typeof (IDictionary) || (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IDictionary<,>)))
950                                         return true;
951
952                         return false;
953                 }
954
955                 // it also supports contract-based dictionary.
956                 private DictionaryTypeMap RegisterDictionary (Type type)
957                 {
958                         if (!TypeImplementsIDictionary (type))
959                                 return null;
960
961                         var cdca = GetAttribute<CollectionDataContractAttribute> (type);
962
963                         DictionaryTypeMap ret =
964                                 new DictionaryTypeMap (type, cdca, this);
965
966                         TryRegister (ret.KeyType);
967                         TryRegister (ret.ValueType);
968
969                         var map = FindUserMap (ret.XmlName, type);
970                         if (map != null) {
971                                 var dmap = map as DictionaryTypeMap;
972                                 if (dmap == null) // The runtime type may still differ (between array and other IList; see bug #670560)
973                                         throw new InvalidOperationException (String.Format ("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, ret.XmlName));
974                         }
975                         contracts.Add (ret);
976
977                         return ret;
978                 }
979
980                 private SerializationMap RegisterSerializable (Type type)
981                 {
982                         QName qname = GetSerializableQName (type);
983
984                         if (FindUserMap (qname, type) != null)
985                                 throw new InvalidOperationException (String.Format ("There is already a registered type for XML name {0}", qname));
986
987                         SharedTypeMap ret = new SharedTypeMap (type, qname, this);
988                         contracts.Add (ret);
989                         ret.Initialize ();
990                         return ret;
991                 }
992
993                 private SerializationMap RegisterIXmlSerializable (Type type)
994                 {
995                         if (type.GetInterface ("System.Xml.Serialization.IXmlSerializable") == null)
996                                 return null;
997
998                         QName qname = GetSerializableQName (type);
999
1000                         if (FindUserMap (qname, type) != null)
1001                                 throw new InvalidOperationException (String.Format ("There is already a registered type for XML name {0}", qname));
1002
1003                         XmlSerializableMap ret = new XmlSerializableMap (type, qname, this);
1004                         contracts.Add (ret);
1005
1006                         return ret;
1007                 }
1008
1009                 void CheckStandardQName (QName qname)
1010                 {
1011                         switch (qname.Namespace) {
1012                         case XmlSchema.Namespace:
1013                         case XmlSchema.InstanceNamespace:
1014                         case MSSimpleNamespace:
1015                         case MSArraysNamespace:
1016                                 throw new InvalidOperationException (String.Format ("Namespace {0} is reserved and cannot be used for user serialization", qname.Namespace));
1017                         }
1018
1019                 }
1020
1021                 private SharedContractMap RegisterContract (Type type)
1022                 {
1023                         QName qname = GetContractQName (type);
1024                         if (qname == null)
1025                                 return null;
1026                         CheckStandardQName (qname);
1027                         if (FindUserMap (qname, type) != null)
1028                                 throw new InvalidOperationException (String.Format ("There is already a registered type for XML name {0}", qname));
1029
1030                         SharedContractMap ret = new SharedContractMap (type, qname, this);
1031                         contracts.Add (ret);
1032                         ret.Initialize ();
1033
1034                         if (type.BaseType != typeof (object)) {
1035                                 TryRegister (type.BaseType);
1036                                 if (!FindUserMap (type.BaseType).IsContractAllowedType)
1037                                         throw new InvalidDataContractException (String.Format ("To be serializable by data contract, type '{0}' cannot inherit from non-contract and non-Serializable type '{1}'", type, type.BaseType));
1038                         }
1039
1040                         object [] attrs = type.GetCustomAttributes (typeof (KnownTypeAttribute), true);
1041                         for (int i = 0; i < attrs.Length; i++) {
1042                                 KnownTypeAttribute kt = (KnownTypeAttribute) attrs [i];
1043                                 foreach (var t in kt.GetTypes (type))
1044                                         TryRegister (t);
1045                         }
1046
1047                         return ret;
1048                 }
1049
1050                 DefaultTypeMap RegisterDefaultTypeMap (Type type)
1051                 {
1052                         DefaultTypeMap ret = new DefaultTypeMap (type, this);
1053                         contracts.Add (ret);
1054                         ret.Initialize ();
1055                         return ret;
1056                 }
1057
1058                 private EnumMap RegisterEnum (Type type)
1059                 {
1060                         QName qname = GetEnumQName (type);
1061                         if (qname == null)
1062                                 return null;
1063
1064                         if (FindUserMap (qname, type) != null)
1065                                 throw new InvalidOperationException (String.Format ("There is already a registered type for XML name {0}", qname));
1066
1067                         EnumMap ret =
1068                                 new EnumMap (type, qname, this);
1069                         contracts.Add (ret);
1070                         return ret;
1071                 }
1072         }
1073 }
1074 #endif