* ReflectionHelper.cs: Added check in CheckSerializableType(). Interfaces
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / ReflectionHelper.cs
1 // 
2 // System.Xml.Serialization.ReflectionHelper 
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2003 Ximian, Inc.\r
8 //
9
10 using System.Reflection;
11 using System.Collections;
12 \r
13 namespace System.Xml.Serialization\r
14 {\r
15         internal class ReflectionHelper\r
16         {\r
17                 Hashtable _clrTypes = new Hashtable ();
18                 Hashtable _schemaTypes = new Hashtable ();
19 \r
20                 public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
21                 {
22                         string mapKey = xmlType + "/" + ns;
23                         if (!_schemaTypes.ContainsKey (xmlType))
24                                 _schemaTypes.Add (mapKey, map);
25                 }
26
27                 public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
28                 {
29                         string mapKey = xmlType + "/" + ns;
30                         return _schemaTypes[mapKey] as XmlTypeMapping;
31                 }
32
33                 public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
34                 {
35                         if (type == typeof(object)) ns = "";
36                         string mapKey = type.FullName + "/" + ns;
37                         if (!_clrTypes.ContainsKey (mapKey))
38                                 _clrTypes.Add (mapKey, map);
39                 }
40
41                 public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
42                 {
43                         if (type == typeof(object)) ns = "";
44                         string mapKey = type.FullName + "/" + ns;
45                         return _clrTypes[mapKey] as XmlTypeMapping;
46                 }       \r
47 \r
48                 public Exception CreateError (XmlTypeMapping map, string message)
49                 {
50                         return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
51                 }
52                 
53                 public static void CheckSerializableType (Type type)
54                 {
55                         if (type.IsArray) return;
56                         
57                         if (type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
58                                 throw new InvalidOperationException (type.Name + " cannot be serialized because it does not have a default public constructor");
59                                 
60                         if (type.IsInterface)
61                                 throw new InvalidOperationException (type.Name + " cannot be serialized because it is an interface");
62                 }
63         }\r
64 }