Binder.cs 14 KB

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