BinderTests.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. //
  2. // System.Reflection.BinderTests - Tests Type.DefaultBinder
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) 2004 Novell, Inc. (http://www.novell.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Reflection;
  14. namespace MonoTests.System.Reflection
  15. {
  16. enum MyEnum {
  17. Zero,
  18. One,
  19. Two
  20. }
  21. class ParamsArrayTest
  22. {
  23. public ParamsArrayTest (params string[] strings)
  24. {}
  25. }
  26. class SampleClass {
  27. public static void SampleMethod (object o) { }
  28. public Type this[decimal i] {
  29. get { return i.GetType (); }
  30. }
  31. public Type this[object i] {
  32. get { return i.GetType (); }
  33. }
  34. }
  35. class SingleIndexer {
  36. public Type this [int i] {
  37. get { return i.GetType (); }
  38. }
  39. }
  40. class MultiIndexer
  41. {
  42. public Type this[byte i] {
  43. get { return i.GetType (); }
  44. }
  45. public Type this[sbyte i] {
  46. get { return i.GetType (); }
  47. }
  48. public Type this[short i] {
  49. get { return i.GetType (); }
  50. }
  51. public Type this[ushort i] {
  52. get { return i.GetType (); }
  53. }
  54. public Type this[int i] {
  55. get { return i.GetType (); }
  56. }
  57. public Type this[uint i] {
  58. get { return i.GetType (); }
  59. }
  60. public Type this[long i] {
  61. get { return i.GetType (); }
  62. }
  63. public Type this[ulong i] {
  64. get { return i.GetType (); }
  65. }
  66. public Type this[float i] {
  67. get { return i.GetType (); }
  68. }
  69. public Type this[double i] {
  70. get { return i.GetType (); }
  71. }
  72. public Type this[decimal i] {
  73. get { return i.GetType (); }
  74. }
  75. public Type this[object i] {
  76. get { return i.GetType (); }
  77. }
  78. public Type this[Enum i] {
  79. get { return i.GetType (); }
  80. }
  81. }
  82. class MethodInfoWrapper : MethodInfo
  83. {
  84. private readonly MethodInfo method;
  85. public MethodInfoWrapper (MethodInfo method)
  86. {
  87. this.method = method;
  88. }
  89. public override object[] GetCustomAttributes (bool inherit)
  90. {
  91. return method.GetCustomAttributes (inherit);
  92. }
  93. public override bool IsDefined (Type attributeType, bool inherit)
  94. {
  95. return method.IsDefined (attributeType, inherit);
  96. }
  97. public override ParameterInfo[] GetParameters ()
  98. {
  99. return method.GetParameters ();
  100. }
  101. public override MethodImplAttributes GetMethodImplementationFlags ()
  102. {
  103. return method.GetMethodImplementationFlags ();
  104. }
  105. public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
  106. {
  107. return method.Invoke (obj, invokeAttr, binder, parameters, culture);
  108. }
  109. public override MethodInfo GetBaseDefinition ()
  110. {
  111. return method.GetBaseDefinition ();
  112. }
  113. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  114. get { return method.ReturnTypeCustomAttributes; }
  115. }
  116. public override string Name {
  117. get { return method.Name; }
  118. }
  119. public override Type ReturnType {
  120. get { return method.ReturnType; }
  121. }
  122. public override Type DeclaringType {
  123. get { return method.DeclaringType; }
  124. }
  125. public override Type ReflectedType {
  126. get { return method.ReflectedType; }
  127. }
  128. public override RuntimeMethodHandle MethodHandle {
  129. get { return method.MethodHandle; }
  130. }
  131. public override MethodAttributes Attributes {
  132. get { return method.Attributes; }
  133. }
  134. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  135. {
  136. return method.GetCustomAttributes (attributeType, inherit);
  137. }
  138. }
  139. [TestFixture]
  140. public class BinderTest
  141. {
  142. Binder binder = Type.DefaultBinder;
  143. [Test]
  144. public void ParamsArrayTestCast ()
  145. {
  146. string[] test_args = { "one", "two", "three" };
  147. var o = Activator.CreateInstance (typeof (ParamsArrayTest), new object[] { test_args });
  148. Assert.IsNotNull (o, "#A1");
  149. }
  150. [Test]
  151. [ExpectedException (typeof (ArgumentException))]
  152. public void SelectPropertyTestNull1 ()
  153. {
  154. // The second argument is the one
  155. binder.SelectProperty (0, null, null, null, null);
  156. }
  157. [Test]
  158. [ExpectedException (typeof (ArgumentException))]
  159. public void SelectPropertyTestEmpty ()
  160. {
  161. // The second argument is the one
  162. binder.SelectProperty (0, new PropertyInfo [] {}, null, null, null);
  163. }
  164. [Test]
  165. [ExpectedException (typeof (NotSupportedException))]
  166. public void ChangeTypeOnDefaultBinder ()
  167. {
  168. binder.ChangeType (null, null, null);
  169. }
  170. [Test]
  171. [ExpectedException (typeof (AmbiguousMatchException))]
  172. public void AmbiguousProperty1 () // Bug 58381
  173. {
  174. Type type = typeof (MultiIndexer);
  175. PropertyInfo pi = type.GetProperty ("Item");
  176. }
  177. [Test]
  178. public void SelectAndInvokeAllProperties1 ()
  179. {
  180. Type type = typeof (MultiIndexer);
  181. PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
  182. BindingFlags.Public |
  183. BindingFlags.Instance);
  184. // These don't cause an AmbiguousMatchException
  185. Type [] types = { typeof (byte), typeof (short),
  186. typeof (int), typeof (long),
  187. typeof (MyEnum) };
  188. /* MS matches short for sbyte!!! */
  189. /* MS matches int for ushort!!! */
  190. /* MS matches long for uint!!! */
  191. /** These do weird things under MS if used together and then in separate arrays *
  192. Type [] types = { typeof (ulong), typeof (float), typeof (double),
  193. typeof (decimal), typeof (object) };
  194. */
  195. MultiIndexer obj = new MultiIndexer ();
  196. foreach (Type t in types) {
  197. PropertyInfo prop = null;
  198. try {
  199. prop = binder.SelectProperty (0, props, null, new Type [] {t}, null);
  200. } catch (Exception e) {
  201. throw new Exception ("Type: " + t, e);
  202. }
  203. Type gotten = (Type) prop.GetValue (obj, new object [] {Activator.CreateInstance (t)});
  204. Assert.AreEqual (t, gotten);
  205. }
  206. }
  207. [Test]
  208. public void SelectAndInvokeAllProperties2 ()
  209. {
  210. Type type = typeof (MultiIndexer);
  211. PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
  212. BindingFlags.Public |
  213. BindingFlags.Instance);
  214. Type [] types = { typeof (ushort), typeof (char) };
  215. MultiIndexer obj = new MultiIndexer ();
  216. PropertyInfo prop1 = binder.SelectProperty (0, props, null, new Type [] {types [0]}, null);
  217. PropertyInfo prop2 = binder.SelectProperty (0, props, null, new Type [] {types [1]}, null);
  218. Assert.AreEqual (prop1, prop2);
  219. }
  220. [Test]
  221. public void Select1Match2 ()
  222. {
  223. Type type = typeof (SingleIndexer);
  224. PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
  225. BindingFlags.Public |
  226. BindingFlags.Instance);
  227. PropertyInfo prop = binder.SelectProperty (0, props, null, new Type [0], null);
  228. Assert.IsNull (prop, "empty");
  229. }
  230. [Test]
  231. public void Select1Match ()
  232. {
  233. Type type = typeof (SingleIndexer);
  234. PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
  235. BindingFlags.Public |
  236. BindingFlags.Instance);
  237. PropertyInfo prop;
  238. prop = binder.SelectProperty (0, props, null, new Type [] { typeof (long) }, null);
  239. Assert.IsNull (prop, "long");
  240. prop = binder.SelectProperty (0, props, null, new Type [] { typeof (int) }, null);
  241. Assert.IsNotNull (prop, "int");
  242. prop = binder.SelectProperty (0, props, null, new Type [] { typeof (short) }, null);
  243. Assert.IsNotNull (prop, "short");
  244. }
  245. [Test]
  246. [Category ("NotWorking")]
  247. public void SelectMethod_AmbiguousMatch ()
  248. {
  249. Type type = typeof (BinderTest);
  250. BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
  251. MethodBase [] match;
  252. Type [] types;
  253. types = new Type [] { typeof (object), typeof (object []) };
  254. MethodInfo mi_params = type.GetMethod ("params_method1", flags, binder, types, null);
  255. Assert.IsNotNull (mi_params, "#A1");
  256. Assert.AreEqual (2, mi_params.GetParameters ().Length, "#A2");
  257. Assert.AreEqual (typeof (object), mi_params.GetParameters () [0].ParameterType, "#B3");
  258. Assert.AreEqual (typeof (object []), mi_params.GetParameters () [1].ParameterType, "#B4");
  259. types = new Type [] { typeof (object) };
  260. MethodInfo mi_single_param = type.GetMethod ("params_method1", flags, binder, types, null);
  261. Assert.IsNotNull (mi_single_param, "#B1");
  262. Assert.AreEqual (1, mi_single_param.GetParameters ().Length, "#B2");
  263. Assert.AreEqual (typeof (object), mi_single_param.GetParameters () [0].ParameterType, "#B3");
  264. match = new MethodBase [] { mi_single_param, mi_single_param };
  265. types = new Type [] { typeof (object) };
  266. try {
  267. binder.SelectMethod (flags, match, types, null);
  268. Assert.Fail ("#C1");
  269. } catch (AmbiguousMatchException) {
  270. // Ambiguous match found
  271. }
  272. match = new MethodBase [] { mi_single_param, mi_single_param };
  273. types = new Type [] { typeof (string) };
  274. try {
  275. binder.SelectMethod (flags, match, types, null);
  276. Assert.Fail ("#D1");
  277. } catch (AmbiguousMatchException) {
  278. // Ambiguous match found
  279. }
  280. }
  281. [Test]
  282. public void SelectMethod_ByRef ()
  283. {
  284. Type type = typeof (ByRefMatch);
  285. BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance;
  286. MethodBase [] match;
  287. Type [] types;
  288. MethodBase selected;
  289. Type ref_int;
  290. MethodInfo mi_run = type.GetMethod ("Run", flags, binder,
  291. new Type [] { typeof (int) }, null);
  292. Assert.IsFalse (mi_run.GetParameters () [0].ParameterType.IsByRef, "#A1");
  293. #if NET_2_0
  294. MethodInfo mi_run_ref = type.GetMethod ("Run", flags, binder,
  295. new Type [] { typeof (int).MakeByRefType () }, null);
  296. #else
  297. MethodInfo mi_run_ref = type.GetMethod ("RunV1", flags);
  298. #endif
  299. Assert.IsTrue (mi_run_ref.GetParameters () [0].ParameterType.IsByRef, "#A2");
  300. #if NET_2_0
  301. ref_int = typeof (int).MakeByRefType ();
  302. #else
  303. ref_int = mi_run_ref.GetParameters () [0].ParameterType;
  304. #endif
  305. match = new MethodBase [] { mi_run_ref };
  306. types = new Type [] { typeof (int) };
  307. selected = binder.SelectMethod (flags, match, types, null);
  308. Assert.IsNull (selected, "#B1");
  309. types = new Type [] { ref_int };
  310. selected = binder.SelectMethod (flags, match, types, null);
  311. Assert.AreSame (mi_run_ref, selected, "#B2");
  312. match = new MethodBase [] { mi_run };
  313. types = new Type [] { typeof (int) };
  314. selected = binder.SelectMethod (flags, match, types, null);
  315. Assert.AreSame (mi_run, selected, "#C1");
  316. types = new Type [] { ref_int };
  317. selected = binder.SelectMethod (flags, match, types, null);
  318. Assert.IsNull (selected, "#C1");
  319. match = new MethodBase [] { mi_run, mi_run_ref };
  320. types = new Type [] { typeof (int) };
  321. selected = binder.SelectMethod (flags, match, types, null);
  322. Assert.AreSame (mi_run, selected, "#D1");
  323. types = new Type [] { ref_int };
  324. selected = binder.SelectMethod (flags, match, types, null);
  325. Assert.AreSame (mi_run_ref, selected, "#D2");
  326. }
  327. [Test]
  328. [Category ("NotWorking")]
  329. public void SelectMethod_Params ()
  330. {
  331. Type type = typeof (BinderTest);
  332. BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
  333. MethodBase [] match;
  334. Type [] types;
  335. MethodBase selected;
  336. types = new Type [] { typeof (object), typeof (object) };
  337. MethodInfo mi_non_params = type.GetMethod ("params_method1", flags, binder, types, null);
  338. Assert.IsNotNull (mi_non_params, "#A1");
  339. Assert.AreEqual (2, mi_non_params.GetParameters ().Length, "#A2");
  340. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [0].ParameterType, "#A3");
  341. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [1].ParameterType, "#A4");
  342. types = new Type [] { typeof (object), typeof (object []) };
  343. MethodInfo mi_params = type.GetMethod ("params_method1", flags, binder, types, null);
  344. Assert.IsNotNull (mi_params, "#B1");
  345. Assert.AreEqual (2, mi_params.GetParameters ().Length, "#B2");
  346. Assert.AreEqual (typeof (object), mi_params.GetParameters () [0].ParameterType, "#B3");
  347. Assert.AreEqual (typeof (object []), mi_params.GetParameters () [1].ParameterType, "#B4");
  348. types = new Type [] { typeof (object) };
  349. MethodInfo mi_single_param = type.GetMethod ("params_method1", flags, binder, types, null);
  350. Assert.IsNotNull (mi_single_param, "#C1");
  351. Assert.AreEqual (1, mi_single_param.GetParameters ().Length, "#C2");
  352. Assert.AreEqual (typeof (object), mi_single_param.GetParameters () [0].ParameterType, "#C3");
  353. match = new MethodBase [] { mi_params };
  354. types = new Type [] { typeof (object) };
  355. selected = binder.SelectMethod (flags, match, types, null);
  356. Assert.IsNull (selected, "#D1");
  357. types = new Type [] { typeof (object), typeof (object) };
  358. selected = binder.SelectMethod (flags, match, types, null);
  359. Assert.IsNull (selected, "#D2");
  360. types = new Type [] { typeof (object), typeof (object []) };
  361. selected = binder.SelectMethod (flags, match, types, null);
  362. Assert.AreSame (mi_params, selected, "#D3");
  363. types = new Type [] { typeof (object), typeof (object), typeof (object) };
  364. selected = binder.SelectMethod (flags, match, types, null);
  365. Assert.IsNull (selected, "#D4");
  366. match = new MethodBase [] { mi_non_params };
  367. types = new Type [] { typeof (object) };
  368. selected = binder.SelectMethod (flags, match, types, null);
  369. Assert.IsNull (selected, "#E1");
  370. types = new Type [] { typeof (object), typeof (object) };
  371. selected = binder.SelectMethod (flags, match, types, null);
  372. Assert.AreSame (mi_non_params, selected, "#E2");
  373. types = new Type [] { typeof (object), typeof (object []) };
  374. selected = binder.SelectMethod (flags, match, types, null);
  375. Assert.AreSame (mi_non_params, selected, "#E3");
  376. types = new Type [] { typeof (object), typeof (object), typeof (object) };
  377. selected = binder.SelectMethod (flags, match, types, null);
  378. Assert.IsNull (selected, "#E4");
  379. match = new MethodBase [] { mi_non_params, mi_params };
  380. types = new Type [] { typeof (object) };
  381. selected = binder.SelectMethod (flags, match, types, null);
  382. Assert.IsNull (selected, "#F1");
  383. match = new MethodBase [] { mi_non_params, mi_params };
  384. types = new Type [] { typeof (object), typeof (object) };
  385. selected = binder.SelectMethod (flags, match, types, null);
  386. Assert.AreSame (mi_non_params, selected, "#F2");
  387. match = new MethodBase [] { mi_non_params, mi_params };
  388. types = new Type [] { typeof (object), typeof (object []) };
  389. selected = binder.SelectMethod (flags, match, types, null);
  390. Assert.AreSame (mi_params, selected, "#F3");
  391. match = new MethodBase [] { mi_non_params, mi_params };
  392. types = new Type [] { typeof (object), typeof (object), typeof (object) };
  393. selected = binder.SelectMethod (flags, match, types, null);
  394. Assert.IsNull (selected, "#F4");
  395. match = new MethodBase [] { mi_params, mi_non_params };
  396. types = new Type [] { typeof (object) };
  397. selected = binder.SelectMethod (flags, match, types, null);
  398. Assert.IsNull (selected, "#G1");
  399. match = new MethodBase [] { mi_params, mi_non_params };
  400. types = new Type [] { typeof (object), typeof (object) };
  401. selected = binder.SelectMethod (flags, match, types, null);
  402. Assert.AreSame (mi_non_params, selected, "#G2");
  403. match = new MethodBase [] { mi_params, mi_non_params };
  404. types = new Type [] { typeof (object), typeof (object []) };
  405. selected = binder.SelectMethod (flags, match, types, null);
  406. Assert.AreSame (mi_params, selected, "#G3");
  407. match = new MethodBase [] { mi_params, mi_non_params };
  408. types = new Type [] { typeof (object), typeof (object), typeof (object) };
  409. selected = binder.SelectMethod (flags, match, types, null);
  410. Assert.IsNull (selected, "#G4");
  411. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  412. types = new Type [] { typeof (object) };
  413. selected = binder.SelectMethod (flags, match, types, null);
  414. Assert.AreSame (mi_single_param, selected, "#H1");
  415. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  416. types = new Type [] { typeof (object), typeof (object) };
  417. selected = binder.SelectMethod (flags, match, types, null);
  418. Assert.AreSame (mi_non_params, selected, "#H2");
  419. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  420. types = new Type [] { typeof (object), typeof (object []) };
  421. selected = binder.SelectMethod (flags, match, types, null);
  422. Assert.AreSame (mi_params, selected, "#H3");
  423. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  424. types = new Type [] { typeof (object), typeof (object), typeof (object) };
  425. selected = binder.SelectMethod (flags, match, types, null);
  426. Assert.IsNull (selected, "#H4");
  427. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  428. types = new Type [] { typeof (string) };
  429. selected = binder.SelectMethod (flags, match, types, null);
  430. Assert.AreSame (mi_single_param, selected, "#I1");
  431. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  432. types = new Type [] { typeof (string), typeof (string) };
  433. selected = binder.SelectMethod (flags, match, types, null);
  434. Assert.AreSame (mi_non_params, selected, "#I2");
  435. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  436. types = new Type [] { typeof (string), typeof (string []) };
  437. selected = binder.SelectMethod (flags, match, types, null);
  438. Assert.AreSame (mi_params, selected, "#I3");
  439. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  440. types = new Type [] { typeof (string), typeof (string), typeof (long) };
  441. selected = binder.SelectMethod (flags, match, types, null);
  442. Assert.IsNull (selected, "#I4");
  443. }
  444. [Test] // bug #314809
  445. public void ArgNullOnMethod ()
  446. {
  447. Type type = typeof (SampleClass);
  448. BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod;
  449. type.InvokeMember ("SampleMethod", flags, null, null, new object[] { null });
  450. }
  451. [Test]
  452. public void ArgNullOnProperty ()
  453. {
  454. Type type = typeof (SampleClass);
  455. PropertyInfo [] props = type.GetProperties (BindingFlags.DeclaredOnly |
  456. BindingFlags.Public |
  457. BindingFlags.Instance);
  458. PropertyInfo prop = binder.SelectProperty (0, props, null, new Type [] {null}, null);
  459. Assert.IsNotNull (prop);
  460. }
  461. [Test]
  462. public void BindToMethod_ByRef ()
  463. {
  464. Type type = typeof (ByRefMatch);
  465. BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance;
  466. MethodBase [] match;
  467. object [] args = new object [] { 5 };
  468. object state;
  469. MethodBase selected;
  470. CultureInfo culture = CultureInfo.InvariantCulture;
  471. MethodInfo mi_run = type.GetMethod ("Run", flags, binder,
  472. new Type [] { typeof (int) }, null);
  473. Assert.IsFalse (mi_run.GetParameters () [0].ParameterType.IsByRef, "#A1");
  474. #if NET_2_0
  475. MethodInfo mi_run_ref = type.GetMethod ("Run", flags, binder,
  476. new Type [] { typeof (int).MakeByRefType () }, null);
  477. #else
  478. MethodInfo mi_run_ref = type.GetMethod ("RunV1", flags);
  479. #endif
  480. Assert.IsTrue (mi_run_ref.GetParameters () [0].ParameterType.IsByRef, "#A2");
  481. match = new MethodBase [] { mi_run };
  482. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  483. null, out state);
  484. Assert.AreSame (mi_run, selected, "#B1");
  485. match = new MethodBase [] { mi_run_ref };
  486. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  487. null, out state);
  488. Assert.AreSame (mi_run_ref, selected, "#B2");
  489. match = new MethodBase [] { mi_run, mi_run_ref };
  490. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  491. null, out state);
  492. Assert.AreSame (mi_run, selected, "#B3");
  493. match = new MethodBase [] { mi_run_ref, mi_run };
  494. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  495. null, out state);
  496. Assert.AreSame (mi_run, selected, "#B4");
  497. }
  498. [Test]
  499. [Category ("NotWorking")]
  500. public void BindToMethod_AmbiguousMatch ()
  501. {
  502. Type type = typeof (BinderTest);
  503. BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
  504. MethodBase [] match;
  505. Type [] types;
  506. object state;
  507. object [] args;
  508. CultureInfo culture = CultureInfo.InvariantCulture;
  509. types = new Type [] { typeof (object), typeof (object []) };
  510. MethodInfo mi_params = type.GetMethod ("params_method1", flags, binder, types, null);
  511. Assert.IsNotNull (mi_params, "#A1");
  512. Assert.AreEqual (2, mi_params.GetParameters ().Length, "#A2");
  513. Assert.AreEqual (typeof (object), mi_params.GetParameters () [0].ParameterType, "#B3");
  514. Assert.AreEqual (typeof (object []), mi_params.GetParameters () [1].ParameterType, "#B4");
  515. types = new Type [] { typeof (object) };
  516. MethodInfo mi_single_param = type.GetMethod ("params_method1", flags, binder, types, null);
  517. Assert.IsNotNull (mi_single_param, "#B1");
  518. Assert.AreEqual (1, mi_single_param.GetParameters ().Length, "#B2");
  519. Assert.AreEqual (typeof (object), mi_single_param.GetParameters () [0].ParameterType, "#B3");
  520. match = new MethodBase [] { mi_single_param, mi_single_param };
  521. args = new object [] { new object () };
  522. try {
  523. binder.BindToMethod (flags, match, ref args, null, culture,
  524. null, out state);
  525. Assert.Fail ("#C1");
  526. } catch (AmbiguousMatchException) {
  527. // Ambiguous match found
  528. }
  529. match = new MethodBase [] { mi_single_param, mi_single_param };
  530. args = new object [] { string.Empty };
  531. try {
  532. binder.BindToMethod (flags, match, ref args, null, culture,
  533. null, out state);
  534. Assert.Fail ("#D1");
  535. } catch (AmbiguousMatchException) {
  536. // Ambiguous match found
  537. }
  538. match = new MethodBase [] { mi_params, mi_params };
  539. args = new object [] { new object (), new object () };
  540. try {
  541. binder.BindToMethod (flags, match, ref args, null, culture,
  542. null, out state);
  543. Assert.Fail ("#E1");
  544. } catch (AmbiguousMatchException) {
  545. // Ambiguous match found
  546. }
  547. match = new MethodBase [] { mi_params, mi_params };
  548. args = new object [] { string.Empty, 0L };
  549. try {
  550. binder.BindToMethod (flags, match, ref args, null, culture,
  551. null, out state);
  552. Assert.Fail ("#F1");
  553. } catch (AmbiguousMatchException) {
  554. // Ambiguous match found
  555. }
  556. }
  557. [Test]
  558. [Category ("NotWorking")]
  559. public void BindToMethod_Params ()
  560. {
  561. Type type = typeof (BinderTest);
  562. BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
  563. MethodBase [] match;
  564. Type [] types;
  565. MethodBase selected;
  566. object state;
  567. object [] args;
  568. CultureInfo culture = CultureInfo.InvariantCulture;
  569. types = new Type [] { typeof (object), typeof (object) };
  570. MethodInfo mi_non_params = type.GetMethod ("params_method1", flags, binder, types, null);
  571. Assert.IsNotNull (mi_non_params, "#A1");
  572. Assert.AreEqual (2, mi_non_params.GetParameters ().Length, "#A2");
  573. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [0].ParameterType, "#A3");
  574. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [1].ParameterType, "#A4");
  575. types = new Type [] { typeof (object), typeof (object []) };
  576. MethodInfo mi_params = type.GetMethod ("params_method1", flags, binder, types, null);
  577. Assert.IsNotNull (mi_params, "#B1");
  578. Assert.AreEqual (2, mi_params.GetParameters ().Length, "#B2");
  579. Assert.AreEqual (typeof (object), mi_params.GetParameters () [0].ParameterType, "#B3");
  580. Assert.AreEqual (typeof (object []), mi_params.GetParameters () [1].ParameterType, "#B4");
  581. types = new Type [] { typeof (object) };
  582. MethodInfo mi_single_param = type.GetMethod ("params_method1", flags, binder, types, null);
  583. Assert.IsNotNull (mi_single_param, "#C1");
  584. Assert.AreEqual (1, mi_single_param.GetParameters ().Length, "#C2");
  585. Assert.AreEqual (typeof (object), mi_single_param.GetParameters () [0].ParameterType, "#C3");
  586. match = new MethodBase [] { mi_params };
  587. args = new object [] { new object () };
  588. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  589. null, out state);
  590. Assert.AreSame (mi_params, selected, "#D1");
  591. args = new object [] { new object (), new object () };
  592. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  593. null, out state);
  594. Assert.AreSame (mi_params, selected, "#D2");
  595. args = new object [] { new object (), new object [0] };
  596. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  597. null, out state);
  598. Assert.AreSame (mi_params, selected, "#D3");
  599. args = new object [] { new object (), new object (), new object () };
  600. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  601. null, out state);
  602. Assert.AreSame (mi_params, selected, "#D4");
  603. match = new MethodBase [] { mi_non_params };
  604. args = new object [] { new object () };
  605. try {
  606. binder.BindToMethod (flags, match, ref args, null, culture,
  607. null, out state);
  608. Assert.Fail ("#E1");
  609. } catch (MissingMethodException) {
  610. // Member not found
  611. }
  612. args = new object [] { new object (), new object () };
  613. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  614. null, out state);
  615. Assert.AreSame (mi_non_params, selected, "#E2");
  616. args = new object [] { new object (), new object [0] };
  617. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  618. null, out state);
  619. Assert.AreSame (mi_non_params, selected, "#E3");
  620. args = new object [] { new object (), new object (), new object () };
  621. try {
  622. binder.BindToMethod (flags, match, ref args, null, culture,
  623. null, out state);
  624. Assert.Fail ("#E4");
  625. } catch (MissingMethodException) {
  626. // Member not found
  627. }
  628. match = new MethodBase [] { mi_non_params, mi_params };
  629. args = new object [] { new object () };
  630. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  631. null, out state);
  632. Assert.AreSame (mi_params, selected, "#F1");
  633. match = new MethodBase [] { mi_non_params, mi_params };
  634. args = new object [] { new object (), new object () };
  635. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  636. null, out state);
  637. Assert.AreSame (mi_non_params, selected, "#F2");
  638. match = new MethodBase [] { mi_non_params, mi_params };
  639. args = new object [] { new object (), new object [0] };
  640. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  641. null, out state);
  642. Assert.AreSame (mi_params, selected, "#F3");
  643. match = new MethodBase [] { mi_non_params, mi_params };
  644. args = new object [] { new object (), new object (), new object () };
  645. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  646. null, out state);
  647. Assert.AreSame (mi_params, selected, "#F4");
  648. match = new MethodBase [] { mi_params, mi_non_params };
  649. args = new object [] { new object () };
  650. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  651. null, out state);
  652. Assert.AreSame (mi_params, selected, "#G1");
  653. match = new MethodBase [] { mi_params, mi_non_params };
  654. args = new object [] { new object (), new object () };
  655. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  656. null, out state);
  657. Assert.AreSame (mi_non_params, selected, "#G2");
  658. match = new MethodBase [] { mi_params, mi_non_params };
  659. args = new object [] { new object (), new object [0] };
  660. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  661. null, out state);
  662. Assert.AreSame (mi_params, selected, "#G3");
  663. match = new MethodBase [] { mi_params, mi_non_params };
  664. args = new object [] { new object (), new object (), new object () };
  665. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  666. null, out state);
  667. Assert.AreSame (mi_params, selected, "#G4");
  668. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  669. args = new object [] { new object () };
  670. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  671. null, out state);
  672. Assert.AreSame (mi_single_param, selected, "#H1");
  673. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  674. args = new object [] { new object (), new object () };
  675. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  676. null, out state);
  677. Assert.AreSame (mi_non_params, selected, "#H2");
  678. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  679. args = new object [] { new object (), new object [0] };
  680. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  681. null, out state);
  682. Assert.AreSame (mi_params, selected, "#H3");
  683. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  684. args = new object [] { new object (), new object (), new object () };
  685. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  686. null, out state);
  687. Assert.AreSame (mi_params, selected, "#H4");
  688. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  689. args = new object [] { string.Empty };
  690. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  691. null, out state);
  692. Assert.AreSame (mi_single_param, selected, "#I1");
  693. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  694. args = new object [] { string.Empty, string.Empty };
  695. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  696. null, out state);
  697. Assert.AreSame (mi_non_params, selected, "#I2");
  698. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  699. args = new object [] { string.Empty, new string [0] };
  700. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  701. null, out state);
  702. Assert.AreSame (mi_params, selected, "#I3");
  703. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  704. args = new object [] { string.Empty, string.Empty, 5L };
  705. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  706. null, out state);
  707. Assert.AreSame (mi_params, selected, "#I4");
  708. }
  709. [Test]
  710. [Category ("NotDotNet")]
  711. [Category ("NotWorking")]
  712. public void BindToMethod_Params_Mono ()
  713. {
  714. Type type = typeof (BinderTest);
  715. BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
  716. MethodBase [] match;
  717. Type [] types;
  718. MethodBase selected;
  719. object state;
  720. object [] args;
  721. CultureInfo culture = CultureInfo.InvariantCulture;
  722. types = new Type [] { typeof (object), typeof (object) };
  723. MethodInfo mi_non_params = type.GetMethod ("params_method1", flags, binder, types, null);
  724. Assert.IsNotNull (mi_non_params, "#A1");
  725. Assert.AreEqual (2, mi_non_params.GetParameters ().Length, "#A2");
  726. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [0].ParameterType, "#A3");
  727. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [1].ParameterType, "#A4");
  728. types = new Type [] { typeof (object), typeof (object []) };
  729. MethodInfo mi_params = type.GetMethod ("params_method1", flags, binder, types, null);
  730. Assert.IsNotNull (mi_params, "#B1");
  731. Assert.AreEqual (2, mi_params.GetParameters ().Length, "#B2");
  732. Assert.AreEqual (typeof (object), mi_params.GetParameters () [0].ParameterType, "#B3");
  733. Assert.AreEqual (typeof (object []), mi_params.GetParameters () [1].ParameterType, "#B4");
  734. types = new Type [] { typeof (object) };
  735. MethodInfo mi_single_param = type.GetMethod ("params_method1", flags, binder, types, null);
  736. Assert.IsNotNull (mi_single_param, "#C1");
  737. Assert.AreEqual (1, mi_single_param.GetParameters ().Length, "#C2");
  738. Assert.AreEqual (typeof (object), mi_single_param.GetParameters () [0].ParameterType, "#C3");
  739. match = new MethodBase [] { mi_non_params, mi_params };
  740. args = new object [] { new object () };
  741. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  742. null, out state);
  743. Assert.AreSame (mi_params, selected, "#D1");
  744. args = new object [] { new object (), new object () };
  745. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  746. null, out state);
  747. Assert.AreSame (mi_non_params, selected, "#D2");
  748. args = new object [] { new object (), new object [0] };
  749. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  750. null, out state);
  751. Assert.AreSame (mi_params, selected, "#D3");
  752. args = new object [] { new object (), new object (), new object () };
  753. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  754. null, out state);
  755. Assert.AreSame (mi_params, selected, "#D4");
  756. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  757. args = new object [] { new object () };
  758. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  759. null, out state);
  760. Assert.AreSame (mi_single_param, selected, "#E1");
  761. args = new object [] { new object (), new object () };
  762. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  763. null, out state);
  764. Assert.AreSame (mi_non_params, selected, "#E2");
  765. }
  766. [Test]
  767. [Category ("NotWorking")]
  768. public void BindToMethod_Params_MS ()
  769. {
  770. Type type = typeof (BinderTest);
  771. BindingFlags flags = BindingFlags.Static | BindingFlags.Public;
  772. MethodBase [] match;
  773. Type [] types;
  774. MethodBase selected;
  775. object state;
  776. object [] args;
  777. CultureInfo culture = CultureInfo.InvariantCulture;
  778. types = new Type [] { typeof (object), typeof (object) };
  779. MethodInfo mi_non_params = type.GetMethod ("params_method1", flags, binder, types, null);
  780. Assert.IsNotNull (mi_non_params, "#A1");
  781. Assert.AreEqual (2, mi_non_params.GetParameters ().Length, "#A2");
  782. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [0].ParameterType, "#A3");
  783. Assert.AreEqual (typeof (object), mi_non_params.GetParameters () [1].ParameterType, "#A4");
  784. types = new Type [] { typeof (object), typeof (object []) };
  785. MethodInfo mi_params = type.GetMethod ("params_method1", flags, binder, types, null);
  786. Assert.IsNotNull (mi_params, "#B1");
  787. Assert.AreEqual (2, mi_params.GetParameters ().Length, "#B2");
  788. Assert.AreEqual (typeof (object), mi_params.GetParameters () [0].ParameterType, "#B3");
  789. Assert.AreEqual (typeof (object []), mi_params.GetParameters () [1].ParameterType, "#B4");
  790. types = new Type [] { typeof (object) };
  791. MethodInfo mi_single_param = type.GetMethod ("params_method1", flags, binder, types, null);
  792. Assert.IsNotNull (mi_single_param, "#C1");
  793. Assert.AreEqual (1, mi_single_param.GetParameters ().Length, "#C2");
  794. Assert.AreEqual (typeof (object), mi_single_param.GetParameters () [0].ParameterType, "#C3");
  795. match = new MethodBase [] { mi_non_params, mi_params };
  796. args = new object [] { new object () };
  797. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  798. null, out state);
  799. Assert.AreSame (mi_params, selected, "#D1");
  800. args = new object [] { new object (), new object () };
  801. try {
  802. binder.BindToMethod (flags, match, ref args, null, culture,
  803. null, out state);
  804. Assert.Fail ("#D2");
  805. } catch (AmbiguousMatchException) {
  806. }
  807. args = new object [] { new object (), new object [0] };
  808. try {
  809. binder.BindToMethod (flags, match, ref args, null, culture,
  810. null, out state);
  811. Assert.Fail ("#D3");
  812. } catch (AmbiguousMatchException) {
  813. }
  814. args = new object [] { new object (), new object (), new object () };
  815. try {
  816. binder.BindToMethod (flags, match, ref args, null, culture,
  817. null, out state);
  818. Assert.Fail ("#D4");
  819. } catch (IndexOutOfRangeException) {
  820. }
  821. match = new MethodBase [] { mi_params, mi_non_params, mi_single_param };
  822. args = new object [] { new object () };
  823. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  824. null, out state);
  825. Assert.AreSame (mi_single_param, selected, "#E1");
  826. args = new object [] { new object (), new object () };
  827. selected = binder.BindToMethod (flags, match, ref args, null, culture,
  828. null, out state);
  829. Assert.AreSame (mi_params, selected, "#E2");
  830. }
  831. [Test] // bug #41691
  832. public void BindToMethodNamedArgs ()
  833. {
  834. Type t = typeof (Bug41691);
  835. StringWriter sw = new StringWriter ();
  836. sw.NewLine = "\n";
  837. object[] argValues = new object [] {"Hello", "World", "Extra", sw};
  838. string [] argNames = new string [] {"firstName", "lastName"};
  839. t.InvokeMember ("PrintName",
  840. BindingFlags.InvokeMethod,
  841. null,
  842. null,
  843. argValues,
  844. null,
  845. null,
  846. argNames);
  847. Assert.AreEqual ("Hello\nExtra\nWorld\n", sw.ToString ());
  848. }
  849. [Test] // #1321
  850. public void BindToMethodNamedArgs_2 ()
  851. {
  852. StringWriter sw = new StringWriter ();
  853. sw.NewLine = "\n";
  854. object[] argValues = new object [] {5, "AB", sw};
  855. string [] argNames = new string [] {"second", "first", "output"};
  856. typeof (BinderTest).InvokeMember ("TestMethod",
  857. BindingFlags.InvokeMethod,
  858. null,
  859. null,
  860. argValues,
  861. null,
  862. null,
  863. argNames);
  864. Assert.AreEqual ("AB\n5\n", sw.ToString ());
  865. }
  866. public static void TestMethod (string first, int second, TextWriter output) {
  867. output.WriteLine (first);
  868. output.WriteLine (second);
  869. }
  870. public class Bug41691
  871. {
  872. public static void PrintName (string lastName, string firstName, string extra, TextWriter output)
  873. {
  874. output.WriteLine (firstName);
  875. output.WriteLine (extra);
  876. output.WriteLine (lastName);
  877. }
  878. }
  879. [Test] // bug #42457
  880. public void GetMethodAmbiguity ()
  881. {
  882. object IntegerObject = 5;
  883. object IntArrayObject = new int[] {5, 2, 5};
  884. object StringArrayObject = new string [] {"One", "Two"};
  885. object [] IntParam = new object [] {IntegerObject};
  886. object [] IntArrayParam = new object [] {IntArrayObject};
  887. object [] StringArrayParam = new object [] {StringArrayObject};
  888. object be = this;
  889. Type betype = this.GetType ();
  890. string name1 = "Bug42457Method";
  891. string name2 = "Bug42457Method2";
  892. MethodInfo mi_obj = betype.GetMethod (name1, Type.GetTypeArray (IntParam));
  893. mi_obj.Invoke (be, IntParam);
  894. Assert.AreEqual (1, bug42457, "#1");
  895. MethodInfo mi_arr = betype.GetMethod (name1, Type.GetTypeArray (IntArrayParam));
  896. mi_arr.Invoke (be, IntArrayParam);
  897. Assert.AreEqual (2, bug42457, "#2");
  898. MethodInfo mi_str = betype.GetMethod (name1, Type.GetTypeArray (StringArrayParam));
  899. mi_str.Invoke (be, StringArrayParam);
  900. Assert.AreEqual (3, bug42457, "#3");
  901. MethodInfo m2_obj = betype.GetMethod (name2, Type.GetTypeArray (IntParam));
  902. m2_obj.Invoke (be, IntParam);
  903. Assert.AreEqual (1, bug42457_2, "#4");
  904. MethodInfo m2_arr = betype.GetMethod (name2, Type.GetTypeArray (IntArrayParam));
  905. m2_arr.Invoke (be, IntArrayParam);
  906. Assert.AreEqual (2, bug42457_2, "#5");
  907. MethodInfo m2_str = betype.GetMethod (name2, Type.GetTypeArray(StringArrayParam));
  908. m2_str.Invoke (be, StringArrayParam);
  909. Assert.AreEqual (3, bug42457_2, "#6");
  910. }
  911. #if NET_2_0
  912. [Test]
  913. public void NullableArg () {
  914. MethodInfo method = (typeof (BinderTest)).GetMethod("SetA", new [] {typeof (Int32)});
  915. Assert.AreEqual (5, method.Invoke (new BinderTest (), new object [] { 5 }));
  916. }
  917. public int SetA(Int32? a) {
  918. return (int)a;
  919. }
  920. #endif
  921. static void MethodWithLongParam(long param)
  922. {
  923. }
  924. [Test]
  925. public void TestParamsAttribute ()
  926. {
  927. MethodInfo mi;
  928. mi = typeof (BinderTest).GetMethod ("params_method1", BindingFlags.Static | BindingFlags.Public, null, new Type [] { typeof (object), typeof (object) }, null);
  929. Assert.IsNotNull (mi, "#A1");
  930. Assert.AreEqual (typeof (object), mi.GetParameters () [1].ParameterType, "#A2");
  931. mi = typeof (BinderTest).GetMethod ("params_method1", BindingFlags.Static | BindingFlags.Public, null, new Type [] { typeof (object), typeof (object []) }, null);
  932. Assert.IsNotNull (mi, "#B1");
  933. Assert.AreEqual (typeof (object []), mi.GetParameters () [1].ParameterType, "#B2");
  934. }
  935. [Test]
  936. public void TestParamsAttribute_1 ()
  937. {
  938. MethodInfo mi = typeof (BinderTest).GetMethod ("params_method1", BindingFlags.Static | BindingFlags.Public, null, new Type [] { typeof (object), typeof (object), typeof (object) }, null);
  939. Assert.IsNull (mi, "#1");
  940. }
  941. [Test]
  942. public void TestParamsAttribute_2 ()
  943. {
  944. MethodInfo mi = typeof (BinderTest).GetMethod ("params_method2", BindingFlags.Static | BindingFlags.Public, null, Type.EmptyTypes, null);
  945. Assert.IsNull (mi, "#1");
  946. }
  947. public static void params_method1 (object o)
  948. {
  949. }
  950. public static void params_method1 (object o, params object[] o2)
  951. {
  952. }
  953. public static void params_method1 (object o, object o2)
  954. {
  955. }
  956. public static void params_method2 (params string[] args)
  957. {
  958. }
  959. public static double DoubleMethod (double d) {
  960. return d;
  961. }
  962. public static float FloatMethod (float f) {
  963. return f;
  964. }
  965. [Test]
  966. public void ChangeType ()
  967. {
  968. // Char -> Double
  969. Assert.AreEqual (42.0, typeof (BinderTest).GetMethod ("DoubleMethod").Invoke (null, new object[] { (char)42 }));
  970. // Char -> Float
  971. Assert.AreEqual (42.0f, typeof (BinderTest).GetMethod ("FloatMethod").Invoke (null, new object[] { (char)42 }));
  972. }
  973. [Test]
  974. public void TestExactBinding ()
  975. {
  976. Type[] types = new Type[] { typeof(int) };
  977. BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.ExactBinding;
  978. Assert.IsNull (typeof (BinderTest).GetMethod("MethodWithLongParam",
  979. flags, null, types, null));
  980. }
  981. public void Bug42457Method (object thing)
  982. {
  983. bug42457 = 1;
  984. }
  985. public void Bug42457Method (Array thing)
  986. {
  987. bug42457 = 2;
  988. }
  989. public void Bug42457Method (string [] thing)
  990. {
  991. bug42457 = 3;
  992. }
  993. public void Bug42457Method2 (object thing)
  994. {
  995. bug42457_2 = 1;
  996. }
  997. public void Bug42457Method2 (Array thing)
  998. {
  999. bug42457_2 = 2;
  1000. }
  1001. public void Bug42457Method2 (string [] thing)
  1002. {
  1003. bug42457_2 = 3;
  1004. }
  1005. int bug42457, bug42457_2;
  1006. [Test] // bug #77079
  1007. public void GetMethodAvoidAmbiguity2 ()
  1008. {
  1009. Type tType = this.GetType ();
  1010. Bug77079A a = new Bug77079C ();
  1011. tType.InvokeMember ("Bug77079",
  1012. BindingFlags.Public | BindingFlags.InvokeMethod |
  1013. BindingFlags.Instance,
  1014. null, this, new object[] {a});
  1015. Assert.AreEqual (2, bug77079);
  1016. }
  1017. int bug77079;
  1018. public void Bug77079 (Bug77079A a)
  1019. {
  1020. bug77079 = 1;
  1021. }
  1022. public void Bug77079 (Bug77079B a)
  1023. {
  1024. bug77079 = 2;
  1025. }
  1026. public class Bug77079A
  1027. {
  1028. }
  1029. public class Bug77079B : Bug77079A
  1030. {
  1031. }
  1032. public class Bug77079C : Bug77079B
  1033. {
  1034. }
  1035. [Test] // bug #76083
  1036. public void GetMethodAvoidAmbiguity3 ()
  1037. {
  1038. Type[] types = new Type[] { typeof (Bug76083ArgDerived) };
  1039. MethodInfo m = typeof (Bug76083Derived).GetMethod ("Foo", types);
  1040. Assert.AreEqual (typeof (Bug76083Derived), m.DeclaringType);
  1041. }
  1042. public class Bug76083ArgBase {}
  1043. public class Bug76083ArgDerived : Bug76083ArgBase {}
  1044. public class Bug76083Base
  1045. {
  1046. public void Foo (Bug76083ArgBase a) {}
  1047. }
  1048. public class Bug76083Derived : Bug76083Base
  1049. {
  1050. public new void Foo (Bug76083ArgBase a) {}
  1051. }
  1052. private const BindingFlags BUG324998_BINDING_FLAGS
  1053. = BindingFlags.Public | BindingFlags.NonPublic
  1054. | BindingFlags.Instance | BindingFlags.Static
  1055. | BindingFlags.IgnoreCase;
  1056. class Bug324998AGood { public virtual void f(int i1, int i2, bool b) {} }
  1057. class Bug324998BGood : Bug324998AGood { public override void f(int i1, int i2, bool b) {} }
  1058. class Bug324998ABad {
  1059. public virtual void f(int i1, int i2) {}
  1060. public virtual void f(int i1, int i2, bool b) {}
  1061. }
  1062. class Bug324998BBad : Bug324998ABad { public override void f(int i1, int i2, bool b) {} }
  1063. [Test]
  1064. public void Bug324998Good () {
  1065. if (typeof(Bug324998BGood).GetMethod("f", BUG324998_BINDING_FLAGS) == null)
  1066. throw new Exception("Bug324998Good");
  1067. }
  1068. [Test]
  1069. [ExpectedException (typeof (AmbiguousMatchException))]
  1070. public void Bug324998Bad () {
  1071. typeof(Bug324998BBad).GetMethod("f", BUG324998_BINDING_FLAGS);
  1072. }
  1073. void Bug380361 (MyEnum e) { }
  1074. [Test]
  1075. public void TestEnumConversion ()
  1076. {
  1077. Type type = this.GetType ();
  1078. MethodInfo mi = type.GetMethod ("Bug380361", BindingFlags.NonPublic | BindingFlags.Instance, binder, new Type [] { typeof (MyEnum) }, null);
  1079. mi.Invoke (this, new object [] { (int)MyEnum.Zero });
  1080. }
  1081. [Test]
  1082. public void TestEnumConversion2 ()
  1083. {
  1084. Type type = this.GetType ();
  1085. MethodInfo mi = type.GetMethod ("Bug380361", BindingFlags.NonPublic | BindingFlags.Instance, binder, new Type [] { typeof (MyEnum) }, null);
  1086. try {
  1087. mi.Invoke (this, new object [] { (long) MyEnum.Zero });
  1088. Assert.Fail ("#1");
  1089. } catch (ArgumentException ex) {
  1090. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1091. Assert.IsNull (ex.InnerException, "#3");
  1092. Assert.IsNotNull (ex.Message, "#4");
  1093. Assert.IsNull (ex.ParamName, "#5");
  1094. }
  1095. }
  1096. class AssertingBinder : Binder {
  1097. public static readonly AssertingBinder Instance = new AssertingBinder ();
  1098. public override FieldInfo BindToField (BindingFlags bindingAttr, FieldInfo [] match, object value, CultureInfo culture)
  1099. {
  1100. Assert.IsNotNull (match);
  1101. return Type.DefaultBinder.BindToField (bindingAttr, match, value, culture);
  1102. }
  1103. public override MethodBase BindToMethod (BindingFlags bindingAttr, MethodBase [] match, ref object [] args, ParameterModifier [] modifiers, CultureInfo culture, string [] names, out object state)
  1104. {
  1105. Assert.IsNotNull (match);
  1106. Assert.IsNotNull (args);
  1107. return Type.DefaultBinder.BindToMethod (bindingAttr, match, ref args, modifiers, culture, names, out state);
  1108. }
  1109. public override object ChangeType (object value, Type type, CultureInfo culture)
  1110. {
  1111. Assert.IsNotNull (value);
  1112. Assert.IsNotNull (type);
  1113. return Type.DefaultBinder.ChangeType (value, type, culture);
  1114. }
  1115. public override void ReorderArgumentArray (ref object [] args, object state)
  1116. {
  1117. Assert.IsNotNull (args);
  1118. Type.DefaultBinder.ReorderArgumentArray (ref args, state);
  1119. }
  1120. public override MethodBase SelectMethod (BindingFlags bindingAttr, MethodBase [] match, Type [] types, ParameterModifier [] modifiers)
  1121. {
  1122. Assert.IsNotNull (match);
  1123. Assert.IsNotNull (types);
  1124. return Type.DefaultBinder.SelectMethod (bindingAttr, match, types, modifiers);
  1125. }
  1126. public override PropertyInfo SelectProperty (BindingFlags bindingAttr, PropertyInfo [] match, Type returnType, Type [] indexes, ParameterModifier [] modifiers)
  1127. {
  1128. Assert.IsNotNull (match);
  1129. return Type.DefaultBinder.SelectProperty (bindingAttr, match, returnType, indexes, modifiers);
  1130. }
  1131. }
  1132. class BaseFoo {
  1133. public void Bar ()
  1134. {
  1135. }
  1136. public int Add(int x, int y)
  1137. {
  1138. return x + y;
  1139. }
  1140. }
  1141. class Foo : BaseFoo {
  1142. public bool Barred;
  1143. public new void Bar ()
  1144. {
  1145. Barred = true;
  1146. }
  1147. }
  1148. class ByRefMatch {
  1149. public void Run (int i)
  1150. {
  1151. }
  1152. #if NET_2_0
  1153. public void Run (out int i)
  1154. #else
  1155. public void RunV1 (out int i)
  1156. #endif
  1157. {
  1158. i = 0;
  1159. }
  1160. }
  1161. [Test] // bug #471257
  1162. public void TestCustomBinderNonNullArgs ()
  1163. {
  1164. var foo = new Foo ();
  1165. typeof (Foo).InvokeMember (
  1166. "Bar",
  1167. BindingFlags.InvokeMethod,
  1168. AssertingBinder.Instance,
  1169. foo,
  1170. null);
  1171. Assert.IsTrue (foo.Barred);
  1172. }
  1173. class Int32Binder : AssertingBinder
  1174. {
  1175. public override object ChangeType(Object value, Type type, CultureInfo ci)
  1176. {
  1177. if (value.GetType() == type) {
  1178. return value;
  1179. } else if (type.IsPrimitive) {
  1180. if (type == typeof(Int32))
  1181. return Convert.ToInt32(value);
  1182. throw new ArgumentException("missing support for primitive: " + type);
  1183. }
  1184. throw new ArgumentException("Could not ChangeType to " + type.FullName);
  1185. }
  1186. }
  1187. [Test]
  1188. [ExpectedException(typeof (TargetParameterCountException))]
  1189. public void TestTargetParameterCountExceptionA ()
  1190. {
  1191. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1192. method.Invoke((new Foo ()), 0, null, null, null);
  1193. }
  1194. [Test]
  1195. [ExpectedException(typeof (TargetParameterCountException))]
  1196. public void TestTargetParameterCountExceptionB ()
  1197. {
  1198. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1199. method.Invoke(new Foo (), 0, null, new object [] {1}, null);
  1200. }
  1201. [Test]
  1202. public void TestBindingFlagsA ()
  1203. {
  1204. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1205. method.Invoke((new Foo ()), 0, null, new object [] {1, 2}, null);
  1206. }
  1207. [Test]
  1208. [ExpectedException(typeof (ArgumentException))]
  1209. public void TestBindingFlagsB ()
  1210. {
  1211. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1212. method.Invoke((new Foo ()), 0, null, new object [] {1, "2"}, null);
  1213. }
  1214. [Test]
  1215. public void TestBindingFlagsExactBindingA ()
  1216. {
  1217. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1218. method.Invoke((new Foo ()), BindingFlags.ExactBinding, null, new object [] {1, 2}, null);
  1219. }
  1220. [Test]
  1221. [ExpectedException(typeof (ArgumentException))]
  1222. public void TestBindingFlagsExactBindingB ()
  1223. {
  1224. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1225. method.Invoke((new Foo ()), BindingFlags.ExactBinding, new Int32Binder (), new object [] {1, "2"}, null);
  1226. }
  1227. [Test]
  1228. public void TestBindingFlagsExactBindingC ()
  1229. {
  1230. MethodInfo method = typeof (Foo).GetMethod ("Add");
  1231. method.Invoke((new Foo ()), 0, new Int32Binder (), new object [] {1, "2"}, null);
  1232. }
  1233. public void Bug325306<T> (int a) {}
  1234. public void Bug325306_ (int a) {}
  1235. [Test] //bug 325306
  1236. [ExpectedException (typeof (AmbiguousMatchException))]
  1237. public void SelectMethodWithExactAndAmbiguousMethods ()
  1238. {
  1239. var m = typeof (BinderTest).GetMethod ("Bug325306_");
  1240. BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;
  1241. AssertingBinder.Instance.SelectMethod (flags, new MethodBase [] {m, m}, new Type[] { typeof (int) }, null);
  1242. }
  1243. [Test] //bug 325306
  1244. [ExpectedException (typeof (AmbiguousMatchException))]
  1245. public void SelectMethodWithGmdAmbiguity ()
  1246. {
  1247. var m0 = typeof (BinderTest).GetMethod ("Bug325306");
  1248. var m1 = typeof (BinderTest).GetMethod ("Bug325306_");
  1249. BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;
  1250. AssertingBinder.Instance.SelectMethod (flags, new MethodBase [] {m0, m1}, new Type[] { typeof (int) }, null);
  1251. }
  1252. public static string Bug636939 (IFormatProvider provider, string pattern, params object [] args)
  1253. {
  1254. return string.Format (pattern, args);
  1255. }
  1256. [Test] // bug #636939
  1257. [Category ("NotWorking")]
  1258. public void SelectMethodWithParamArrayAndNonEqualTypeArguments ()
  1259. {
  1260. const BindingFlags flags =
  1261. BindingFlags.IgnoreCase | BindingFlags.Instance |
  1262. BindingFlags.Static | BindingFlags.Public |
  1263. BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod;
  1264. Assert.AreEqual ("foobarbaz", typeof (BinderTest).InvokeMember (
  1265. "bug636939",
  1266. flags,
  1267. null, // binder
  1268. null, // target
  1269. new object [] { CultureInfo.CurrentCulture, "foo{0}{1}", "bar", "baz" }));
  1270. }
  1271. public static void CustomMethodType_Helper ()
  1272. {
  1273. }
  1274. [Test]
  1275. public void CustomMethodType ()
  1276. {
  1277. var method = new MethodInfoWrapper (GetType ().GetMethod ("CustomMethodType_Helper"));
  1278. var res = Type.DefaultBinder.SelectMethod (BindingFlags.Static | BindingFlags.Public, new[] { method }, Type.EmptyTypes, new ParameterModifier[0]);
  1279. Assert.AreSame (method, res);
  1280. }
  1281. }
  1282. }