2009-03-11 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 #if NET_2_0
40         [ComVisible (true)]
41 #endif
42         [Serializable]
43         [ClassInterface(ClassInterfaceType.AutoDual)]
44         public abstract class Binder
45         {
46                 protected Binder () {}
47
48                 public abstract FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture);
49                 public abstract MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state);
50                 public abstract object ChangeType (object value, Type type, CultureInfo culture);
51                 public abstract void ReorderArgumentArray( ref object[] args, object state);
52                 public abstract MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
53                 public abstract PropertyInfo SelectProperty( BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
54
55                 static Binder default_binder = new Default ();
56
57                 internal static Binder DefaultBinder {
58                         get {
59                                 return default_binder;
60                         }
61                 }
62                 
63                 internal static bool ConvertArgs (Binder binder, object[] args, ParameterInfo[] pinfo, CultureInfo culture) {
64                         if (args == null) {
65                                 if ( pinfo.Length == 0)
66                                         return true;
67                                 else
68                                         throw new TargetParameterCountException ();
69                         }
70                         if (pinfo.Length != args.Length)
71                                 throw new TargetParameterCountException ();
72                         for (int i = 0; i < args.Length; ++i) {
73                                 object v = binder.ChangeType (args [i], pinfo[i].ParameterType, culture);
74                                 if ((v == null) && (args [i] != null))
75                                         return false;
76                                 args [i] = v;
77                         }
78                         return true;
79                 }
80
81                 internal static int GetDerivedLevel (Type type) 
82                 {
83                         Type searchType = type;
84                         int level = 1;
85
86                         while (searchType.BaseType != null) 
87                         {
88                                 level++;
89                                 searchType = searchType.BaseType;
90                         }
91
92                         return level;
93                 }
94
95                 internal static MethodBase FindMostDerivedMatch (MethodBase [] match) 
96                 {
97                         int highLevel = 0;
98                         int matchId = -1;
99                         int count = match.Length;
100
101                         for (int current = 0; current < count; current++) 
102                         {
103                                 MethodBase m = match [current];
104                                 int level = GetDerivedLevel (m.DeclaringType);
105                                 if (level == highLevel)
106                                         throw new AmbiguousMatchException ();
107                                 // If the argument types differ we
108                                 // have an ambigous match, as well
109                                 if (matchId >= 0) {
110                                         ParameterInfo[] p1 = m.GetParameters ();
111                                         ParameterInfo[] p2 = match [matchId].GetParameters ();
112                                         bool equal = true;
113
114                                         if (p1.Length != p2.Length)
115                                                 equal = false;
116                                         else {
117                                                 int i;
118
119                                                 for (i = 0; i < p1.Length; ++i) {
120                                                         if (p1 [i].ParameterType != p2 [i].ParameterType) {
121                                                                 equal = false;
122                                                                 break;
123                                                         }
124                                                 }
125                                         }
126
127                                         if (!equal)
128                                                 throw new AmbiguousMatchException ();
129                                 }
130
131                                 if (level > highLevel) 
132                                 {
133                                         highLevel = level;
134                                         matchId = current;
135                                 }
136                         }
137
138                         return match[matchId];
139                 }
140
141                 internal sealed class Default : Binder {
142                         public override FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture) 
143                         {
144                                 if (match == null)
145                                         throw new ArgumentNullException ("match");
146                                 foreach (FieldInfo f in match) {
147                                         if (check_type (value.GetType (), f.FieldType))
148                                                 return f;
149                                 }
150                                 return null;
151                         }
152
153                         //
154                         // FIXME: There was a MonoTODO, but it does not explain what the problem is
155                         // 
156                         public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
157                         {
158                                 Type[] types;
159                                 if (args == null)
160                                         types = Type.EmptyTypes;
161                                 else {
162                                         types = new Type [args.Length];
163                                         for (int i = 0; i < args.Length; ++i) {
164                                                 if (args [i] != null)
165                                                         types [i] = args [i].GetType ();
166                                         }
167                                 }
168                                 MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers);
169                                 state = null;
170                                 if (names != null)
171                                         ReorderParameters (names, ref args, selected);
172                                 return selected;
173                         }
174
175                         void ReorderParameters (string [] names, ref object [] args, MethodBase selected)
176                         {
177                                 object [] newArgs = new object [args.Length];
178                                 Array.Copy (args, newArgs, args.Length);
179                                 ParameterInfo [] plist = selected.GetParameters ();
180                                 for (int n = 0; n < names.Length; n++)
181                                         for (int p = 0; p < plist.Length; p++) {
182                                                 if (names [n] == plist [p].Name) {
183                                                         newArgs [p] = args [n];
184                                                         break;
185                                                 }
186                                         }
187                                 Array.Copy (newArgs, args, args.Length);
188                         }
189
190                         static bool IsArrayAssignable (Type object_type, Type target_type)
191                         {
192                                 if (object_type.IsArray && target_type.IsArray)
193                                         return IsArrayAssignable (object_type.GetElementType (), target_type.GetElementType ());
194                                                 
195                                 if (target_type.IsAssignableFrom (object_type))
196                                         return true;
197
198                                 return false;
199                         }
200                         
201                         public override object ChangeType (object value, Type type, CultureInfo culture)
202                         {
203                                 if (value == null)
204                                         return null;
205                                 Type vtype = value.GetType ();
206                                 if (type.IsByRef)
207                                         type = type.GetElementType ();
208                                 if (vtype == type || type.IsInstanceOfType (value))
209                                         return value;
210                                 if (vtype.IsArray && type.IsArray){
211                                         if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
212                                                 return value;
213                                 }
214
215                                 if (check_type (vtype, type)) {
216                                         // These are not supported by Convert
217                                         if (type.IsEnum)
218                                                 return Enum.ToObject (type, value);
219                                         if (vtype == typeof (Char)) {
220                                                 if (type == typeof (double))
221                                                         return (double)(char)value;
222                                                 if (type == typeof (float))
223                                                         return (float)(char)value;
224                                         }
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 NET_2_0
257                                 if (to.IsGenericType && to.GetGenericTypeDefinition () == typeof (Nullable<>) && to.GetGenericArguments ()[0] == from)
258                                         return true;
259 #endif
260
261                                 TypeCode fromt = Type.GetTypeCode (from);
262                                 TypeCode tot = Type.GetTypeCode (to);
263
264                                 switch (fromt) {
265                                 case TypeCode.Char:
266                                         switch (tot) {
267                                         case TypeCode.UInt16:
268                                         case TypeCode.UInt32:
269                                         case TypeCode.Int32:
270                                         case TypeCode.UInt64:
271                                         case TypeCode.Int64:
272                                         case TypeCode.Single:
273                                         case TypeCode.Double:
274                                                 return true;
275                                         }
276                                         return to == typeof (object);
277                                 case TypeCode.Byte:
278                                         switch (tot) {
279                                         case TypeCode.Char:
280                                         case TypeCode.UInt16:
281                                         case TypeCode.Int16:
282                                         case TypeCode.UInt32:
283                                         case TypeCode.Int32:
284                                         case TypeCode.UInt64:
285                                         case TypeCode.Int64:
286                                         case TypeCode.Single:
287                                         case TypeCode.Double:
288                                                 return true;
289                                         }
290                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
291                                 case TypeCode.SByte:
292                                         switch (tot) {
293                                         case TypeCode.Int16:
294                                         case TypeCode.Int32:
295                                         case TypeCode.Int64:
296                                         case TypeCode.Single:
297                                         case TypeCode.Double:
298                                                 return true;
299                                         }
300                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
301                                 case TypeCode.UInt16:
302                                         switch (tot) {
303                                         case TypeCode.UInt32:
304                                         case TypeCode.Int32:
305                                         case TypeCode.UInt64:
306                                         case TypeCode.Int64:
307                                         case TypeCode.Single:
308                                         case TypeCode.Double:
309                                                 return true;
310                                         }
311                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
312                                 case TypeCode.Int16:
313                                         switch (tot) {
314                                         case TypeCode.Int32:
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.UInt32:
322                                         switch (tot) {
323                                         case TypeCode.UInt64:
324                                         case TypeCode.Int64:
325                                         case TypeCode.Single:
326                                         case TypeCode.Double:
327                                                 return true;
328                                         }
329                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
330                                 case TypeCode.Int32:
331                                         switch (tot) {
332                                         case TypeCode.Int64:
333                                         case TypeCode.Single:
334                                         case TypeCode.Double:
335                                                 return true;
336                                         }
337                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
338                                 case TypeCode.UInt64:
339                                 case TypeCode.Int64:
340                                         switch (tot) {
341                                         case TypeCode.Single:
342                                         case TypeCode.Double:
343                                                 return true;
344                                         }
345                                         return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
346                                 case TypeCode.Single:
347                                         return tot == TypeCode.Double || to == typeof (object);
348                                 default:
349                                         /* TODO: handle valuetype -> byref */
350                                         if (to == typeof (object) && from.IsValueType)
351                                                 return true;
352
353                                         return to.IsAssignableFrom (from);
354                                 }
355                         }
356
357                         private static bool check_arguments (Type[] types, ParameterInfo[] args) {
358                                 for (int i = 0; i < types.Length; ++i) {
359                                         if (!check_type (types [i], args [i].ParameterType))
360                                                 return false;
361                                 }
362                                 return true;
363                         }
364
365                         public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
366                         {
367                                 MethodBase m;
368                                 int i, j;
369
370                                 if (match == null)
371                                         throw new ArgumentNullException ("match");
372
373                                 /* first look for an exact match... */
374                                 for (i = 0; i < match.Length; ++i) {
375                                         m = match [i];
376                                         ParameterInfo[] args = m.GetParameters ();
377                                         if (args.Length != types.Length)
378                                                 continue;
379                                         for (j = 0; j < types.Length; ++j) {
380                                                 if (types [j] != args [j].ParameterType)
381                                                         break;
382                                         }
383                                         if (j == types.Length)
384                                                 return m;
385                                 }
386
387                                 /* Try methods with ParamArray attribute */
388                                 bool isdefParamArray = false;
389                                 Type elementType = null;
390                                 for (i = 0; i < match.Length; ++i) {
391                                         m = match [i];
392                                         ParameterInfo[] args = m.GetParameters ();
393                                         if (args.Length > types.Length)
394                                                 continue;
395                                         else if (args.Length == 0)
396                                                 continue;
397                                         isdefParamArray = Attribute.IsDefined (args [args.Length - 1], typeof (ParamArrayAttribute));
398                                         if (!isdefParamArray)
399                                                 continue;
400                                         elementType = args [args.Length - 1].ParameterType.GetElementType ();
401                                         for (j = 0; j < types.Length; ++j) {
402                                                 if (j < (args.Length - 1) && types [j] != args [j].ParameterType)
403                                                         break;
404                                                 else if (j >= (args.Length - 1) && types [j] != elementType) 
405                                                         break;
406                                         }
407                                         if (j == types.Length)
408                                                 return m;
409                                 }
410
411                                 if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
412                                         return null;
413
414                                 MethodBase result = null;
415                                 for (i = 0; i < match.Length; ++i) {
416                                         m = match [i];
417                                         ParameterInfo[] args = m.GetParameters ();
418                                         if (args.Length != types.Length)
419                                                 continue;
420                                         if (!check_arguments (types, args))
421                                                 continue;
422
423                                         if (result != null)
424                                                 result = GetBetterMethod (result, m, types);
425                                         else
426                                                 result = m;
427                                 }
428
429                                 return result;
430                         }
431
432                         MethodBase GetBetterMethod (MethodBase m1, MethodBase m2, Type [] types)
433                         {
434 #if NET_2_0
435                                 if (m1.IsGenericMethodDefinition && 
436                                     !m2.IsGenericMethodDefinition)
437                                         return m2;
438                                 if (m2.IsGenericMethodDefinition && 
439                                     !m1.IsGenericMethodDefinition)
440                                         return m1;
441 #endif
442
443                                 ParameterInfo [] pl1 = m1.GetParameters ();
444                                 ParameterInfo [] pl2 = m2.GetParameters ();
445                                 int prev = 0;
446                                 for (int i = 0; i < pl1.Length; i++) {
447                                         int cmp = CompareCloserType (pl1 [i].ParameterType, pl2 [i].ParameterType);
448                                         if (cmp != 0 && prev != 0 && prev != cmp)
449                                                 throw new AmbiguousMatchException ();
450                                         if (cmp != 0)
451                                                 prev = cmp;
452                                 }
453                                 if (prev != 0)
454                                         return prev > 0 ? m2 : m1;
455
456                                 Type dt1 = m1.DeclaringType;
457                                 Type dt2 = m2.DeclaringType;
458                                 if (dt1 != dt2) {
459                                         if (dt1.IsSubclassOf(dt2))
460                                                 return m1;
461                                         if (dt2.IsSubclassOf(dt1))
462                                                 return m2;
463                                 }
464
465                                 bool va1 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
466                                 bool va2 = (m2.CallingConvention & CallingConventions.VarArgs) != 0;
467                                 if (va1 && !va2)
468                                         return m2;
469                                 if (va2 && !va1)
470                                         return m1;
471
472                                 throw new AmbiguousMatchException ();
473                         }
474
475                         int CompareCloserType (Type t1, Type t2)
476                         {
477                                 if (t1 == t2)
478                                         return 0;
479 #if NET_2_0
480                                 if (t1.IsGenericParameter && !t2.IsGenericParameter)
481                                         return 1; // t2
482                                 if (!t1.IsGenericParameter && t2.IsGenericParameter)
483                                         return -1; // t1
484 #endif
485                                 if (t1.HasElementType && t2.HasElementType)
486                                         return CompareCloserType (
487                                                 t1.GetElementType (),
488                                                 t2.GetElementType ());
489
490                                 if (t1.IsSubclassOf (t2))
491                                         return -1; // t1
492                                 if (t2.IsSubclassOf (t1))
493                                         return 1; // t2
494
495                                 if (t1.IsInterface && Array.IndexOf (t2.GetInterfaces (), t1) >= 0)
496                                         return 1; // t2
497                                 if (t2.IsInterface && Array.IndexOf (t1.GetInterfaces (), t2) >= 0)
498                                         return -1; // t1
499
500                                 // What kind of cases could reach here?
501                                 return 0;
502                         }
503
504                         public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
505                         {
506                                 if (match == null || match.Length == 0)
507                                         throw new ArgumentException ("No properties provided", "match");
508
509                                 bool haveRet = (returnType != null);
510                                 int idxlen = (indexes != null) ? indexes.Length : -1;
511                                 PropertyInfo result = null;
512                                 int i;
513                                 int best_score = Int32.MaxValue - 1;
514                                 int fail_score = Int32.MaxValue;
515                                 int level = 0;
516                                 
517                                 for (i = match.Length - 1; i >= 0; i--) {
518                                         PropertyInfo p = match [i];
519                                         ParameterInfo[] args = p.GetIndexParameters ();
520                                         if (idxlen >= 0 && idxlen != args.Length)
521                                                 continue;
522
523                                         if (haveRet && p.PropertyType != returnType)
524                                                 continue;
525
526                                         int score = Int32.MaxValue - 1;
527                                         if (idxlen > 0) {
528                                                 score = check_arguments_with_score (indexes, args);
529                                                 if (score == -1)
530                                                         continue;
531                                         }
532
533                                         int new_level = GetDerivedLevel (p.DeclaringType);
534                                         if (result != null) {
535                                                 if (best_score < score)
536                                                         continue;
537
538                                                 if (best_score == score) {
539                                                         if (level == new_level) {
540                                                                 // Keep searching. May be there's something
541                                                                 // better for us.
542                                                                 fail_score = score;
543                                                                 continue;
544                                                         }
545
546                                                         if (level > new_level)
547                                                                 continue;
548                                                 }
549                                         }
550
551                                         result = p;
552                                         best_score = score;
553                                         level = new_level;
554                                 }
555
556                                 if (fail_score <= best_score)
557                                         throw new AmbiguousMatchException ();
558
559                                 return result;
560                         }
561
562                         static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
563                         {
564                                 int worst = -1;
565
566                                 for (int i = 0; i < types.Length; ++i) {
567                                         int res = check_type_with_score (types [i], args [i].ParameterType);
568                                         if (res == -1)
569                                                 return -1;
570
571                                         if (worst < res)
572                                                 worst = res;
573                                 }
574
575                                 return worst;
576                         }
577
578                         // 0 -> same type or null and !valuetype
579                         // 1 -> to == Enum
580                         // 2 -> value type that don't lose data
581                         // 3 -> to == IsAssignableFrom
582                         // 4 -> to == object
583                         static int check_type_with_score (Type from, Type to)
584                         {
585                                 if (from == null)
586                                         return to.IsValueType ? -1 : 0;
587
588                                 if (from == to)
589                                         return 0;
590
591                                 if (to == typeof (object))
592                                         return 4;
593
594                                 TypeCode fromt = Type.GetTypeCode (from);
595                                 TypeCode tot = Type.GetTypeCode (to);
596
597                                 switch (fromt) {
598                                 case TypeCode.Char:
599                                         switch (tot) {
600                                         case TypeCode.UInt16:
601                                                 return 0;
602
603                                         case TypeCode.UInt32:
604                                         case TypeCode.Int32:
605                                         case TypeCode.UInt64:
606                                         case TypeCode.Int64:
607                                         case TypeCode.Single:
608                                         case TypeCode.Double:
609                                                 return 2;
610                                         }
611                                         return -1;
612                                 case TypeCode.Byte:
613                                         switch (tot) {
614                                         case TypeCode.Char:
615                                         case TypeCode.UInt16:
616                                         case TypeCode.Int16:
617                                         case TypeCode.UInt32:
618                                         case TypeCode.Int32:
619                                         case TypeCode.UInt64:
620                                         case TypeCode.Int64:
621                                         case TypeCode.Single:
622                                         case TypeCode.Double:
623                                                 return 2;
624                                         }
625                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
626                                 case TypeCode.SByte:
627                                         switch (tot) {
628                                         case TypeCode.Int16:
629                                         case TypeCode.Int32:
630                                         case TypeCode.Int64:
631                                         case TypeCode.Single:
632                                         case TypeCode.Double:
633                                                 return 2;
634                                         }
635                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
636                                 case TypeCode.UInt16:
637                                         switch (tot) {
638                                         case TypeCode.UInt32:
639                                         case TypeCode.Int32:
640                                         case TypeCode.UInt64:
641                                         case TypeCode.Int64:
642                                         case TypeCode.Single:
643                                         case TypeCode.Double:
644                                                 return 2;
645                                         }
646                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
647                                 case TypeCode.Int16:
648                                         switch (tot) {
649                                         case TypeCode.Int32:
650                                         case TypeCode.Int64:
651                                         case TypeCode.Single:
652                                         case TypeCode.Double:
653                                                 return 2;
654                                         }
655                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
656                                 case TypeCode.UInt32:
657                                         switch (tot) {
658                                         case TypeCode.UInt64:
659                                         case TypeCode.Int64:
660                                         case TypeCode.Single:
661                                         case TypeCode.Double:
662                                                 return 2;
663                                         }
664                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
665                                 case TypeCode.Int32:
666                                         switch (tot) {
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.UInt64:
674                                 case TypeCode.Int64:
675                                         switch (tot) {
676                                         case TypeCode.Single:
677                                         case TypeCode.Double:
678                                                 return 2;
679                                         }
680                                         return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
681                                 case TypeCode.Single:
682                                         return tot == TypeCode.Double ? 2 : -1;
683                                 default:
684                                         return (to.IsAssignableFrom (from)) ? 3 : -1;
685                                 }
686                         }
687                 }
688         }
689 }
690