Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / corlib / System.Reflection / Binder.cs
index ca241f9bcb1d8d2b8c5f966d1bad540af7f16b9f..898d4ad38489227329ceb132e21f1772fef0f7aa 100644 (file)
@@ -100,9 +100,33 @@ namespace System.Reflection
 
                        for (int current = 0; current < count; current++) 
                        {
-                               int level = GetDerivedLevel (match[current].DeclaringType);
+                               MethodBase m = match [current];
+                               int level = GetDerivedLevel (m.DeclaringType);
                                if (level == highLevel)
                                        throw new AmbiguousMatchException ();
+                               // If the argument types differ we
+                               // have an ambigous match, as well
+                               if (matchId >= 0) {
+                                       ParameterInfo[] p1 = m.GetParameters ();
+                                       ParameterInfo[] p2 = match [matchId].GetParameters ();
+                                       bool equal = true;
+
+                                       if (p1.Length != p2.Length)
+                                               equal = false;
+                                       else {
+                                               int i;
+
+                                               for (i = 0; i < p1.Length; ++i) {
+                                                       if (p1 [i].ParameterType != p2 [i].ParameterType) {
+                                                               equal = false;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+
+                                       if (!equal)
+                                               throw new AmbiguousMatchException ();
+                               }
 
                                if (level > highLevel) 
                                {
@@ -126,7 +150,9 @@ namespace System.Reflection
                                return null;
                        }
 
-                       [MonoTODO]
+                       //
+                       // FIXME: There was a MonoTODO, but it does not explain what the problem is
+                       // 
                        public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
                        {
                                Type[] types;
@@ -148,16 +174,17 @@ namespace System.Reflection
 
                        void ReorderParameters (string [] names, ref object [] args, MethodBase selected)
                        {
+                               object [] newArgs = new object [args.Length];
+                               Array.Copy (args, newArgs, args.Length);
                                ParameterInfo [] plist = selected.GetParameters ();
                                for (int n = 0; n < names.Length; n++)
                                        for (int p = 0; p < plist.Length; p++) {
                                                if (names [n] == plist [p].Name) {
-                                                       object o = args [n];
-                                                       args [n] = args [p];
-                                                       args [p] = o;
+                                                       newArgs [p] = args [n];
+                                                       break;
                                                }
-                                               break;
                                        }
+                               Array.Copy (newArgs, args, args.Length);
                        }
 
                        static bool IsArrayAssignable (Type object_type, Type target_type)
@@ -185,12 +212,22 @@ namespace System.Reflection
                                                return value;
                                }
 
-                               if (check_type (vtype, type))
+                               if (check_type (vtype, type)) {
+                                       // These are not supported by Convert
+                                       if (type.IsEnum)
+                                               return Enum.ToObject (type, value);
+                                       if (vtype == typeof (Char)) {
+                                               if (type == typeof (double))
+                                                       return (double)(char)value;
+                                               if (type == typeof (float))
+                                                       return (float)(char)value;
+                                       }
                                        return Convert.ChangeType (value, type);
+                               }
                                return null;
                        }
 
-                       [MonoTODO]
+                       [MonoTODO ("This method does not do anything in Mono")]
                        public override void ReorderArgumentArray (ref object[] args, object state)
                        {
                                //do nothing until we support named arguments
@@ -204,15 +241,21 @@ namespace System.Reflection
                                if (from == null)
                                        return true;
 
-                               TypeCode fromt = Type.GetTypeCode (from);
-                               TypeCode tot = Type.GetTypeCode (to);
-
                                if (to.IsByRef != from.IsByRef)
                                        return false;
 
                                if (to.IsInterface)
                                        return to.IsAssignableFrom (from);
 
+                               if (to.IsEnum) {
+                                       to = Enum.GetUnderlyingType (to);
+                                       if (from == to)
+                                               return true;
+                               }
+
+                               TypeCode fromt = Type.GetTypeCode (from);
+                               TypeCode tot = Type.GetTypeCode (to);
+
                                switch (fromt) {
                                case TypeCode.Char:
                                        switch (tot) {
@@ -336,6 +379,33 @@ namespace System.Reflection
                                                return m;
                                }
 
+                               /* Try methods with ParamArray attribute */
+                               bool isdefParamArray = false;
+                               Type elementType = null;
+                               for (i = 0; i < match.Length; ++i) {
+                                       m = match [i];
+                                       ParameterInfo[] args = m.GetParameters ();
+                                       if (args.Length > types.Length)
+                                               continue;
+                                       else if (args.Length == 0)
+                                               continue;
+                                       isdefParamArray = Attribute.IsDefined (args [args.Length - 1], typeof (ParamArrayAttribute));
+                                       if (!isdefParamArray)
+                                               continue;
+                                       elementType = args [args.Length - 1].ParameterType.GetElementType ();
+                                       for (j = 0; j < types.Length; ++j) {
+                                               if (j < (args.Length - 1) && types [j] != args [j].ParameterType)
+                                                       break;
+                                               else if (j >= (args.Length - 1) && types [j] != elementType) 
+                                                       break;
+                                       }
+                                       if (j == types.Length)
+                                               return m;
+                               }
+
+                               if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
+                                       return null;
+
                                MethodBase result = null;
                                for (i = 0; i < match.Length; ++i) {
                                        m = match [i];
@@ -378,8 +448,17 @@ namespace System.Reflection
                                if (prev != 0)
                                        return prev > 0 ? m2 : m1;
 
+                               Type dt1 = m1.DeclaringType;
+                               Type dt2 = m2.DeclaringType;
+                               if (dt1 != dt2) {
+                                       if (dt1.IsSubclassOf(dt2))
+                                               return m1;
+                                       if (dt2.IsSubclassOf(dt1))
+                                               return m2;
+                               }
+
                                bool va1 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
-                               bool va2 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
+                               bool va2 = (m2.CallingConvention & CallingConventions.VarArgs) != 0;
                                if (va1 && !va2)
                                        return m2;
                                if (va2 && !va1)
@@ -436,7 +515,7 @@ namespace System.Reflection
                                        if (idxlen >= 0 && idxlen != args.Length)
                                                continue;
 
-                                       if (haveRet && !check_type (p.PropertyType, returnType))
+                                       if (haveRet && p.PropertyType != returnType)
                                                continue;
 
                                        int score = Int32.MaxValue - 1;