Adjust sleeping values
[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
171                                 if (selected != null) {
172                                         if (args == null)
173                                                 args = new object [0];
174         
175                                         AdjustArguments (selected, ref args);
176                                 }
177
178                                 return selected;
179                         }
180
181                         // probably belongs in ReorderArgumentArray
182                         static void AdjustArguments (MethodBase selected, ref object [] args)
183                         {
184                                 var parameters = selected.GetParameters ();
185                                 if (parameters.Length == 0)
186                                         return;
187
188                                 var last_parameter = parameters [parameters.Length - 1];
189                                 if (!Attribute.IsDefined (last_parameter, typeof (ParamArrayAttribute)))
190                                         return;
191
192                                 var adjusted = new object [parameters.Length];
193                                 Array.Copy (args, adjusted, parameters.Length - 1);
194
195                                 var param_args_count = args.Length + 1 - parameters.Length;
196                                 var params_args = Array.CreateInstance (last_parameter.ParameterType.GetElementType (), param_args_count);
197
198                                 for (int i = 0; i < param_args_count; i++)
199                                         params_args.SetValue (args [args.Length - param_args_count + i], i);
200
201                                 adjusted [adjusted.Length - 1] = params_args;
202                                 args = adjusted;
203                         }
204
205                         void ReorderParameters (string [] names, ref object [] args, MethodBase selected)
206                         {
207                                 object [] newArgs = new object [args.Length];
208                                 Array.Copy (args, newArgs, args.Length);
209                                 ParameterInfo [] plist = selected.GetParameters ();
210                                 for (int n = 0; n < names.Length; n++)
211                                         for (int p = 0; p < plist.Length; p++) {
212                                                 if (names [n] == plist [p].Name) {
213                                                         newArgs [p] = args [n];
214                                                         break;
215                                                 }
216                                         }
217                                 Array.Copy (newArgs, args, args.Length);
218                         }
219
220                         static bool IsArrayAssignable (Type object_type, Type target_type)
221                         {
222                                 if (object_type.IsArray && target_type.IsArray)
223                                         return IsArrayAssignable (object_type.GetElementType (), target_type.GetElementType ());
224                                                 
225                                 if (target_type.IsAssignableFrom (object_type))
226                                         return true;
227
228                                 return false;
229                         }
230                         
231                         public override object ChangeType (object value, Type type, CultureInfo culture)
232                         {
233                                 if (value == null)
234                                         return null;
235                                 Type vtype = value.GetType ();
236                                 if (type.IsByRef)
237                                         type = type.GetElementType ();
238                                 if (vtype == type || type.IsInstanceOfType (value))
239                                         return value;
240                                 if (vtype.IsArray && type.IsArray){
241                                         if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
242                                                 return value;
243                                 }
244
245                                 if (check_type (vtype, type)) {
246                                         // These are not supported by Convert
247                                         if (type.IsEnum)
248                                                 return Enum.ToObject (type, value);
249                                         if (vtype == typeof (Char)) {
250                                                 if (type == typeof (double))
251                                                         return (double)(char)value;
252                                                 if (type == typeof (float))
253                                                         return (float)(char)value;
254                                         }
255                                         if (vtype == typeof (IntPtr) && type.IsPointer)
256                                                 return value;
257                                         return Convert.ChangeType (value, type);
258                                 }
259                                 return null;
260                         }
261
262                         [MonoTODO ("This method does not do anything in Mono")]
263                         public override void ReorderArgumentArray (ref object[] args, object state)
264                         {
265                                 //do nothing until we support named arguments
266                                 //throw new NotImplementedException ();
267                         }
268
269                         private static bool check_type (Type from, Type to) {
270                                 if (from == to)
271                                         return true;
272
273                                 if (from == null)
274                                         return true;
275
276                                 if (to.IsByRef != from.IsByRef)
277                                         return false;
278
279                                 if (to.IsInterface)
280                                         return to.IsAssignableFrom (from);
281
282                                 if (to.IsEnum) {
283                                         to = Enum.GetUnderlyingType (to);
284                                         if (from == to)
285                                                 return true;
286                                 }
287
288                                 if (to.IsGenericType && to.GetGenericTypeDefinition () == typeof (Nullable<>) && to.GetGenericArguments ()[0] == from)
289                                         return true;
290
291                                 TypeCode fromt = Type.GetTypeCode (from);
292                                 TypeCode tot = Type.GetTypeCode (to);
293
294                                 switch (fromt) {
295                                 case TypeCode.Char:
296                                         switch (tot) {
297                                         case TypeCode.UInt16:
298                                         case TypeCode.UInt32:
299                                         case TypeCode.Int32:
300                                         case TypeCode.UInt64:
301                                         case TypeCode.Int64:
302                                         case TypeCode.Single:
303                                         case TypeCode.Double:
304                                                 return true;
305                                         }
306                                         return to == typeof (object);
307                                 case TypeCode.Byte:
308                                         switch (tot) {
309                                         case TypeCode.Char:
310                                         case TypeCode.UInt16:
311                                         case TypeCode.Int16:
312                                         case TypeCode.UInt32:
313                                         case TypeCode.Int32:
314                                         case TypeCode.UInt64:
315                                         case TypeCode.Int64:
316                                         case TypeCode.Single:
317                                         case TypeCode.Double:
318                                                 return true;
319                                         }
320                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
321                                 case TypeCode.SByte:
322                                         switch (tot) {
323                                         case TypeCode.Int16:
324                                         case TypeCode.Int32:
325                                         case TypeCode.Int64:
326                                         case TypeCode.Single:
327                                         case TypeCode.Double:
328                                                 return true;
329                                         }
330                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
331                                 case TypeCode.UInt16:
332                                         switch (tot) {
333                                         case TypeCode.UInt32:
334                                         case TypeCode.Int32:
335                                         case TypeCode.UInt64:
336                                         case TypeCode.Int64:
337                                         case TypeCode.Single:
338                                         case TypeCode.Double:
339                                                 return true;
340                                         }
341                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
342                                 case TypeCode.Int16:
343                                         switch (tot) {
344                                         case TypeCode.Int32:
345                                         case TypeCode.Int64:
346                                         case TypeCode.Single:
347                                         case TypeCode.Double:
348                                                 return true;
349                                         }
350                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
351                                 case TypeCode.UInt32:
352                                         switch (tot) {
353                                         case TypeCode.UInt64:
354                                         case TypeCode.Int64:
355                                         case TypeCode.Single:
356                                         case TypeCode.Double:
357                                                 return true;
358                                         }
359                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
360                                 case TypeCode.Int32:
361                                         switch (tot) {
362                                         case TypeCode.Int64:
363                                         case TypeCode.Single:
364                                         case TypeCode.Double:
365                                                 return true;
366                                         }
367                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
368                                 case TypeCode.UInt64:
369                                 case TypeCode.Int64:
370                                         switch (tot) {
371                                         case TypeCode.Single:
372                                         case TypeCode.Double:
373                                                 return true;
374                                         }
375                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
376                                 case TypeCode.Single:
377                                         return tot == TypeCode.Double || to == typeof (object);
378                                 default:
379                                         /* TODO: handle valuetype -> byref */
380                                         if (to == typeof (object) && from.IsValueType)
381                                                 return true;
382                                         if (to.IsPointer && from == typeof (IntPtr))
383                                                 return true;
384
385                                         return to.IsAssignableFrom (from);
386                                 }
387                         }
388
389                         private static bool check_arguments (Type[] types, ParameterInfo[] args, bool allowByRefMatch) {
390                                 for (int i = 0; i < types.Length; ++i) {
391                                         bool match = check_type (types [i], args [i].ParameterType);
392                                         if (!match && allowByRefMatch) {
393                                                 Type param_type = args [i].ParameterType;
394                                                 if (param_type.IsByRef)
395                                                         match = check_type (types [i], param_type.GetElementType ());
396                                         }
397                                         if (!match)
398                                                 return false;
399                                 }
400                                 return true;
401                         }
402
403                         public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase [] match, Type [] types, ParameterModifier [] modifiers)
404                         {
405                                 return SelectMethod (bindingAttr, match, types, modifiers,
406                                         false);
407                         }
408
409                         MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers, bool allowByRefMatch)
410                         {
411                                 MethodBase m;
412                                 int i, j;
413
414                                 if (match == null)
415                                         throw new ArgumentNullException ("match");
416
417                                 /* first look for an exact match... */
418                                 MethodBase exact_match = null;
419                                 for (i = 0; i < match.Length; ++i) {
420                                         m = match [i];
421                                         ParameterInfo[] args = m.GetParameters ();
422                                         if (args.Length != types.Length)
423                                                 continue;
424                                         for (j = 0; j < types.Length; ++j) {
425                                                 if (types [j] != args [j].ParameterType)
426                                                         break;
427                                         }
428                                         if (j == types.Length) {
429                                                 if (exact_match != null) {
430                                                         exact_match = null;
431                                                         break;
432                                                 } else {
433                                                         exact_match = m;
434                                                 }
435                                         }
436                                 }
437                                 if (exact_match != null)
438                                         return exact_match;
439
440                                 /* Try methods with ParamArray attribute */
441                                 bool isdefParamArray = false;
442                                 Type elementType = null;
443                                 for (i = 0; i < match.Length; ++i) {
444                                         m = match [i];
445                                         ParameterInfo[] args = m.GetParameters ();
446                                         if (args.Length > types.Length + 1)
447                                                 continue;
448                                         else if (args.Length == 0)
449                                                 continue;
450                                         isdefParamArray = Attribute.IsDefined (args [args.Length - 1], typeof (ParamArrayAttribute));
451                                         if (!isdefParamArray)
452                                                 continue;
453                                         elementType = args [args.Length - 1].ParameterType.GetElementType ();
454                                         for (j = 0; j < types.Length; ++j) {
455                                                 if (j < (args.Length - 1) && types [j] != args [j].ParameterType)
456                                                         break;
457                                                 else if (j >= (args.Length - 1) && types [j] != elementType) 
458                                                         break;
459                                         }
460                                         if (j == types.Length)
461                                                 return m;
462                                 }
463
464                                 if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
465                                         return null;
466
467                                 MethodBase result = null;
468                                 for (i = 0; i < match.Length; ++i) {
469                                         m = match [i];
470                                         ParameterInfo[] args = m.GetParameters ();
471                                         if (args.Length != types.Length)
472                                                 continue;
473                                         if (!check_arguments (types, args, allowByRefMatch))
474                                                 continue;
475
476                                         if (result != null)
477                                                 result = GetBetterMethod (result, m, types);
478                                         else
479                                                 result = m;
480                                 }
481
482                                 return result;
483                         }
484
485                         MethodBase GetBetterMethod (MethodBase m1, MethodBase m2, Type [] types)
486                         {
487                                 ParameterInfo [] pl1 = m1.GetParameters ();
488                                 ParameterInfo [] pl2 = m2.GetParameters ();
489                                 int prev = 0;
490                                 for (int i = 0; i < pl1.Length; i++) {
491                                         int cmp = CompareCloserType (pl1 [i].ParameterType, pl2 [i].ParameterType);
492                                         if (cmp != 0 && prev != 0 && prev != cmp)
493                                                 throw new AmbiguousMatchException ();
494                                         if (cmp != 0)
495                                                 prev = cmp;
496                                 }
497                                 if (prev != 0)
498                                         return prev > 0 ? m2 : m1;
499
500                                 Type dt1 = m1.DeclaringType;
501                                 Type dt2 = m2.DeclaringType;
502                                 if (dt1 != dt2) {
503                                         if (dt1.IsSubclassOf(dt2))
504                                                 return m1;
505                                         if (dt2.IsSubclassOf(dt1))
506                                                 return m2;
507                                 }
508
509                                 bool va1 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
510                                 bool va2 = (m2.CallingConvention & CallingConventions.VarArgs) != 0;
511                                 if (va1 && !va2)
512                                         return m2;
513                                 if (va2 && !va1)
514                                         return m1;
515
516                                 throw new AmbiguousMatchException ();
517                         }
518
519                         int CompareCloserType (Type t1, Type t2)
520                         {
521                                 if (t1 == t2)
522                                         return 0;
523                                 if (t1.IsGenericParameter && !t2.IsGenericParameter)
524                                         return 1; // t2
525                                 if (!t1.IsGenericParameter && t2.IsGenericParameter)
526                                         return -1; // t1
527                                 if (t1.HasElementType && t2.HasElementType)
528                                         return CompareCloserType (
529                                                 t1.GetElementType (),
530                                                 t2.GetElementType ());
531
532                                 if (t1.IsSubclassOf (t2))
533                                         return -1; // t1
534                                 if (t2.IsSubclassOf (t1))
535                                         return 1; // t2
536
537                                 if (t1.IsInterface && Array.IndexOf (t2.GetInterfaces (), t1) >= 0)
538                                         return 1; // t2
539                                 if (t2.IsInterface && Array.IndexOf (t1.GetInterfaces (), t2) >= 0)
540                                         return -1; // t1
541
542                                 // What kind of cases could reach here?
543                                 return 0;
544                         }
545
546                         public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
547                         {
548                                 if (match == null || match.Length == 0)
549                                         throw new ArgumentException ("No properties provided", "match");
550
551                                 bool haveRet = (returnType != null);
552                                 int idxlen = (indexes != null) ? indexes.Length : -1;
553                                 PropertyInfo result = null;
554                                 int i;
555                                 int best_score = Int32.MaxValue - 1;
556                                 int fail_score = Int32.MaxValue;
557                                 int level = 0;
558                                 
559                                 for (i = match.Length - 1; i >= 0; i--) {
560                                         PropertyInfo p = match [i];
561                                         ParameterInfo[] args = p.GetIndexParameters ();
562                                         if (idxlen >= 0 && idxlen != args.Length)
563                                                 continue;
564
565                                         if (haveRet && p.PropertyType != returnType)
566                                                 continue;
567
568                                         int score = Int32.MaxValue - 1;
569                                         if (idxlen > 0) {
570                                                 score = check_arguments_with_score (indexes, args);
571                                                 if (score == -1)
572                                                         continue;
573                                         }
574
575                                         int new_level = GetDerivedLevel (p.DeclaringType);
576                                         if (result != null) {
577                                                 if (best_score < score)
578                                                         continue;
579
580                                                 if (best_score == score) {
581                                                         if (level == new_level) {
582                                                                 // Keep searching. May be there's something
583                                                                 // better for us.
584                                                                 fail_score = score;
585                                                                 continue;
586                                                         }
587
588                                                         if (level > new_level)
589                                                                 continue;
590                                                 }
591                                         }
592
593                                         result = p;
594                                         best_score = score;
595                                         level = new_level;
596                                 }
597
598                                 if (fail_score <= best_score)
599                                         throw new AmbiguousMatchException ();
600
601                                 return result;
602                         }
603
604                         static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
605                         {
606                                 int worst = -1;
607
608                                 for (int i = 0; i < types.Length; ++i) {
609                                         int res = check_type_with_score (types [i], args [i].ParameterType);
610                                         if (res == -1)
611                                                 return -1;
612
613                                         if (worst < res)
614                                                 worst = res;
615                                 }
616
617                                 return worst;
618                         }
619
620                         // 0 -> same type or null and !valuetype
621                         // 1 -> to == Enum
622                         // 2 -> value type that don't lose data
623                         // 3 -> to == IsAssignableFrom
624                         // 4 -> to == object
625                         static int check_type_with_score (Type from, Type to)
626                         {
627                                 if (from == null)
628                                         return to.IsValueType ? -1 : 0;
629
630                                 if (from == to)
631                                         return 0;
632
633                                 if (to == typeof (object))
634                                         return 4;
635
636                                 TypeCode fromt = Type.GetTypeCode (from);
637                                 TypeCode tot = Type.GetTypeCode (to);
638
639                                 switch (fromt) {
640                                 case TypeCode.Char:
641                                         switch (tot) {
642                                         case TypeCode.UInt16:
643                                                 return 0;
644
645                                         case TypeCode.UInt32:
646                                         case TypeCode.Int32:
647                                         case TypeCode.UInt64:
648                                         case TypeCode.Int64:
649                                         case TypeCode.Single:
650                                         case TypeCode.Double:
651                                                 return 2;
652                                         }
653                                         return -1;
654                                 case TypeCode.Byte:
655                                         switch (tot) {
656                                         case TypeCode.Char:
657                                         case TypeCode.UInt16:
658                                         case TypeCode.Int16:
659                                         case TypeCode.UInt32:
660                                         case TypeCode.Int32:
661                                         case TypeCode.UInt64:
662                                         case TypeCode.Int64:
663                                         case TypeCode.Single:
664                                         case TypeCode.Double:
665                                                 return 2;
666                                         }
667                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
668                                 case TypeCode.SByte:
669                                         switch (tot) {
670                                         case TypeCode.Int16:
671                                         case TypeCode.Int32:
672                                         case TypeCode.Int64:
673                                         case TypeCode.Single:
674                                         case TypeCode.Double:
675                                                 return 2;
676                                         }
677                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
678                                 case TypeCode.UInt16:
679                                         switch (tot) {
680                                         case TypeCode.UInt32:
681                                         case TypeCode.Int32:
682                                         case TypeCode.UInt64:
683                                         case TypeCode.Int64:
684                                         case TypeCode.Single:
685                                         case TypeCode.Double:
686                                                 return 2;
687                                         }
688                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
689                                 case TypeCode.Int16:
690                                         switch (tot) {
691                                         case TypeCode.Int32:
692                                         case TypeCode.Int64:
693                                         case TypeCode.Single:
694                                         case TypeCode.Double:
695                                                 return 2;
696                                         }
697                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
698                                 case TypeCode.UInt32:
699                                         switch (tot) {
700                                         case TypeCode.UInt64:
701                                         case TypeCode.Int64:
702                                         case TypeCode.Single:
703                                         case TypeCode.Double:
704                                                 return 2;
705                                         }
706                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
707                                 case TypeCode.Int32:
708                                         switch (tot) {
709                                         case TypeCode.Int64:
710                                         case TypeCode.Single:
711                                         case TypeCode.Double:
712                                                 return 2;
713                                         }
714                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
715                                 case TypeCode.UInt64:
716                                 case TypeCode.Int64:
717                                         switch (tot) {
718                                         case TypeCode.Single:
719                                         case TypeCode.Double:
720                                                 return 2;
721                                         }
722                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
723                                 case TypeCode.Single:
724                                         return tot == TypeCode.Double ? 2 : -1;
725                                 default:
726                                         return (to.IsAssignableFrom (from)) ? 3 : -1;
727                                 }
728                         }
729                 }
730         }
731 }
732