Merge pull request #561 from akoeplinger/bufferless-fix
[mono.git] / mcs / class / corlib / System.Reflection / Binder.cs
1 // System.Reflection.Binder
2 //
3 // Authors:
4 //      Sean MacIsaac (macisaac@ximian.com)
5 //      Paolo Molaro (lupus@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Marek Safar (marek.safar@gmail.com)
8 //
9 // (C) Ximian, Inc. 2001 - 2003
10 // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
11 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35
36 namespace System.Reflection
37 {
38         [ComVisible (true)]
39         [Serializable]
40         [ClassInterface(ClassInterfaceType.AutoDual)]
41         public abstract class Binder
42         {
43                 protected Binder () {}
44
45                 public abstract FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture);
46                 public abstract MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state);
47                 public abstract object ChangeType (object value, Type type, CultureInfo culture);
48                 public abstract void ReorderArgumentArray( ref object[] args, object state);
49                 public abstract MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
50                 public abstract PropertyInfo SelectProperty( BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
51
52                 static readonly Binder default_binder = new Default ();
53
54                 internal static Binder DefaultBinder {
55                         get {
56                                 return default_binder;
57                         }
58                 }
59                 
60                 internal bool ConvertArgs (object[] args, ParameterInfo[] pinfo, CultureInfo culture, bool exactMatch)
61                 {
62                         if (args == null) {
63                                 if (pinfo.Length == 0)
64                                         return true;
65                                 
66                                 throw new TargetParameterCountException ();
67                         }
68
69                         if (pinfo.Length != args.Length)
70                                 throw new TargetParameterCountException ();
71                         
72                         for (int i = 0; i < args.Length; ++i) {
73                                 var arg = args [i];
74                                 var pi = pinfo [i];
75                                 if (arg == Type.Missing) {
76                                         args [i] = pi.DefaultValue;
77                                         continue;
78                                 }
79
80                                 if (arg != null && arg.GetType () == pi.ParameterType)
81                                         continue;
82
83                                 if (exactMatch)
84                                         return false;
85
86                                 object v = ChangeType (arg, pi.ParameterType, culture);
87                                 if (v == null && args [i] != null)
88                                         return false;
89         
90                                 args [i] = v;
91                         }
92
93                         return true;
94                 }
95
96                 internal static int GetDerivedLevel (Type type) 
97                 {
98                         Type searchType = type;
99                         int level = 1;
100
101                         while (searchType.BaseType != null) 
102                         {
103                                 level++;
104                                 searchType = searchType.BaseType;
105                         }
106
107                         return level;
108                 }
109
110                 internal static MethodBase FindMostDerivedMatch (MethodBase [] match) 
111                 {
112                         int highLevel = 0;
113                         int matchId = -1;
114                         int count = match.Length;
115
116                         for (int current = 0; current < count; current++) 
117                         {
118                                 MethodBase m = match [current];
119                                 int level = GetDerivedLevel (m.DeclaringType);
120                                 if (level == highLevel)
121                                         throw new AmbiguousMatchException ();
122                                 // If the argument types differ we
123                                 // have an ambigous match, as well
124                                 if (matchId >= 0) {
125                                         ParameterInfo[] p1 = m.GetParameters ();
126                                         ParameterInfo[] p2 = match [matchId].GetParameters ();
127                                         bool equal = true;
128
129                                         if (p1.Length != p2.Length)
130                                                 equal = false;
131                                         else {
132                                                 int i;
133
134                                                 for (i = 0; i < p1.Length; ++i) {
135                                                         if (p1 [i].ParameterType != p2 [i].ParameterType) {
136                                                                 equal = false;
137                                                                 break;
138                                                         }
139                                                 }
140                                         }
141
142                                         if (!equal)
143                                                 throw new AmbiguousMatchException ();
144                                 }
145
146                                 if (level > highLevel) 
147                                 {
148                                         highLevel = level;
149                                         matchId = current;
150                                 }
151                         }
152
153                         return match[matchId];
154                 }
155
156                 internal sealed class Default : Binder {
157                         public override FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture) 
158                         {
159                                 if (match == null)
160                                         throw new ArgumentNullException ("match");
161                                 foreach (FieldInfo f in match) {
162                                         if (check_type (value.GetType (), f.FieldType))
163                                                 return f;
164                                 }
165                                 return null;
166                         }
167
168                         public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
169                         {
170                                 Type[] types;
171                                 if (args == null)
172                                         types = Type.EmptyTypes;
173                                 else {
174                                         types = new Type [args.Length];
175                                         for (int i = 0; i < args.Length; ++i) {
176                                                 if (args [i] != null)
177                                                         types [i] = args [i].GetType ();
178                                         }
179                                 }
180
181                                 MethodBase selected = null;
182                                 if (names != null) {
183                                         foreach (var m in match) {
184                                                 var parameters = m.GetParameters ();
185                                                 int i;
186
187                                                 /*
188                                                  * Find the corresponding parameter for each parameter name,
189                                                  * reorder types/modifiers array during the search.
190                                                  */
191                                                 Type[] newTypes = (Type[])types.Clone ();
192                                                 ParameterModifier[] newModifiers = modifiers != null ? (ParameterModifier[])modifiers.Clone () : null;
193                                                 for (i = 0; i < names.Length; ++i) {
194                                                         /* Find the corresponding parameter */
195                                                         int nindex = -1;
196                                                         for (int j = 0; j < parameters.Length; ++j) {
197                                                                 if (parameters [j].Name == names [i]) {
198                                                                         nindex = j;
199                                                                         break;
200                                                                 }
201                                                         }
202                                                         if (nindex == -1)
203                                                                 break;
204                                                         if (i < newTypes.Length && nindex < types.Length)
205                                                                 newTypes [i] = types [nindex];
206                                                         if (modifiers != null && i < newModifiers.Length && nindex < modifiers.Length)
207                                                                 newModifiers [i] = modifiers [nindex];
208                                                 }
209                                                 if (i < names.Length)
210                                                         continue;
211
212                                                 selected = SelectMethod (bindingAttr, new MethodBase [] { m }, newTypes, newModifiers, true, args);
213                                                 if (selected != null)
214                                                         break;
215                                         }
216                                 } else {
217                                         selected = SelectMethod (bindingAttr, match, types, modifiers, true, args);
218                                 }
219
220                                 state = null;
221                                 if (selected != null && names != null)
222                                         ReorderParameters (names, ref args, selected);
223
224                                 if (selected != null) {
225                                         if (args == null)
226                                                 args = EmptyArray<object>.Value;
227         
228                                         AdjustArguments (selected, ref args);
229                                 }
230
231                                 return selected;
232                         }
233
234                         // probably belongs in ReorderArgumentArray
235                         static void AdjustArguments (MethodBase selected, ref object [] args)
236                         {
237                                 var parameters = selected.GetParameters ();
238                                 var parameters_length = parameters.Length;
239                                 if (parameters_length == 0)
240                                         return;
241
242                                 var last_parameter = parameters [parameters.Length - 1];
243                                 Type last_parameter_type = last_parameter.ParameterType;
244                                 if (!Attribute.IsDefined (last_parameter, typeof (ParamArrayAttribute)))
245                                         return;
246
247                                 var args_length = args.Length;
248                                 var param_args_count = args_length + 1 - parameters_length;
249                                 var first_vararg_index = args_length - param_args_count;
250                                 if (first_vararg_index < args_length) {
251                                         var first_vararg = args [first_vararg_index];
252                                         if (first_vararg != null && first_vararg.GetType () == last_parameter_type)
253                                                 return;
254                                 }
255                                 
256                                 var params_args = Array.CreateInstance (last_parameter_type.GetElementType (), param_args_count);
257                                 for (int i = 0; i < param_args_count; i++)
258                                         params_args.SetValue (args [first_vararg_index + i], i);
259
260                                 var adjusted = new object [parameters_length];
261                                 Array.Copy (args, adjusted, parameters_length - 1);
262                                 
263                                 adjusted [adjusted.Length - 1] = params_args;
264                                 args = adjusted;
265                         }
266
267                         void ReorderParameters (string [] names, ref object [] args, MethodBase selected)
268                         {
269                                 object [] newArgs = new object [args.Length];
270                                 Array.Copy (args, newArgs, args.Length);
271                                 ParameterInfo [] plist = selected.GetParameters ();
272                                 for (int n = 0; n < names.Length; n++)
273                                         for (int p = 0; p < plist.Length; p++) {
274                                                 if (names [n] == plist [p].Name) {
275                                                         newArgs [p] = args [n];
276                                                         break;
277                                                 }
278                                         }
279                                 Array.Copy (newArgs, args, args.Length);
280                         }
281
282                         static bool IsArrayAssignable (Type object_type, Type target_type)
283                         {
284                                 if (object_type.IsArray && target_type.IsArray)
285                                         return IsArrayAssignable (object_type.GetElementType (), target_type.GetElementType ());
286                                                 
287                                 if (target_type.IsAssignableFrom (object_type))
288                                         return true;
289
290                                 return false;
291                         }
292                         
293                         public override object ChangeType (object value, Type type, CultureInfo culture)
294                         {
295                                 if (value == null)
296                                         return null;
297                                 Type vtype = value.GetType ();
298                                 if (type.IsByRef)
299                                         type = type.GetElementType ();
300                                 if (vtype == type || type.IsInstanceOfType (value))
301                                         return value;
302                                 if (vtype.IsArray && type.IsArray){
303                                         if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
304                                                 return value;
305                                 }
306
307                                 if (check_type (vtype, type)) {
308                                         // These are not supported by Convert
309                                         if (type.IsEnum)
310                                                 return Enum.ToObject (type, value);
311                                         if (vtype == typeof (Char)) {
312                                                 if (type == typeof (double))
313                                                         return (double)(char)value;
314                                                 if (type == typeof (float))
315                                                         return (float)(char)value;
316                                         }
317                                         if (vtype == typeof (IntPtr) && type.IsPointer)
318                                                 return value;
319                                         return Convert.ChangeType (value, type);
320                                 }
321                                 return null;
322                         }
323
324                         [MonoTODO ("This method does not do anything in Mono")]
325                         public override void ReorderArgumentArray (ref object[] args, object state)
326                         {
327                                 //do nothing until we support named arguments
328                                 //throw new NotImplementedException ();
329                         }
330
331                         private static bool check_type (Type from, Type to) {
332                                 if (from == to)
333                                         return true;
334
335                                 if (from == null)
336                                         return true;
337
338                                 if (to.IsByRef != from.IsByRef)
339                                         return false;
340
341                                 if (to.IsInterface)
342                                         return to.IsAssignableFrom (from);
343
344                                 if (to.IsEnum) {
345                                         to = Enum.GetUnderlyingType (to);
346                                         if (from == to)
347                                                 return true;
348                                 }
349
350                                 if (to.IsGenericType && to.GetGenericTypeDefinition () == typeof (Nullable<>) && to.GetGenericArguments ()[0] == from)
351                                         return true;
352
353                                 TypeCode fromt = Type.GetTypeCode (from);
354                                 TypeCode tot = Type.GetTypeCode (to);
355
356                                 switch (fromt) {
357                                 case TypeCode.Char:
358                                         switch (tot) {
359                                         case TypeCode.UInt16:
360                                         case TypeCode.UInt32:
361                                         case TypeCode.Int32:
362                                         case TypeCode.UInt64:
363                                         case TypeCode.Int64:
364                                         case TypeCode.Single:
365                                         case TypeCode.Double:
366                                                 return true;
367                                         }
368                                         return to == typeof (object);
369                                 case TypeCode.Byte:
370                                         switch (tot) {
371                                         case TypeCode.Char:
372                                         case TypeCode.UInt16:
373                                         case TypeCode.Int16:
374                                         case TypeCode.UInt32:
375                                         case TypeCode.Int32:
376                                         case TypeCode.UInt64:
377                                         case TypeCode.Int64:
378                                         case TypeCode.Single:
379                                         case TypeCode.Double:
380                                                 return true;
381                                         }
382                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
383                                 case TypeCode.SByte:
384                                         switch (tot) {
385                                         case TypeCode.Int16:
386                                         case TypeCode.Int32:
387                                         case TypeCode.Int64:
388                                         case TypeCode.Single:
389                                         case TypeCode.Double:
390                                                 return true;
391                                         }
392                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
393                                 case TypeCode.UInt16:
394                                         switch (tot) {
395                                         case TypeCode.UInt32:
396                                         case TypeCode.Int32:
397                                         case TypeCode.UInt64:
398                                         case TypeCode.Int64:
399                                         case TypeCode.Single:
400                                         case TypeCode.Double:
401                                                 return true;
402                                         }
403                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
404                                 case TypeCode.Int16:
405                                         switch (tot) {
406                                         case TypeCode.Int32:
407                                         case TypeCode.Int64:
408                                         case TypeCode.Single:
409                                         case TypeCode.Double:
410                                                 return true;
411                                         }
412                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
413                                 case TypeCode.UInt32:
414                                         switch (tot) {
415                                         case TypeCode.UInt64:
416                                         case TypeCode.Int64:
417                                         case TypeCode.Single:
418                                         case TypeCode.Double:
419                                                 return true;
420                                         }
421                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
422                                 case TypeCode.Int32:
423                                         switch (tot) {
424                                         case TypeCode.Int64:
425                                         case TypeCode.Single:
426                                         case TypeCode.Double:
427                                                 return true;
428                                         }
429                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
430                                 case TypeCode.UInt64:
431                                 case TypeCode.Int64:
432                                         switch (tot) {
433                                         case TypeCode.Single:
434                                         case TypeCode.Double:
435                                                 return true;
436                                         }
437                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
438                                 case TypeCode.Single:
439                                         return tot == TypeCode.Double || to == typeof (object);
440                                 default:
441                                         /* TODO: handle valuetype -> byref */
442                                         if (to == typeof (object) && from.IsValueType)
443                                                 return true;
444                                         if (to.IsPointer && from == typeof (IntPtr))
445                                                 return true;
446
447                                         return to.IsAssignableFrom (from);
448                                 }
449                         }
450
451                         private static bool check_arguments (Type[] types, ParameterInfo[] args, bool allowByRefMatch) {
452                                 for (int i = 0; i < types.Length; ++i) {
453                                         bool match = check_type (types [i], args [i].ParameterType);
454                                         if (!match && allowByRefMatch) {
455                                                 Type param_type = args [i].ParameterType;
456                                                 if (param_type.IsByRef)
457                                                         match = check_type (types [i], param_type.GetElementType ());
458                                         }
459                                         if (!match)
460                                                 return false;
461                                 }
462                                 return true;
463                         }
464
465                         public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase [] match, Type [] types, ParameterModifier [] modifiers)
466                         {
467                                 return SelectMethod (bindingAttr, match, types, modifiers,
468                                         false, null);
469                         }
470
471                         MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers, bool allowByRefMatch, object[] parameters)
472                         {
473                                 MethodBase m;
474                                 int i, j;
475
476                                 if (match == null)
477                                         throw new ArgumentNullException ("match");
478
479                                 /* first look for an exact match... */
480                                 MethodBase exact_match = null;
481                                 for (i = 0; i < match.Length; ++i) {
482                                         m = match [i];
483                                         ParameterInfo[] args = m.GetParameters ();
484                                         if (args.Length != types.Length)
485                                                 continue;
486                                         for (j = 0; j < types.Length; ++j) {
487                                                 if (types [j] != args [j].ParameterType)
488                                                         break;
489                                         }
490                                         if (j == types.Length) {
491                                                 if (exact_match != null) {
492                                                         exact_match = null;
493                                                         break;
494                                                 } else {
495                                                         exact_match = m;
496                                                 }
497                                         }
498                                 }
499                                 if (exact_match != null)
500                                         return exact_match;
501
502                                 /* Try methods with ParamArray attribute */
503                                 bool isdefParamArray = false;
504                                 Type elementType = null;
505                                 for (i = 0; i < match.Length; ++i) {
506                                         m = match [i];
507                                         ParameterInfo[] args = m.GetParameters ();
508                                         if (args.Length > types.Length + 1)
509                                                 continue;
510                                         else if (args.Length == 0)
511                                                 continue;
512                                         isdefParamArray = Attribute.IsDefined (args [args.Length - 1], typeof (ParamArrayAttribute));
513                                         if (!isdefParamArray)
514                                                 continue;
515                                         elementType = args [args.Length - 1].ParameterType.GetElementType ();
516                                         for (j = 0; j < types.Length; ++j) {
517                                                 if (j < (args.Length - 1) && types [j] != args [j].ParameterType)
518                                                         break;
519                                                 else if (j >= (args.Length - 1) && types [j] != elementType) 
520                                                         break;
521                                         }
522                                         if (j == types.Length)
523                                                 return m;
524                                 }
525
526                                 if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
527                                         return null;
528
529                                 MethodBase result = null;
530                                 for (i = 0; i < match.Length; ++i) {
531                                         m = match [i];
532                                         ParameterInfo[] args = m.GetParameters ();
533                                         if (args.Length != types.Length)
534                                                 continue;
535                                         if (!check_arguments (types, args, allowByRefMatch))
536                                                 continue;
537
538                                         if (result != null)
539                                                 result = GetBetterMethod (result, m, types);
540                                         else
541                                                 result = m;
542                                 }
543
544                                 if (result != null || parameters == null || types.Length != parameters.Length)
545                                         return result;
546
547                                 // Xamarin-5278: try with parameters that are COM objects
548                                 // REVIEW: do we also need to implement best method match?
549                                 for (i = 0; i < match.Length; ++i) {
550                                         m = match [i];
551                                         ParameterInfo[] methodArgs = m.GetParameters ();
552                                         if (methodArgs.Length != types.Length)
553                                                 continue;
554                                         for (j = 0; j < types.Length; ++j) {
555                                                 var requiredType = methodArgs [j].ParameterType;
556                                                 if (types [j] == requiredType)
557                                                         continue;
558 #if !MOBILE
559                                                 if (types [j] == typeof (__ComObject) && requiredType.IsInterface) {
560                                                         var iface = Marshal.GetComInterfaceForObject (parameters [j], requiredType);
561                                                         if (iface != IntPtr.Zero) {
562                                                                 // the COM object implements the desired interface
563                                                                 Marshal.Release (iface);
564                                                                 continue;
565                                                         }
566                                                 }
567 #endif
568                                                 break;
569                                         }
570
571                                         if (j == types.Length)
572                                                 return m;
573                                 }
574                                 return null;
575                         }
576
577                         MethodBase GetBetterMethod (MethodBase m1, MethodBase m2, Type [] types)
578                         {
579                                 ParameterInfo [] pl1 = m1.GetParameters ();
580                                 ParameterInfo [] pl2 = m2.GetParameters ();
581                                 int prev = 0;
582                                 for (int i = 0; i < pl1.Length; i++) {
583                                         int cmp = CompareCloserType (pl1 [i].ParameterType, pl2 [i].ParameterType);
584                                         if (cmp != 0 && prev != 0 && prev != cmp)
585                                                 throw new AmbiguousMatchException ();
586                                         if (cmp != 0)
587                                                 prev = cmp;
588                                 }
589                                 if (prev != 0)
590                                         return prev > 0 ? m2 : m1;
591
592                                 Type dt1 = m1.DeclaringType;
593                                 Type dt2 = m2.DeclaringType;
594                                 if (dt1 != dt2) {
595                                         if (dt1.IsSubclassOf(dt2))
596                                                 return m1;
597                                         if (dt2.IsSubclassOf(dt1))
598                                                 return m2;
599                                 }
600
601                                 bool va1 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
602                                 bool va2 = (m2.CallingConvention & CallingConventions.VarArgs) != 0;
603                                 if (va1 && !va2)
604                                         return m2;
605                                 if (va2 && !va1)
606                                         return m1;
607
608                                 throw new AmbiguousMatchException ();
609                         }
610
611                         int CompareCloserType (Type t1, Type t2)
612                         {
613                                 if (t1 == t2)
614                                         return 0;
615                                 if (t1.IsGenericParameter && !t2.IsGenericParameter)
616                                         return 1; // t2
617                                 if (!t1.IsGenericParameter && t2.IsGenericParameter)
618                                         return -1; // t1
619                                 if (t1.HasElementType && t2.HasElementType)
620                                         return CompareCloserType (
621                                                 t1.GetElementType (),
622                                                 t2.GetElementType ());
623
624                                 if (t1.IsSubclassOf (t2))
625                                         return -1; // t1
626                                 if (t2.IsSubclassOf (t1))
627                                         return 1; // t2
628
629                                 if (t1.IsInterface && Array.IndexOf (t2.GetInterfaces (), t1) >= 0)
630                                         return 1; // t2
631                                 if (t2.IsInterface && Array.IndexOf (t1.GetInterfaces (), t2) >= 0)
632                                         return -1; // t1
633
634                                 // What kind of cases could reach here?
635                                 return 0;
636                         }
637
638                         public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
639                         {
640                                 if (match == null || match.Length == 0)
641                                         throw new ArgumentException ("No properties provided", "match");
642
643                                 bool haveRet = (returnType != null);
644                                 int idxlen = (indexes != null) ? indexes.Length : -1;
645                                 PropertyInfo result = null;
646                                 int i;
647                                 int best_score = Int32.MaxValue - 1;
648                                 int fail_score = Int32.MaxValue;
649                                 int level = 0;
650                                 
651                                 for (i = match.Length - 1; i >= 0; i--) {
652                                         PropertyInfo p = match [i];
653                                         ParameterInfo[] args = p.GetIndexParameters ();
654                                         if (idxlen >= 0 && idxlen != args.Length)
655                                                 continue;
656
657                                         if (haveRet && p.PropertyType != returnType)
658                                                 continue;
659
660                                         int score = Int32.MaxValue - 1;
661                                         if (idxlen > 0) {
662                                                 score = check_arguments_with_score (indexes, args);
663                                                 if (score == -1)
664                                                         continue;
665                                         }
666
667                                         int new_level = GetDerivedLevel (p.DeclaringType);
668                                         if (result != null) {
669                                                 if (best_score < score)
670                                                         continue;
671
672                                                 if (best_score == score) {
673                                                         if (level == new_level) {
674                                                                 // Keep searching. May be there's something
675                                                                 // better for us.
676                                                                 fail_score = score;
677                                                                 continue;
678                                                         }
679
680                                                         if (level > new_level)
681                                                                 continue;
682                                                 }
683                                         }
684
685                                         result = p;
686                                         best_score = score;
687                                         level = new_level;
688                                 }
689
690                                 if (fail_score <= best_score)
691                                         throw new AmbiguousMatchException ();
692
693                                 return result;
694                         }
695
696                         static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
697                         {
698                                 int worst = -1;
699
700                                 for (int i = 0; i < types.Length; ++i) {
701                                         int res = check_type_with_score (types [i], args [i].ParameterType);
702                                         if (res == -1)
703                                                 return -1;
704
705                                         if (worst < res)
706                                                 worst = res;
707                                 }
708
709                                 return worst;
710                         }
711
712                         // 0 -> same type or null and !valuetype
713                         // 1 -> to == Enum
714                         // 2 -> value type that don't lose data
715                         // 3 -> to == IsAssignableFrom
716                         // 4 -> to == object
717                         static int check_type_with_score (Type from, Type to)
718                         {
719                                 if (from == null)
720                                         return to.IsValueType ? -1 : 0;
721
722                                 if (from == to)
723                                         return 0;
724
725                                 if (to == typeof (object))
726                                         return 4;
727
728                                 TypeCode fromt = Type.GetTypeCode (from);
729                                 TypeCode tot = Type.GetTypeCode (to);
730
731                                 switch (fromt) {
732                                 case TypeCode.Char:
733                                         switch (tot) {
734                                         case TypeCode.UInt16:
735                                                 return 0;
736
737                                         case TypeCode.UInt32:
738                                         case TypeCode.Int32:
739                                         case TypeCode.UInt64:
740                                         case TypeCode.Int64:
741                                         case TypeCode.Single:
742                                         case TypeCode.Double:
743                                                 return 2;
744                                         }
745                                         return -1;
746                                 case TypeCode.Byte:
747                                         switch (tot) {
748                                         case TypeCode.Char:
749                                         case TypeCode.UInt16:
750                                         case TypeCode.Int16:
751                                         case TypeCode.UInt32:
752                                         case TypeCode.Int32:
753                                         case TypeCode.UInt64:
754                                         case TypeCode.Int64:
755                                         case TypeCode.Single:
756                                         case TypeCode.Double:
757                                                 return 2;
758                                         }
759                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
760                                 case TypeCode.SByte:
761                                         switch (tot) {
762                                         case TypeCode.Int16:
763                                         case TypeCode.Int32:
764                                         case TypeCode.Int64:
765                                         case TypeCode.Single:
766                                         case TypeCode.Double:
767                                                 return 2;
768                                         }
769                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
770                                 case TypeCode.UInt16:
771                                         switch (tot) {
772                                         case TypeCode.UInt32:
773                                         case TypeCode.Int32:
774                                         case TypeCode.UInt64:
775                                         case TypeCode.Int64:
776                                         case TypeCode.Single:
777                                         case TypeCode.Double:
778                                                 return 2;
779                                         }
780                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
781                                 case TypeCode.Int16:
782                                         switch (tot) {
783                                         case TypeCode.Int32:
784                                         case TypeCode.Int64:
785                                         case TypeCode.Single:
786                                         case TypeCode.Double:
787                                                 return 2;
788                                         }
789                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
790                                 case TypeCode.UInt32:
791                                         switch (tot) {
792                                         case TypeCode.UInt64:
793                                         case TypeCode.Int64:
794                                         case TypeCode.Single:
795                                         case TypeCode.Double:
796                                                 return 2;
797                                         }
798                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
799                                 case TypeCode.Int32:
800                                         switch (tot) {
801                                         case TypeCode.Int64:
802                                         case TypeCode.Single:
803                                         case TypeCode.Double:
804                                                 return 2;
805                                         }
806                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
807                                 case TypeCode.UInt64:
808                                 case TypeCode.Int64:
809                                         switch (tot) {
810                                         case TypeCode.Single:
811                                         case TypeCode.Double:
812                                                 return 2;
813                                         }
814                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
815                                 case TypeCode.Single:
816                                         return tot == TypeCode.Double ? 2 : -1;
817                                 default:
818                                         return (to.IsAssignableFrom (from)) ? 3 : -1;
819                                 }
820                         }
821                 }
822         }
823 }
824