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