Binder.cs 23 KB

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