Moved chain building and validation from Mono.Security to System
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / ReflectionHelper.cs
index 23ca1fabd7ece6d5b5f57ded8eac17f6b1384023..a44b9b836f821ea8a82251d34bdff5bce3a77c41 100644 (file)
@@ -4,7 +4,7 @@
 // Author:
 //   Lluis Sanchez Gual (lluis@ximian.com)
 //
-// Copyright (C) 2003 Ximian, Inc.\r
+// Copyright (C) 2003 Ximian, Inc.
 //
 
 //
 
 using System.Reflection;
 using System.Collections;
-\r
-namespace System.Xml.Serialization\r
-{\r
-       internal class ReflectionHelper\r
-       {\r
+
+namespace System.Xml.Serialization
+{
+       internal class ReflectionHelper
+       {
                Hashtable _clrTypes = new Hashtable ();
                Hashtable _schemaTypes = new Hashtable ();
-\r
+
                public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
                {
                        string mapKey = xmlType + "/" + ns;
-                       if (!_schemaTypes.ContainsKey (xmlType))
+                       if (!_schemaTypes.ContainsKey (mapKey))
                                _schemaTypes.Add (mapKey, map);
                }
 
@@ -64,30 +64,64 @@ namespace System.Xml.Serialization
                        if (type == typeof(object)) ns = "";
                        string mapKey = type.FullName + "/" + ns;
                        return _clrTypes[mapKey] as XmlTypeMapping;
-               }       \r
-\r
+               }       
+
                public Exception CreateError (XmlTypeMapping map, string message)
                {
                        return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
                }
-               
-               public static void CheckSerializableType (Type type)
+
+#if NET_2_0
+               static readonly ParameterModifier [] empty_modifiers = new ParameterModifier [0];
+#endif         
+
+               public static void CheckSerializableType (Type type, bool allowPrivateConstructors)
                {
                        if (type.IsArray) return;
                        
-                       if (type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
+#if NET_2_0
+                       if (!allowPrivateConstructors && type.GetConstructor (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, empty_modifiers) == null && !type.IsAbstract && !type.IsValueType)
+#else
+                       if (!allowPrivateConstructors && type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
+#endif
                                throw new InvalidOperationException (type.FullName + " cannot be serialized because it does not have a default public constructor");
                                
-                       if (type.IsInterface)
+                       if (type.IsInterface && !TypeTranslator.GetTypeData (type).IsListType)
                                throw new InvalidOperationException (type.FullName + " cannot be serialized because it is an interface");
                                
                        Type t = type;
+                       Type oldt = null;
                        do {
                                if (!t.IsPublic && !t.IsNestedPublic)
                                        throw new InvalidOperationException (type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
+                               oldt = t;
                                t = t.DeclaringType;
                        }
-                       while (t != null);
+                       while (t != null && t != oldt);
+               }
+               
+               public static string BuildMapKey (Type type)
+               {
+                       return type.FullName + "::";
+               }
+               
+               public static string BuildMapKey (MethodInfo method, string tag)
+               {
+                       string res = method.DeclaringType.FullName + ":" + method.ReturnType.FullName + " " + method.Name + "(";
+                       
+                       ParameterInfo[] pars = method.GetParameters ();
+                       
+                       for (int n=0; n<pars.Length; n++)
+                       {
+                               if (n > 0) res += ", ";
+                               res += pars[n].ParameterType.FullName;
+                       }
+                       res += ")";
+                       
+                       if (tag != null)
+                               res += ":" + tag;
+                               
+                       return res;
                }
-       }\r
+       }
 }