Binder.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. for (i = 0; i < match.Length; ++i) {
  370. m = match [i];
  371. ParameterInfo[] args = m.GetParameters ();
  372. if (args.Length != types.Length)
  373. continue;
  374. for (j = 0; j < types.Length; ++j) {
  375. if (types [j] != args [j].ParameterType)
  376. break;
  377. }
  378. if (j == types.Length)
  379. return m;
  380. }
  381. /* Try methods with ParamArray attribute */
  382. bool isdefParamArray = false;
  383. Type elementType = null;
  384. for (i = 0; i < match.Length; ++i) {
  385. m = match [i];
  386. ParameterInfo[] args = m.GetParameters ();
  387. if (args.Length > types.Length + 1)
  388. continue;
  389. else if (args.Length == 0)
  390. continue;
  391. isdefParamArray = Attribute.IsDefined (args [args.Length - 1], typeof (ParamArrayAttribute));
  392. if (!isdefParamArray)
  393. continue;
  394. elementType = args [args.Length - 1].ParameterType.GetElementType ();
  395. for (j = 0; j < types.Length; ++j) {
  396. if (j < (args.Length - 1) && types [j] != args [j].ParameterType)
  397. break;
  398. else if (j >= (args.Length - 1) && types [j] != elementType)
  399. break;
  400. }
  401. if (j == types.Length)
  402. return m;
  403. }
  404. if ((int)(bindingAttr & BindingFlags.ExactBinding) != 0)
  405. return null;
  406. MethodBase result = null;
  407. for (i = 0; i < match.Length; ++i) {
  408. m = match [i];
  409. ParameterInfo[] args = m.GetParameters ();
  410. if (args.Length != types.Length)
  411. continue;
  412. if (!check_arguments (types, args, allowByRefMatch))
  413. continue;
  414. if (result != null)
  415. result = GetBetterMethod (result, m, types);
  416. else
  417. result = m;
  418. }
  419. return result;
  420. }
  421. MethodBase GetBetterMethod (MethodBase m1, MethodBase m2, Type [] types)
  422. {
  423. if (m1.IsGenericMethodDefinition &&
  424. !m2.IsGenericMethodDefinition)
  425. return m2;
  426. if (m2.IsGenericMethodDefinition &&
  427. !m1.IsGenericMethodDefinition)
  428. return m1;
  429. ParameterInfo [] pl1 = m1.GetParameters ();
  430. ParameterInfo [] pl2 = m2.GetParameters ();
  431. int prev = 0;
  432. for (int i = 0; i < pl1.Length; i++) {
  433. int cmp = CompareCloserType (pl1 [i].ParameterType, pl2 [i].ParameterType);
  434. if (cmp != 0 && prev != 0 && prev != cmp)
  435. throw new AmbiguousMatchException ();
  436. if (cmp != 0)
  437. prev = cmp;
  438. }
  439. if (prev != 0)
  440. return prev > 0 ? m2 : m1;
  441. Type dt1 = m1.DeclaringType;
  442. Type dt2 = m2.DeclaringType;
  443. if (dt1 != dt2) {
  444. if (dt1.IsSubclassOf(dt2))
  445. return m1;
  446. if (dt2.IsSubclassOf(dt1))
  447. return m2;
  448. }
  449. bool va1 = (m1.CallingConvention & CallingConventions.VarArgs) != 0;
  450. bool va2 = (m2.CallingConvention & CallingConventions.VarArgs) != 0;
  451. if (va1 && !va2)
  452. return m2;
  453. if (va2 && !va1)
  454. return m1;
  455. throw new AmbiguousMatchException ();
  456. }
  457. int CompareCloserType (Type t1, Type t2)
  458. {
  459. if (t1 == t2)
  460. return 0;
  461. if (t1.IsGenericParameter && !t2.IsGenericParameter)
  462. return 1; // t2
  463. if (!t1.IsGenericParameter && t2.IsGenericParameter)
  464. return -1; // t1
  465. if (t1.HasElementType && t2.HasElementType)
  466. return CompareCloserType (
  467. t1.GetElementType (),
  468. t2.GetElementType ());
  469. if (t1.IsSubclassOf (t2))
  470. return -1; // t1
  471. if (t2.IsSubclassOf (t1))
  472. return 1; // t2
  473. if (t1.IsInterface && Array.IndexOf (t2.GetInterfaces (), t1) >= 0)
  474. return 1; // t2
  475. if (t2.IsInterface && Array.IndexOf (t1.GetInterfaces (), t2) >= 0)
  476. return -1; // t1
  477. // What kind of cases could reach here?
  478. return 0;
  479. }
  480. public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
  481. {
  482. if (match == null || match.Length == 0)
  483. throw new ArgumentException ("No properties provided", "match");
  484. bool haveRet = (returnType != null);
  485. int idxlen = (indexes != null) ? indexes.Length : -1;
  486. PropertyInfo result = null;
  487. int i;
  488. int best_score = Int32.MaxValue - 1;
  489. int fail_score = Int32.MaxValue;
  490. int level = 0;
  491. for (i = match.Length - 1; i >= 0; i--) {
  492. PropertyInfo p = match [i];
  493. ParameterInfo[] args = p.GetIndexParameters ();
  494. if (idxlen >= 0 && idxlen != args.Length)
  495. continue;
  496. if (haveRet && p.PropertyType != returnType)
  497. continue;
  498. int score = Int32.MaxValue - 1;
  499. if (idxlen > 0) {
  500. score = check_arguments_with_score (indexes, args);
  501. if (score == -1)
  502. continue;
  503. }
  504. int new_level = GetDerivedLevel (p.DeclaringType);
  505. if (result != null) {
  506. if (best_score < score)
  507. continue;
  508. if (best_score == score) {
  509. if (level == new_level) {
  510. // Keep searching. May be there's something
  511. // better for us.
  512. fail_score = score;
  513. continue;
  514. }
  515. if (level > new_level)
  516. continue;
  517. }
  518. }
  519. result = p;
  520. best_score = score;
  521. level = new_level;
  522. }
  523. if (fail_score <= best_score)
  524. throw new AmbiguousMatchException ();
  525. return result;
  526. }
  527. static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
  528. {
  529. int worst = -1;
  530. for (int i = 0; i < types.Length; ++i) {
  531. int res = check_type_with_score (types [i], args [i].ParameterType);
  532. if (res == -1)
  533. return -1;
  534. if (worst < res)
  535. worst = res;
  536. }
  537. return worst;
  538. }
  539. // 0 -> same type or null and !valuetype
  540. // 1 -> to == Enum
  541. // 2 -> value type that don't lose data
  542. // 3 -> to == IsAssignableFrom
  543. // 4 -> to == object
  544. static int check_type_with_score (Type from, Type to)
  545. {
  546. if (from == null)
  547. return to.IsValueType ? -1 : 0;
  548. if (from == to)
  549. return 0;
  550. if (to == typeof (object))
  551. return 4;
  552. TypeCode fromt = Type.GetTypeCode (from);
  553. TypeCode tot = Type.GetTypeCode (to);
  554. switch (fromt) {
  555. case TypeCode.Char:
  556. switch (tot) {
  557. case TypeCode.UInt16:
  558. return 0;
  559. case TypeCode.UInt32:
  560. case TypeCode.Int32:
  561. case TypeCode.UInt64:
  562. case TypeCode.Int64:
  563. case TypeCode.Single:
  564. case TypeCode.Double:
  565. return 2;
  566. }
  567. return -1;
  568. case TypeCode.Byte:
  569. switch (tot) {
  570. case TypeCode.Char:
  571. case TypeCode.UInt16:
  572. case TypeCode.Int16:
  573. case TypeCode.UInt32:
  574. case TypeCode.Int32:
  575. case TypeCode.UInt64:
  576. case TypeCode.Int64:
  577. case TypeCode.Single:
  578. case TypeCode.Double:
  579. return 2;
  580. }
  581. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  582. case TypeCode.SByte:
  583. switch (tot) {
  584. case TypeCode.Int16:
  585. case TypeCode.Int32:
  586. case TypeCode.Int64:
  587. case TypeCode.Single:
  588. case TypeCode.Double:
  589. return 2;
  590. }
  591. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  592. case TypeCode.UInt16:
  593. switch (tot) {
  594. case TypeCode.UInt32:
  595. case TypeCode.Int32:
  596. case TypeCode.UInt64:
  597. case TypeCode.Int64:
  598. case TypeCode.Single:
  599. case TypeCode.Double:
  600. return 2;
  601. }
  602. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  603. case TypeCode.Int16:
  604. switch (tot) {
  605. case TypeCode.Int32:
  606. case TypeCode.Int64:
  607. case TypeCode.Single:
  608. case TypeCode.Double:
  609. return 2;
  610. }
  611. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  612. case TypeCode.UInt32:
  613. switch (tot) {
  614. case TypeCode.UInt64:
  615. case TypeCode.Int64:
  616. case TypeCode.Single:
  617. case TypeCode.Double:
  618. return 2;
  619. }
  620. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  621. case TypeCode.Int32:
  622. switch (tot) {
  623. case TypeCode.Int64:
  624. case TypeCode.Single:
  625. case TypeCode.Double:
  626. return 2;
  627. }
  628. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  629. case TypeCode.UInt64:
  630. case TypeCode.Int64:
  631. switch (tot) {
  632. case TypeCode.Single:
  633. case TypeCode.Double:
  634. return 2;
  635. }
  636. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  637. case TypeCode.Single:
  638. return tot == TypeCode.Double ? 2 : -1;
  639. default:
  640. return (to.IsAssignableFrom (from)) ? 3 : -1;
  641. }
  642. }
  643. }
  644. }
  645. }