Binder.cs 20 KB

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