Binder.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 (vtype == type || type.IsAssignableFrom (vtype))
  150. return value;
  151. if (vtype.IsArray && type.IsArray){
  152. if (IsArrayAssignable (vtype.GetElementType (), type.GetElementType ()))
  153. return value;
  154. }
  155. if (check_type (vtype, type))
  156. return Convert.ChangeType (value, type);
  157. return null;
  158. }
  159. [MonoTODO]
  160. public override void ReorderArgumentArray (ref object[] args, object state)
  161. {
  162. //do nothing until we support named arguments
  163. //throw new NotImplementedException ();
  164. }
  165. private static bool check_type (Type from, Type to) {
  166. if (from == to)
  167. return true;
  168. if (from == null)
  169. return !to.IsValueType;
  170. TypeCode fromt = Type.GetTypeCode (from);
  171. TypeCode tot = Type.GetTypeCode (to);
  172. switch (fromt) {
  173. case TypeCode.Char:
  174. switch (tot) {
  175. case TypeCode.UInt16:
  176. case TypeCode.UInt32:
  177. case TypeCode.Int32:
  178. case TypeCode.UInt64:
  179. case TypeCode.Int64:
  180. case TypeCode.Single:
  181. case TypeCode.Double:
  182. return true;
  183. }
  184. return to == typeof (object);
  185. case TypeCode.Byte:
  186. switch (tot) {
  187. case TypeCode.Char:
  188. case TypeCode.UInt16:
  189. case TypeCode.Int16:
  190. case TypeCode.UInt32:
  191. case TypeCode.Int32:
  192. case TypeCode.UInt64:
  193. case TypeCode.Int64:
  194. case TypeCode.Single:
  195. case TypeCode.Double:
  196. return true;
  197. }
  198. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  199. case TypeCode.SByte:
  200. switch (tot) {
  201. case TypeCode.Int16:
  202. case TypeCode.Int32:
  203. case TypeCode.Int64:
  204. case TypeCode.Single:
  205. case TypeCode.Double:
  206. return true;
  207. }
  208. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  209. case TypeCode.UInt16:
  210. switch (tot) {
  211. case TypeCode.UInt32:
  212. case TypeCode.Int32:
  213. case TypeCode.UInt64:
  214. case TypeCode.Int64:
  215. case TypeCode.Single:
  216. case TypeCode.Double:
  217. return true;
  218. }
  219. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  220. case TypeCode.Int16:
  221. switch (tot) {
  222. case TypeCode.Int32:
  223. case TypeCode.Int64:
  224. case TypeCode.Single:
  225. case TypeCode.Double:
  226. return true;
  227. }
  228. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  229. case TypeCode.UInt32:
  230. switch (tot) {
  231. case TypeCode.UInt64:
  232. case TypeCode.Int64:
  233. case TypeCode.Single:
  234. case TypeCode.Double:
  235. return true;
  236. }
  237. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  238. case TypeCode.Int32:
  239. switch (tot) {
  240. case TypeCode.Int64:
  241. case TypeCode.Single:
  242. case TypeCode.Double:
  243. return true;
  244. }
  245. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  246. case TypeCode.UInt64:
  247. case TypeCode.Int64:
  248. switch (tot) {
  249. case TypeCode.Single:
  250. case TypeCode.Double:
  251. return true;
  252. }
  253. return to == typeof (object) || (from.IsEnum && to == typeof (Enum));
  254. case TypeCode.Single:
  255. return tot == TypeCode.Double || to == typeof (object);
  256. default:
  257. /* TODO: handle valuetype -> byref */
  258. if (to == typeof (object) && from.IsValueType)
  259. return true;
  260. return to.IsAssignableFrom (from);
  261. }
  262. }
  263. private static bool check_arguments (Type[] types, ParameterInfo[] args) {
  264. for (int i = 0; i < types.Length; ++i) {
  265. if (!check_type (types [i], args [i].ParameterType))
  266. return false;
  267. }
  268. return true;
  269. }
  270. public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
  271. {
  272. MethodBase m;
  273. int i, j;
  274. if (match == null)
  275. throw new ArgumentNullException ("match");
  276. /* first look for an exact match... */
  277. for (i = 0; i < match.Length; ++i) {
  278. m = match [i];
  279. ParameterInfo[] args = m.GetParameters ();
  280. if (args.Length != types.Length)
  281. continue;
  282. for (j = 0; j < types.Length; ++j) {
  283. if (types [j] != args [j].ParameterType)
  284. break;
  285. }
  286. if (j == types.Length)
  287. return m;
  288. }
  289. MethodBase result = null;
  290. for (i = 0; i < match.Length; ++i) {
  291. m = match [i];
  292. ParameterInfo[] args = m.GetParameters ();
  293. if (args.Length != types.Length)
  294. continue;
  295. if (!check_arguments (types, args))
  296. continue;
  297. if (result != null)
  298. throw new AmbiguousMatchException ();
  299. result = m;
  300. }
  301. return result;
  302. }
  303. public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers)
  304. {
  305. if (match == null || match.Length == 0)
  306. throw new ArgumentException ("No properties provided", "match");
  307. bool haveRet = (returnType != null);
  308. int idxlen = (indexes != null) ? indexes.Length : 0;
  309. PropertyInfo result = null;
  310. int i;
  311. int best_score = Int32.MaxValue - 1;
  312. int fail_score = Int32.MaxValue;
  313. int level = 0;
  314. for (i = match.Length - 1; i >= 0; i--) {
  315. PropertyInfo p = match [i];
  316. ParameterInfo[] args = p.GetIndexParameters ();
  317. if (idxlen > 0 && idxlen != args.Length)
  318. continue;
  319. if (haveRet && !check_type (p.PropertyType, returnType))
  320. continue;
  321. int score = Int32.MaxValue - 1;
  322. if (idxlen > 0) {
  323. score = check_arguments_with_score (indexes, args);
  324. if (score == -1)
  325. continue;
  326. }
  327. int new_level = GetDerivedLevel (p.DeclaringType);
  328. if (result != null) {
  329. if (best_score < score)
  330. continue;
  331. if (best_score == score) {
  332. if (level == new_level) {
  333. // Keep searching. May be there's something
  334. // better for us.
  335. fail_score = score;
  336. continue;
  337. }
  338. if (level > new_level)
  339. continue;
  340. }
  341. }
  342. result = p;
  343. best_score = score;
  344. level = new_level;
  345. }
  346. if (fail_score <= best_score)
  347. throw new AmbiguousMatchException ();
  348. return result;
  349. }
  350. static int check_arguments_with_score (Type [] types, ParameterInfo [] args)
  351. {
  352. int worst = -1;
  353. for (int i = 0; i < types.Length; ++i) {
  354. int res = check_type_with_score (types [i], args [i].ParameterType);
  355. if (res == -1)
  356. return -1;
  357. if (worst < res)
  358. worst = res;
  359. }
  360. return worst;
  361. }
  362. // 0 -> same type or null and !valuetype
  363. // 1 -> to == Enum
  364. // 2 -> value type that don't lose data
  365. // 3 -> to == IsAssignableFrom
  366. // 4 -> to == object
  367. static int check_type_with_score (Type from, Type to)
  368. {
  369. if (from == null)
  370. return to.IsValueType ? -1 : 0;
  371. if (from == to)
  372. return 0;
  373. if (to == typeof (object))
  374. return 4;
  375. TypeCode fromt = Type.GetTypeCode (from);
  376. TypeCode tot = Type.GetTypeCode (to);
  377. switch (fromt) {
  378. case TypeCode.Char:
  379. switch (tot) {
  380. case TypeCode.UInt16:
  381. return 0;
  382. case TypeCode.UInt32:
  383. case TypeCode.Int32:
  384. case TypeCode.UInt64:
  385. case TypeCode.Int64:
  386. case TypeCode.Single:
  387. case TypeCode.Double:
  388. return 2;
  389. }
  390. return -1;
  391. case TypeCode.Byte:
  392. switch (tot) {
  393. case TypeCode.Char:
  394. case TypeCode.UInt16:
  395. case TypeCode.Int16:
  396. case TypeCode.UInt32:
  397. case TypeCode.Int32:
  398. case TypeCode.UInt64:
  399. case TypeCode.Int64:
  400. case TypeCode.Single:
  401. case TypeCode.Double:
  402. return 2;
  403. }
  404. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  405. case TypeCode.SByte:
  406. switch (tot) {
  407. case TypeCode.Int16:
  408. case TypeCode.Int32:
  409. case TypeCode.Int64:
  410. case TypeCode.Single:
  411. case TypeCode.Double:
  412. return 2;
  413. }
  414. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  415. case TypeCode.UInt16:
  416. switch (tot) {
  417. case TypeCode.UInt32:
  418. case TypeCode.Int32:
  419. case TypeCode.UInt64:
  420. case TypeCode.Int64:
  421. case TypeCode.Single:
  422. case TypeCode.Double:
  423. return 2;
  424. }
  425. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  426. case TypeCode.Int16:
  427. switch (tot) {
  428. case TypeCode.Int32:
  429. case TypeCode.Int64:
  430. case TypeCode.Single:
  431. case TypeCode.Double:
  432. return 2;
  433. }
  434. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  435. case TypeCode.UInt32:
  436. switch (tot) {
  437. case TypeCode.UInt64:
  438. case TypeCode.Int64:
  439. case TypeCode.Single:
  440. case TypeCode.Double:
  441. return 2;
  442. }
  443. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  444. case TypeCode.Int32:
  445. switch (tot) {
  446. case TypeCode.Int64:
  447. case TypeCode.Single:
  448. case TypeCode.Double:
  449. return 2;
  450. }
  451. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  452. case TypeCode.UInt64:
  453. case TypeCode.Int64:
  454. switch (tot) {
  455. case TypeCode.Single:
  456. case TypeCode.Double:
  457. return 2;
  458. }
  459. return (from.IsEnum && to == typeof (Enum)) ? 1 : -1;
  460. case TypeCode.Single:
  461. return tot == TypeCode.Double ? 2 : -1;
  462. default:
  463. return (to.IsAssignableFrom (from)) ? 3 : -1;
  464. }
  465. }
  466. }
  467. }
  468. }