2 // System.Xml.Serialization.ReflectionHelper
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) 2003 Ximian, Inc.
\r
10 using System.Reflection;
11 using System.Collections;
13 namespace System.Xml.Serialization
\r
15 internal class ReflectionHelper
\r
17 Hashtable _clrTypes = new Hashtable ();
18 Hashtable _schemaTypes = new Hashtable ();
20 public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
22 string mapKey = xmlType + "/" + ns;
23 if (!_schemaTypes.ContainsKey (xmlType))
24 _schemaTypes.Add (mapKey, map);
27 public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
29 string mapKey = xmlType + "/" + ns;
30 return _schemaTypes[mapKey] as XmlTypeMapping;
33 public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
35 if (type == typeof(object)) ns = "";
36 string mapKey = type.FullName + "/" + ns;
37 if (!_clrTypes.ContainsKey (mapKey))
38 _clrTypes.Add (mapKey, map);
41 public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
43 if (type == typeof(object)) ns = "";
44 string mapKey = type.FullName + "/" + ns;
45 return _clrTypes[mapKey] as XmlTypeMapping;
48 public Exception CreateError (XmlTypeMapping map, string message)
50 return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
53 public static void CheckSerializableType (Type type)
55 if (type.IsArray) return;
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");
61 throw new InvalidOperationException (type.Name + " cannot be serialized because it is an interface");
64 throw new InvalidOperationException (type.Name + " is inaccessible due to its protection level. Only public types can be processed");