Binder.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. [Serializable]
  37. [ClassInterface(ClassInterfaceType.AutoDual)]
  38. public abstract class Binder
  39. {
  40. protected Binder () {}
  41. public abstract FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture);
  42. public abstract MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state);
  43. public abstract object ChangeType (object value, Type type, CultureInfo culture);
  44. public abstract void ReorderArgumentArray( ref object[] args, object state);
  45. public abstract MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers);
  46. public abstract PropertyInfo SelectProperty( BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers);
  47. static Binder default_binder;
  48. internal static Binder DefaultBinder {
  49. get {
  50. if (null == default_binder)
  51. {
  52. lock (typeof (Binder))
  53. {
  54. if (default_binder == null)
  55. default_binder = new Default ();
  56. return default_binder;
  57. }
  58. }
  59. return default_binder;
  60. }
  61. }
  62. internal static bool ConvertArgs (Binder binder, object[] args, ParameterInfo[] pinfo, CultureInfo culture) {
  63. if (args == null) {
  64. if ( pinfo.Length == 0)
  65. return true;
  66. else
  67. throw new TargetParameterCountException ();
  68. }
  69. if (pinfo.Length != args.Length)
  70. throw new TargetParameterCountException ();
  71. for (int i = 0; i < args.Length; ++i) {
  72. object v = binder.ChangeType (args [i], pinfo[i].ParameterType, culture);
  73. if ((v == null) && (args [i] != null))
  74. return false;
  75. args [i] = v;
  76. }
  77. return true;
  78. }
  79. internal static int GetDerivedLevel (Type type)
  80. {
  81. Type searchType = type;
  82. int level = 1;
  83. while (searchType.BaseType != null)
  84. {
  85. level++;
  86. searchType = searchType.BaseType;
  87. }
  88. return level;
  89. }
  90. internal static MethodBase FindMostDerivedMatch (MethodBase [] match)
  91. {
  92. int highLevel = 0;
  93. int matchId = -1;
  94. int count = match.Length;
  95. for (int current = 0; current < count; current++)
  96. {
  97. int level = GetDerivedLevel (match[current].DeclaringType);
  98. if (level == highLevel)
  99. throw new AmbiguousMatchException ();
  100. if (level > highLevel)
  101. {
  102. highLevel = level;
  103. matchId = current;
  104. }
  105. }
  106. return match[matchId];
  107. }
  108. internal sealed class Default : Binder {
  109. public override FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture)
  110. {
  111. if (match == null)
  112. throw new ArgumentNullException ("match");
  113. foreach (FieldInfo f in match) {
  114. if (check_type (value.GetType (), f.FieldType))
  115. return f;
  116. }
  117. return null;
  118. }
  119. [MonoTODO]
  120. public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
  121. {
  122. Type[] types;
  123. if (args == null)
  124. types = Type.EmptyTypes;
  125. else {
  126. types = new Type [args.Length];
  127. for (int i = 0; i < args.Length; ++i) {
  128. if (args [i] != null)
  129. types [i] = args [i].GetType ();
  130. }
  131. }
  132. MethodBase selected = SelectMethod (bindingAttr, match, types, modifiers);
  133. state = null;
  134. return selected;
  135. }
  136. static bool IsArrayAssignable (Type object_type, Type target_type)
  137. {
  138. if (object_type.IsArray && target_type.IsArray)
  139. return IsArrayAssignable (object_type.GetElementType (), target_type.GetElementType ());
  140. if (target_type.IsAssignableFrom (object_type))
  141. return true;
  142. return false;
  143. }
  144. public override object ChangeType (object value, Type type, CultureInfo culture)
  145. {
  146. if (value == null)
  147. return null;
  148. Type vtype = value.GetType ();
  149. if (type.IsByRef)
  150. type = type.GetElementType ();
  151. if (vtype == type || type.IsAssignableFrom (vtype))
  152. return value;
  153. if (vtype.IsArray && type.IsArray){
  154. if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
  155. return value;
  156. }
  157. if (check_type (vtype, type))
  158. return Convert.ChangeType (value, type);
  159. return null;
  160. }
  161. [MonoTODO]
  162. public override void ReorderArgumentArray (ref object[] args, object state)
  163. {
  164. //do nothing until we support named arguments
  165. //throw new NotImplementedException ();
  166. }
  167. private static bool check_type (Type from, Type to) {
  168. if (from == to)
  169. return true;
  170. if (from == null)
  171. return !to.IsValueType;
  172. TypeCode fromt = Type.GetTypeCode (from);
  173. TypeCode tot = Type.GetTypeCode (to);
  174. if (to.IsByRef && !from.IsByRef)
  175. return check_type (from, to.GetElementType ());
  176. switch (fromt) {
  177. case TypeCode.Char:
  178. switch (tot) {
  179. case TypeCode.UInt16:
  180. case TypeCode.UInt32:
  181. case TypeCode.Int32:
  182. case TypeCode.UInt64:
  183. case TypeCode.Int64:
  184. case TypeCode.Single:
  185. case TypeCode.Double:
  186. return true;
  187. }
  188. return to == typeof (object);
  189. case TypeCode.Byte:
  190. switch (tot) {
  191. case TypeCode.Char:
  192. case TypeCode.UInt16:
  193. case TypeCode.Int16:
  194. case TypeCode.UInt32:
  195. case TypeCode.Int32:
  196. case TypeCode.UInt64:
  197. case TypeCode.Int64:
  198. case TypeCode.Single:
  199. case TypeCode.Double:
  200. return true;
  201. }
  202. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  203. case TypeCode.SByte:
  204. switch (tot) {
  205. case TypeCode.Int16:
  206. case TypeCode.Int32:
  207. case TypeCode.Int64:
  208. case TypeCode.Single:
  209. case TypeCode.Double:
  210. return true;
  211. }
  212. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  213. case TypeCode.UInt16:
  214. switch (tot) {
  215. case TypeCode.UInt32:
  216. case TypeCode.Int32:
  217. case TypeCode.UInt64:
  218. case TypeCode.Int64:
  219. case TypeCode.Single:
  220. case TypeCode.Double:
  221. return true;
  222. }
  223. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  224. case TypeCode.Int16:
  225. switch (tot) {
  226. case TypeCode.Int32:
  227. case TypeCode.Int64:
  228. case TypeCode.Single:
  229. case TypeCode.Double:
  230. return true;
  231. }
  232. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  233. case TypeCode.UInt32:
  234. switch (tot) {
  235. case TypeCode.UInt64:
  236. case TypeCode.Int64:
  237. case TypeCode.Single:
  238. case TypeCode.Double:
  239. return true;
  240. }
  241. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  242. case TypeCode.Int32:
  243. switch (tot) {
  244. case TypeCode.Int64:
  245. case TypeCode.Single:
  246. case TypeCode.Double:
  247. return true;
  248. }
  249. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  250. case TypeCode.UInt64:
  251. case TypeCode.Int64:
  252. switch (tot) {
  253. case TypeCode.Single:
  254. case TypeCode.Double:
  255. return true;
  256. }
  257. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  258. case TypeCode.Single:
  259. return tot == TypeCode.Double || to == typeof (object);
  260. default:
  261. /* TODO: handle valuetype -> byref */
  262. if (to == typeof (object) && from.IsValueType)
  263. return true;
  264. return to.IsAssignableFrom (from);
  265. }
  266. }
  267. private static bool check_arguments (Type[] types, ParameterInfo[] args) {
  268. for (int i = 0; i < types.Length; ++i) {
  269. if (!check_type (types [i], args [i].ParameterType))
  270. return false;
  271. }
  272. return true;
  273. }
  274. public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
  275. {
  276. MethodBase m;
  277. int i, j;
  278. if (match == null)
  279. throw new ArgumentNullException ("match");
  280. /* first look for an exact match... */
  281. for (i = 0; i < match.Length; ++i) {
  282. m = match [i];
  283. ParameterInfo[] args = m.GetParameters ();
  284. if (args.Length != types.Length)
  285. continue;
  286. for (j = 0; j < types.Length; ++j) {
  287. if (types [j] != args [j].ParameterType)
  288. break;
  289. }
  290. if (j == types.Length)
  291. return m;
  292. }
  293. MethodBase result = null;
  294. for (i = 0; i < match.Length; ++i) {
  295. m = match [i];
  296. ParameterInfo[] args = m.GetParameters ();
  297. if (args.Length != types.Length)
  298. continue;
  299. if (!check_arguments (types, args))
  300. continue;
  301. if (result != null)
  302. throw new AmbiguousMatchException ();
  303. result = m;
  304. }
  305. return result;
  306. }
  307. public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
  308. {
  309. if (match == null || match.Length == 0)
  310. throw new ArgumentException ("No properties provided", "match");
  311. bool haveRet = (returnType != null);
  312. int idxlen = (indexes != null) ? indexes.Length : 0;
  313. PropertyInfo result = null;
  314. int i;
  315. int best_score = Int32.MaxValue - 1;
  316. int fail_score = Int32.MaxValue;
  317. int level = 0;
  318. for (i = match.Length - 1; i >= 0; i--) {
  319. PropertyInfo p = match [i];
  320. ParameterInfo[] args = p.GetIndexParameters ();
  321. if (idxlen > 0 && idxlen != args.Length)
  322. continue;
  323. if (haveRet && !check_type (p.PropertyType, returnType))
  324. continue;
  325. int score = Int32.MaxValue - 1;
  326. if (idxlen > 0) {
  327. score = check_arguments_with_score (indexes, args);
  328. if (score == -1)
  329. continue;
  330. }
  331. int new_level = GetDerivedLevel (p.DeclaringType);
  332. if (result != null) {
  333. if (best_score < score)
  334. continue;
  335. if (best_score == score) {
  336. if (level == new_level) {
  337. // Keep searching. May be there's something
  338. // better for us.
  339. fail_score = score;
  340. continue;
  341. }
  342. if (level > new_level)
  343. continue;
  344. }
  345. }
  346. result = p;
  347. best_score = score;
  348. level = new_level;
  349. }
  350. if (fail_score <= best_score)
  351. throw new AmbiguousMatchException ();
  352. return result;
  353. }
  354. static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
  355. {
  356. int worst = -1;
  357. for (int i = 0; i < types.Length; ++i) {
  358. int res = check_type_with_score (types [i], args [i].ParameterType);
  359. if (res == -1)
  360. return -1;
  361. if (worst < res)
  362. worst = res;
  363. }
  364. return worst;
  365. }
  366. // 0 -> same type or null and !valuetype
  367. // 1 -> to == Enum
  368. // 2 -> value type that don't lose data
  369. // 3 -> to == IsAssignableFrom
  370. // 4 -> to == object
  371. static int check_type_with_score (Type from, Type to)
  372. {
  373. if (from == null)
  374. return to.IsValueType ? -1 : 0;
  375. if (from == to)
  376. return 0;
  377. if (to == typeof (object))
  378. return 4;
  379. TypeCode fromt = Type.GetTypeCode (from);
  380. TypeCode tot = Type.GetTypeCode (to);
  381. switch (fromt) {
  382. case TypeCode.Char:
  383. switch (tot) {
  384. case TypeCode.UInt16:
  385. return 0;
  386. case TypeCode.UInt32:
  387. case TypeCode.Int32:
  388. case TypeCode.UInt64:
  389. case TypeCode.Int64:
  390. case TypeCode.Single:
  391. case TypeCode.Double:
  392. return 2;
  393. }
  394. return -1;
  395. case TypeCode.Byte:
  396. switch (tot) {
  397. case TypeCode.Char:
  398. case TypeCode.UInt16:
  399. case TypeCode.Int16:
  400. case TypeCode.UInt32:
  401. case TypeCode.Int32:
  402. case TypeCode.UInt64:
  403. case TypeCode.Int64:
  404. case TypeCode.Single:
  405. case TypeCode.Double:
  406. return 2;
  407. }
  408. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  409. case TypeCode.SByte:
  410. switch (tot) {
  411. case TypeCode.Int16:
  412. case TypeCode.Int32:
  413. case TypeCode.Int64:
  414. case TypeCode.Single:
  415. case TypeCode.Double:
  416. return 2;
  417. }
  418. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  419. case TypeCode.UInt16:
  420. switch (tot) {
  421. case TypeCode.UInt32:
  422. case TypeCode.Int32:
  423. case TypeCode.UInt64:
  424. case TypeCode.Int64:
  425. case TypeCode.Single:
  426. case TypeCode.Double:
  427. return 2;
  428. }
  429. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  430. case TypeCode.Int16:
  431. switch (tot) {
  432. case TypeCode.Int32:
  433. case TypeCode.Int64:
  434. case TypeCode.Single:
  435. case TypeCode.Double:
  436. return 2;
  437. }
  438. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  439. case TypeCode.UInt32:
  440. switch (tot) {
  441. case TypeCode.UInt64:
  442. case TypeCode.Int64:
  443. case TypeCode.Single:
  444. case TypeCode.Double:
  445. return 2;
  446. }
  447. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  448. case TypeCode.Int32:
  449. switch (tot) {
  450. case TypeCode.Int64:
  451. case TypeCode.Single:
  452. case TypeCode.Double:
  453. return 2;
  454. }
  455. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  456. case TypeCode.UInt64:
  457. case TypeCode.Int64:
  458. switch (tot) {
  459. case TypeCode.Single:
  460. case TypeCode.Double:
  461. return 2;
  462. }
  463. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  464. case TypeCode.Single:
  465. return tot == TypeCode.Double ? 2 : -1;
  466. default:
  467. return (to.IsAssignableFrom (from)) ? 3 : -1;
  468. }
  469. }
  470. }
  471. }
  472. }