2007-04-20 Atsushi Enomoto <atsushi@ximian.com>
[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.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Reflection;
32 using System.Collections;
33
34 namespace System.Xml.Serialization
35 {
36         internal class ReflectionHelper
37         {
38                 Hashtable _clrTypes = new Hashtable ();
39                 Hashtable _schemaTypes = new Hashtable ();
40
41                 public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
42                 {
43                         string mapKey = xmlType + "/" + ns;
44                         if (!_schemaTypes.ContainsKey (mapKey))
45                                 _schemaTypes.Add (mapKey, map);
46                 }
47
48                 public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
49                 {
50                         string mapKey = xmlType + "/" + ns;
51                         return _schemaTypes[mapKey] as XmlTypeMapping;
52                 }
53
54                 public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
55                 {
56                         if (type == typeof(object)) ns = "";
57                         string mapKey = type.FullName + "/" + ns;
58                         if (!_clrTypes.ContainsKey (mapKey))
59                                 _clrTypes.Add (mapKey, map);
60                 }
61
62                 public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
63                 {
64                         if (type == typeof(object)) ns = "";
65                         string mapKey = type.FullName + "/" + ns;
66                         return _clrTypes[mapKey] as XmlTypeMapping;
67                 }       
68
69                 public Exception CreateError (XmlTypeMapping map, string message)
70                 {
71                         return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
72                 }
73
74                 static readonly ParameterModifier [] empty_modifiers = new ParameterModifier [0];
75
76                 public static void CheckSerializableType (Type type, bool allowPrivateConstructors)
77                 {
78                         if (type.IsArray) return;
79                         
80 #if NET_2_0
81                         if (!allowPrivateConstructors && type.GetConstructor (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, empty_modifiers) == null && !type.IsAbstract && !type.IsValueType)
82 #else
83                         if (!allowPrivateConstructors && type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
84 #endif
85                                 throw new InvalidOperationException (type.FullName + " cannot be serialized because it does not have a default public constructor");
86                                 
87                         if (type.IsInterface && !TypeTranslator.GetTypeData (type).IsListType)
88                                 throw new InvalidOperationException (type.FullName + " cannot be serialized because it is an interface");
89                                 
90                         Type t = type;
91                         Type oldt = null;
92                         do {
93                                 if (!t.IsPublic && !t.IsNestedPublic)
94                                         throw new InvalidOperationException (type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
95                                 oldt = t;
96                                 t = t.DeclaringType;
97                         }
98                         while (t != null && t != oldt);
99                 }
100                 
101                 public static string BuildMapKey (Type type)
102                 {
103                         return type.FullName + "::";
104                 }
105                 
106                 public static string BuildMapKey (MethodInfo method, string tag)
107                 {
108                         string res = method.DeclaringType.FullName + ":" + method.ReturnType.FullName + " " + method.Name + "(";
109                         
110                         ParameterInfo[] pars = method.GetParameters ();
111                         
112                         for (int n=0; n<pars.Length; n++)
113                         {
114                                 if (n > 0) res += ", ";
115                                 res += pars[n].ParameterType.FullName;
116                         }
117                         res += ")";
118                         
119                         if (tag != null)
120                                 res += ":" + tag;
121                                 
122                         return res;
123                 }
124         }
125 }