New tests.
[mono.git] / mcs / class / corlib / System.Reflection / Binder.cs
index 323db37245444359d0064149c0a3b702b7c5c00e..615db74d7936d59b98fa795831711593b1e045b1 100644 (file)
@@ -36,9 +36,7 @@ using System.Runtime.InteropServices;
 
 namespace System.Reflection
 {
-#if NET_2_0
        [ComVisible (true)]
-#endif
        [Serializable]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        public abstract class Binder
@@ -165,13 +163,45 @@ namespace System.Reflection
                                                        types [i] = args [i].GetType ();
                                        }
                                }
-                               MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers);
+                               MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers, true);
                                state = null;
                                if (names != null)
                                        ReorderParameters (names, ref args, selected);
+
+                               if (selected != null) {
+                                       if (args == null)
+                                               args = new object [0];
+       
+                                       AdjustArguments (selected, ref args);
+                               }
+
                                return selected;
                        }
 
+                       // probably belongs in ReorderArgumentArray
+                       static void AdjustArguments (MethodBase selected, ref object [] args)
+                       {
+                               var parameters = selected.GetParameters ();
+                               if (parameters.Length == 0)
+                                       return;
+
+                               var last_parameter = parameters [parameters.Length - 1];
+                               if (!Attribute.IsDefined (last_parameter, typeof (ParamArrayAttribute)))
+                                       return;
+
+                               var adjusted = new object [parameters.Length];
+                               Array.Copy (args, adjusted, parameters.Length - 1);
+
+                               var param_args_count = args.Length + 1 - parameters.Length;
+                               var params_args = Array.CreateInstance (last_parameter.ParameterType.GetElementType (), param_args_count);
+
+                               for (int i = 0; i < param_args_count; i++)
+                                       params_args.SetValue (args [args.Length - param_args_count + i], i);
+
+                               adjusted [adjusted.Length - 1] = params_args;
+                               args = adjusted;
+                       }
+
                        void ReorderParameters (string [] names, ref object [] args, MethodBase selected)
                        {
                                object [] newArgs = new object [args.Length];
@@ -212,8 +242,20 @@ 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;
+                                       }
+                                       if (vtype == typeof (IntPtr) && type.IsPointer)
+                                               return value;
                                        return Convert.ChangeType (value, type);
+                               }
                                return null;
                        }
 
@@ -231,15 +273,24 @@ 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;
+                               }
+
+                               if (to.IsGenericType && to.GetGenericTypeDefinition () == typeof (Nullable<>) && to.GetGenericArguments ()[0] == from)
+                                       return true;
+
+                               TypeCode fromt = Type.GetTypeCode (from);
+                               TypeCode tot = Type.GetTypeCode (to);
+
                                switch (fromt) {
                                case TypeCode.Char:
                                        switch (tot) {
@@ -328,20 +379,34 @@ namespace System.Reflection
                                        /* TODO: handle valuetype -> byref */
                                        if (to == typeof (object) && from.IsValueType)
                                                return true;
+                                       if (to.IsPointer && from == typeof (IntPtr))
+                                               return true;
 
                                        return to.IsAssignableFrom (from);
                                }
                        }
 
-                       private static bool check_arguments (Type[] types, ParameterInfo[] args) {
+                       private static bool check_arguments (Type[] types, ParameterInfo[] args, bool allowByRefMatch) {
                                for (int i = 0; i < types.Length; ++i) {
-                                       if (!check_type (types [i], args [i].ParameterType))
+                                       bool match = check_type (types [i], args [i].ParameterType);
+                                       if (!match && allowByRefMatch) {
+                                               Type param_type = args [i].ParameterType;
+                                               if (param_type.IsByRef)
+                                                       match = check_type (types [i], param_type.GetElementType ());
+                                       }
+                                       if (!match)
                                                return false;
                                }
                                return true;
                        }
 
-                       public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
+                       public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase [] match, Type [] types, ParameterModifier [] modifiers)
+                       {
+                               return SelectMethod (bindingAttr, match, types, modifiers,
+                                       false);
+                       }
+
+                       MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers, bool allowByRefMatch)
                        {
                                MethodBase m;
                                int i, j;
@@ -350,6 +415,7 @@ namespace System.Reflection
                                        throw new ArgumentNullException ("match");
 
                                /* first look for an exact match... */
+                               MethodBase exact_match = null;
                                for (i = 0; i < match.Length; ++i) {
                                        m = match [i];
                                        ParameterInfo[] args = m.GetParameters ();
@@ -359,9 +425,17 @@ namespace System.Reflection
                                                if (types [j] != args [j].ParameterType)
                                                        break;
                                        }
-                                       if (j == types.Length)
-                                               return m;
+                                       if (j == types.Length) {
+                                               if (exact_match != null) {
+                                                       exact_match = null;
+                                                       break;
+                                               } else {
+                                                       exact_match = m;
+                                               }
+                                       }
                                }
+                               if (exact_match != null)
+                                       return exact_match;
 
                                /* Try methods with ParamArray attribute */
                                bool isdefParamArray = false;
@@ -369,7 +443,7 @@ namespace System.Reflection
                                for (i = 0; i < match.Length; ++i) {
                                        m = match [i];
                                        ParameterInfo[] args = m.GetParameters ();
-                                       if (args.Length > types.Length)
+                                       if (args.Length > types.Length + 1)
                                                continue;
                                        else if (args.Length == 0)
                                                continue;
@@ -396,7 +470,7 @@ namespace System.Reflection
                                        ParameterInfo[] args = m.GetParameters ();
                                        if (args.Length != types.Length)
                                                continue;
-                                       if (!check_arguments (types, args))
+                                       if (!check_arguments (types, args, allowByRefMatch))
                                                continue;
 
                                        if (result != null)
@@ -410,15 +484,6 @@ namespace System.Reflection
 
                        MethodBase GetBetterMethod (MethodBase m1, MethodBase m2, Type [] types)
                        {
-#if NET_2_0
-                               if (m1.IsGenericMethodDefinition && 
-                                   !m2.IsGenericMethodDefinition)
-                                       return m2;
-                               if (m2.IsGenericMethodDefinition && 
-                                   !m1.IsGenericMethodDefinition)
-                                       return m1;
-#endif
-
                                ParameterInfo [] pl1 = m1.GetParameters ();
                                ParameterInfo [] pl2 = m2.GetParameters ();
                                int prev = 0;
@@ -455,12 +520,10 @@ namespace System.Reflection
                        {
                                if (t1 == t2)
                                        return 0;
-#if NET_2_0
                                if (t1.IsGenericParameter && !t2.IsGenericParameter)
                                        return 1; // t2
                                if (!t1.IsGenericParameter && t2.IsGenericParameter)
                                        return -1; // t1
-#endif
                                if (t1.HasElementType && t2.HasElementType)
                                        return CompareCloserType (
                                                t1.GetElementType (),
@@ -499,7 +562,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;