[corlib] Parse datetime string using culture calendar. Fixes #18052
[mono.git] / mcs / class / corlib / System.Reflection / Binder.cs
index 780b21aed00690239574ea7f59577756ce15a6e9..7c0f829a38009aeef38578e6b3fda3755c6f2bf0 100644 (file)
@@ -4,12 +4,11 @@
 //     Sean MacIsaac (macisaac@ximian.com)
 //     Paolo Molaro (lupus@ximian.com)
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//     Marek Safar (marek.safar@gmail.com)
 //
 // (C) Ximian, Inc. 2001 - 2003
 // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 
 using System.Globalization;
 using System.Runtime.InteropServices;
+using System.Collections.Generic;
 
 namespace System.Reflection
 {
-#if NET_2_0
        [ComVisible (true)]
-#endif
        [Serializable]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        public abstract class Binder
@@ -52,7 +50,7 @@ namespace System.Reflection
                public abstract MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
                public abstract PropertyInfo SelectProperty( BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
 
-               static Binder default_binder = new Default ();
+               static readonly Binder default_binder = new Default ();
 
                internal static Binder DefaultBinder {
                        get {
@@ -60,22 +58,224 @@ namespace System.Reflection
                        }
                }
                
-               internal static bool ConvertArgs (Binder binder, object[] args, ParameterInfo[] pinfo, CultureInfo culture) {
+               internal void ConvertValues (object[] args, ParameterInfo[] pinfo, CultureInfo culture, bool exactMatch)
+               {
                        if (args == null) {
-                               if ( pinfo.Length == 0)
-                                       return true;
-                               else
-                                       throw new TargetParameterCountException ();
+                               if (pinfo.Length == 0)
+                                       return;
+                               
+                               throw new TargetParameterCountException ();
                        }
+
                        if (pinfo.Length != args.Length)
                                throw new TargetParameterCountException ();
+                       
                        for (int i = 0; i < args.Length; ++i) {
-                               object v = binder.ChangeType (args [i], pinfo[i].ParameterType, culture);
-                               if ((v == null) && (args [i] != null))
-                                       return false;
-                               args [i] = v;
+                               var arg = args [i];
+                               var pi = pinfo [i];
+                               if (arg == Type.Missing) {
+                                       args [i] = pi.DefaultValue;
+                                       continue;
+                               }
+
+                               args [i] = ConvertValue (arg, pi.ParameterType, culture, exactMatch);
+                       }
+               }
+
+               internal object ConvertValue (object value, Type type, CultureInfo culture, bool exactMatch)
+               {
+                       bool failed = false;
+                       var res = TryConvertToType (value, type, ref failed);
+                       if (!failed)
+                               return res;
+
+                       if (exactMatch || this == default_binder)
+                               throw new ArgumentException ("Object type " + value.GetType() + " cannot be converted to target type: " + type.FullName);
+
+                       return ChangeType (value, type, culture);
+               }
+
+               object TryConvertToType (object value, Type type, ref bool failed)
+               {
+                       if (type.IsInstanceOfType (value)) {
+                               return value;
+                       }
+
+                       if (type.IsByRef) {
+                       var elementType = type.GetElementType ();
+                       if (value == null || elementType.IsInstanceOfType (value)) {
+                                       return value;
+                               }
+                       }
+
+                       if (value == null)
+                               return value;
+
+                       if (type.IsEnum) {
+                               type = Enum.GetUnderlyingType (type);
+                               if (type == value.GetType ())
+                                       return value;
+                       }
+
+                       if (type.IsPrimitive) {
+                               var res = IsConvertibleToPrimitiveType (value, type);
+                               if (res != null)
+                                       return res;
+                       } else if (type.IsPointer) {
+                               var vtype = value.GetType ();
+                               if (vtype == typeof (IntPtr) || vtype == typeof (UIntPtr))
+                                       return value;
+                       }
+
+                       failed = true;
+                       return null;
+               }
+
+               // Binder uses some incompatible conversion rules. For example
+               // int value cannot be used with decimal parameter but in other
+               // ways it's more flexible than normal convertor, for example
+               // long value can be used with int based enum
+               static object IsConvertibleToPrimitiveType (object value, Type targetType)              
+               {
+                       var type = value.GetType ();
+                       if (type.IsEnum) {
+                               type = Enum.GetUnderlyingType (type);
+                               if (type == targetType)
+                                       return value;
+                       }
+
+                       var from = Type.GetTypeCode (type);
+                       var to = Type.GetTypeCode (targetType);
+
+                       switch (to) {
+                               case TypeCode.Char:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (Char) (Byte) value;
+                                               case TypeCode.UInt16:
+                                                       return value;
+                                       }
+                                       break;
+                               case TypeCode.Int16:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (Int16) (Byte) value;
+                                               case TypeCode.SByte:
+                                                       return (Int16) (SByte) value;                                           
+                                       }
+                                       break;
+                               case TypeCode.UInt16:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (UInt16) (Byte) value;
+                                               case TypeCode.Char:
+                                                       return value;
+                                       }
+                                       break;
+                               case TypeCode.Int32:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (Int32) (Byte) value;
+                                               case TypeCode.SByte:
+                                                       return (Int32) (SByte) value;
+                                               case TypeCode.Char:
+                                                       return (Int32) (Char) value;
+                                               case TypeCode.Int16:
+                                                       return (Int32) (Int16) value;
+                                               case TypeCode.UInt16:
+                                                       return (Int32) (UInt16) value;
+                                       }
+                                       break;
+                               case TypeCode.UInt32:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (UInt32) (Byte) value;
+                                               case TypeCode.Char:
+                                                       return (UInt32) (Char) value;
+                                               case TypeCode.UInt16:
+                                                       return (UInt32) (UInt16) value;
+                                       }
+                                       break;
+                               case TypeCode.Int64:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (Int64) (Byte) value;
+                                               case TypeCode.SByte:
+                                                       return (Int64) (SByte) value;                                                   
+                                               case TypeCode.Int16:
+                                                       return (Int64) (Int16) value;
+                                               case TypeCode.Char:
+                                                       return (Int64) (Char) value;
+                                               case TypeCode.UInt16:
+                                                       return (Int64) (UInt16) value;
+                                               case TypeCode.Int32:
+                                                       return (Int64) (Int32) value;
+                                               case TypeCode.UInt32:
+                                                       return (Int64) (UInt32) value;
+                                       }
+                                       break;
+                               case TypeCode.UInt64:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (UInt64) (Byte) value;
+                                               case TypeCode.Char:
+                                                       return (UInt64) (Char) value;
+                                               case TypeCode.UInt16:
+                                                       return (UInt64) (UInt16) value;
+                                               case TypeCode.UInt32:
+                                                       return (UInt64) (UInt32) value;
+                                       }
+                                       break;
+                               case TypeCode.Single:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (Single) (Byte) value;
+                                               case TypeCode.SByte:
+                                                       return (Single) (SByte) value;
+                                               case TypeCode.Int16:
+                                                       return (Single) (Int16) value;
+                                               case TypeCode.Char:
+                                                       return (Single) (Char) value;
+                                               case TypeCode.UInt16:
+                                                       return (Single) (UInt16) value;
+                                               case TypeCode.Int32:
+                                                       return (Single) (Int32) value;
+                                               case TypeCode.UInt32:
+                                                       return (Single) (UInt32) value;
+                                               case TypeCode.Int64:
+                                                       return (Single) (Int64) value;
+                                               case TypeCode.UInt64:
+                                                       return (Single) (UInt64) value;
+                                       }
+                                       break;
+                               case TypeCode.Double:
+                                       switch (from) {
+                                               case TypeCode.Byte:
+                                                       return (Double) (Byte) value;
+                                               case TypeCode.SByte:
+                                                       return (Double) (SByte) value;
+                                               case TypeCode.Char:
+                                                       return (Double) (Char) value;
+                                               case TypeCode.Int16:
+                                                       return (Double) (Int16) value;
+                                               case TypeCode.UInt16:
+                                                       return (Double) (UInt16) value;
+                                               case TypeCode.Int32:
+                                                       return (Double) (Int32) value;
+                                               case TypeCode.UInt32:
+                                                       return (Double) (UInt32) value;
+                                               case TypeCode.Int64:
+                                                       return (Double) (Int64) value;
+                                               case TypeCode.UInt64:
+                                                       return (Double) (UInt64) value;
+                                               case TypeCode.Single:
+                                                       return (Double) (Single) value;
+                                       }
+                                       break;
                        }
-                       return true;
+
+                       // Everything else is rejected
+                       return null;
                }
 
                internal static int GetDerivedLevel (Type type) 
@@ -107,8 +307,8 @@ namespace System.Reflection
                                // If the argument types differ we
                                // have an ambigous match, as well
                                if (matchId >= 0) {
-                                       ParameterInfo[] p1 = m.GetParameters ();
-                                       ParameterInfo[] p2 = match [matchId].GetParameters ();
+                                       ParameterInfo[] p1 = m.GetParametersInternal ();
+                                       ParameterInfo[] p2 = match [matchId].GetParametersInternal ();
                                        bool equal = true;
 
                                        if (p1.Length != p2.Length)
@@ -150,9 +350,6 @@ namespace System.Reflection
                                return null;
                        }
 
-                       //
-                       // 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;
@@ -165,18 +362,105 @@ namespace System.Reflection
                                                        types [i] = args [i].GetType ();
                                        }
                                }
-                               MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers);
+
+                               MethodBase selected = null;
+                               if (names != null) {
+                                       foreach (var m in match) {
+                                               var parameters = m.GetParametersInternal ();
+                                               int i;
+
+                                               /*
+                                                * Find the corresponding parameter for each parameter name,
+                                                * reorder types/modifiers array during the search.
+                                                */
+                                               Type[] newTypes = new Type [types.Length];
+                                               Array.FastCopy (types, 0, newTypes, 0, types.Length);
+
+                                               ParameterModifier[] newModifiers = null;
+                                               if (modifiers != null) {
+                                                       newModifiers = new ParameterModifier [modifiers.Length];
+                                                       Array.FastCopy (modifiers, 0, newModifiers, 0, modifiers.Length);
+                                               }
+
+                                               for (i = 0; i < names.Length; ++i) {
+                                                       /* Find the corresponding parameter */
+                                                       int nindex = -1;
+                                                       for (int j = 0; j < parameters.Length; ++j) {
+                                                               if (parameters [j].Name == names [i]) {
+                                                                       nindex = j;
+                                                                       break;
+                                                               }
+                                                       }
+                                                       if (nindex == -1)
+                                                               break;
+                                                       if (i < newTypes.Length && nindex < types.Length)
+                                                               newTypes [i] = types [nindex];
+                                                       if (modifiers != null && i < newModifiers.Length && nindex < modifiers.Length)
+                                                               newModifiers [i] = modifiers [nindex];
+                                               }
+                                               if (i < names.Length)
+                                                       continue;
+
+                                               selected = SelectMethod (bindingAttr, new MethodBase [] { m }, newTypes, newModifiers, true, ref args);
+                                               if (selected != null)
+                                                       break;
+                                       }
+                               } else {
+                                       selected = SelectMethod (bindingAttr, match, types, modifiers, true, ref args);
+                               }
+
                                state = null;
-                               if (names != null)
+                               if (selected != null && names != null)
                                        ReorderParameters (names, ref args, selected);
+
+                               if (selected != null) {
+                                       if (args == null)
+                                               args = EmptyArray<object>.Value;
+       
+                                       AdjustArguments (selected, ref args);
+                               }
+
                                return selected;
                        }
 
+                       // probably belongs in ReorderArgumentArray
+                       static void AdjustArguments (MethodBase selected, ref object [] args)
+                       {
+                               var parameters = selected.GetParametersInternal ();
+                               var parameters_length = parameters.Length;
+                               if (parameters_length == 0)
+                                       return;
+
+                               var last_parameter = parameters [parameters.Length - 1];
+                               Type last_parameter_type = last_parameter.ParameterType;
+                               if (!Attribute.IsDefined (last_parameter, typeof (ParamArrayAttribute)))
+                                       return;
+
+                               var args_length = args.Length;
+                               var param_args_count = args_length + 1 - parameters_length;
+                               var first_vararg_index = args_length - param_args_count;
+                               if (first_vararg_index < args_length) {
+                                       var first_vararg = args [first_vararg_index];
+                                       if (first_vararg != null && first_vararg.GetType () == last_parameter_type)
+                                               return;
+                               }
+                               
+                               var params_args = Array.CreateInstance (last_parameter_type.GetElementType (), param_args_count);
+                               for (int i = 0; i < param_args_count; i++)
+                                       params_args.SetValue (args [first_vararg_index + i], i);
+
+                               var adjusted = new object [parameters_length];
+                               Array.Copy (args, adjusted, parameters_length - 1);
+                               
+                               adjusted [adjusted.Length - 1] = params_args;
+                               args = adjusted;
+                       }
+
                        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 ();
+                               ParameterInfo [] plist = selected.GetParametersInternal ();
                                for (int n = 0; n < names.Length; n++)
                                        for (int p = 0; p < plist.Length; p++) {
                                                if (names [n] == plist [p].Name) {
@@ -186,43 +470,10 @@ namespace System.Reflection
                                        }
                                Array.Copy (newArgs, args, args.Length);
                        }
-
-                       static bool IsArrayAssignable (Type object_type, Type target_type)
-                       {
-                               if (object_type.IsArray && target_type.IsArray)
-                                       return IsArrayAssignable (object_type.GetElementType (), target_type.GetElementType ());
-                                               
-                               if (target_type.IsAssignableFrom (object_type))
-                                       return true;
-
-                               return false;
-                       }
                        
                        public override object ChangeType (object value, Type type, CultureInfo culture)
                        {
-                               if (value == null)
-                                       return null;
-                               Type vtype = value.GetType ();
-                               if (type.IsByRef)
-                                       type = type.GetElementType ();
-                               if (vtype == type || type.IsInstanceOfType (value))
-                                       return value;
-                               if (vtype.IsArray && type.IsArray){
-                                       if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
-                                               return value;
-                               }
-
-                               if (check_type (vtype, type)) {
-                                       // These are not supported by Convert
-                                       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;
+                               throw new NotSupportedException ();
                        }
 
                        [MonoTODO ("This method does not do anything in Mono")]
@@ -239,15 +490,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) {
@@ -336,20 +596,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)
+                       {
+                               object[] args = null;
+                               return SelectMethod (bindingAttr, match, types, modifiers, false, ref args);
+                       }
+
+                       MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers, bool allowByRefMatch, ref object[] arguments)
                        {
                                MethodBase m;
                                int i, j;
@@ -358,77 +632,146 @@ 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 ();
-                                       if (args.Length != types.Length)
+                                       if (m.GetParametersCount () != types.Length)
                                                continue;
+
+                                       ParameterInfo[] args = m.GetParametersInternal ();
                                        for (j = 0; j < types.Length; ++j) {
                                                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;
-                               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 (arguments != null) {
+                                       for (i = 0; i < match.Length; ++i) {
+                                               m = match [i];
+
+                                               var count = m.GetParametersCount ();
+                                               if (count == 0 || count > types.Length + 1)
+                                                       continue;
+
+                                               var pi = m.GetParametersInternal ();
+                                               if (!Attribute.IsDefined (pi [pi.Length - 1], typeof (ParamArrayAttribute)))
+                                                       continue;
+
+                                               var elementType = pi [pi.Length - 1].ParameterType.GetElementType ();
+                                               for (j = 0; j < types.Length; ++j) {
+                                                       if (j < (pi.Length - 1) && types [j] != pi [j].ParameterType)
+                                                               break;
+                                                       
+                                                       if (j >= (pi.Length - 1) && types [j] != elementType) 
+                                                               break;
+                                               }
+
+                                               if (j == types.Length)
+                                                       return m;
                                        }
-                                       if (j == types.Length)
-                                               return m;
                                }
 
-                               if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
+                               if ((bindingAttr & BindingFlags.ExactBinding) != 0)
                                        return null;
 
                                MethodBase result = null;
+                               ParameterInfo[] result_pi = null;
                                for (i = 0; i < match.Length; ++i) {
                                        m = match [i];
-                                       ParameterInfo[] args = m.GetParameters ();
-                                       if (args.Length != types.Length)
-                                               continue;
-                                       if (!check_arguments (types, args))
+                                       var pi = m.GetParametersInternal ();
+                                       var full_pi = pi;
+                                       if (pi.Length != types.Length) {
+                                               if ((bindingAttr & BindingFlags.OptionalParamBinding) == 0)
+                                                       continue;
+
+                                               List<ParameterInfo> pi_reduced = null;
+                                               for (var ii = pi.Length - 1; ii >= 0; --ii) {
+                                                       if ((pi [ii].Attributes & ParameterAttributes.HasDefault) == 0)
+                                                               break;
+
+                                                       if (pi_reduced == null) {
+                                                               pi_reduced = new List<ParameterInfo> (pi);
+                                                       }
+
+                                                       pi_reduced.RemoveAt (ii);
+                                               }
+
+                                               if (pi_reduced == null || pi_reduced.Count != types.Length)
+                                                       continue;
+
+                                               pi = pi_reduced.ToArray ();
+                                       }
+
+                                       if (!check_arguments (types, pi, allowByRefMatch))
                                                continue;
 
-                                       if (result != null)
+                                       if (result != null) {
                                                result = GetBetterMethod (result, m, types);
-                                       else
-                                               result = m;
+                                               if (result != m)
+                                                       continue;
+                                       }
+
+                                       result = m;
+                                       result_pi = full_pi;
                                }
 
-                               return result;
+                               if (result != null) {
+                                       i = arguments == null ? 0 : arguments.Length;
+                                       Array.Resize (ref arguments, result_pi.Length);
+                                       for (; i < arguments.Length; ++i)
+                                               arguments [i] = result_pi [i].DefaultValue;
+
+                                       return result;
+                               }
+
+                               if (arguments == null || types.Length != arguments.Length)
+                                       return null;
+
+                               // Xamarin-5278: try with parameters that are COM objects
+                               // REVIEW: do we also need to implement best method match?
+                               for (i = 0; i < match.Length; ++i) {
+                                       m = match [i];
+                                       ParameterInfo[] methodArgs = m.GetParametersInternal ();
+                                       if (methodArgs.Length != types.Length)
+                                               continue;
+                                       for (j = 0; j < types.Length; ++j) {
+                                               var requiredType = methodArgs [j].ParameterType;
+                                               if (types [j] == requiredType)
+                                                       continue;
+#if !MOBILE
+                                               if (types [j] == typeof (__ComObject) && requiredType.IsInterface) {
+                                                       var iface = Marshal.GetComInterfaceForObject (arguments [j], requiredType);
+                                                       if (iface != IntPtr.Zero) {
+                                                               // the COM object implements the desired interface
+                                                               Marshal.Release (iface);
+                                                               continue;
+                                                       }
+                                               }
+#endif
+                                               break;
+                                       }
+
+                                       if (j == types.Length)
+                                               return m;
+                               }
+                               return null;
                        }
 
                        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 ();
+                               ParameterInfo [] pl1 = m1.GetParametersInternal ();
+                               ParameterInfo [] pl2 = m2.GetParametersInternal ();
                                int prev = 0;
                                for (int i = 0; i < pl1.Length; i++) {
                                        int cmp = CompareCloserType (pl1 [i].ParameterType, pl2 [i].ParameterType);
@@ -463,12 +806,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 (),
@@ -507,7 +848,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;