MethodInfoTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. //
  2. // System.Reflection.MethodInfo Test Cases
  3. //
  4. // Authors:
  5. // Zoltan Varga ([email protected])
  6. //
  7. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Threading;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Runtime.InteropServices;
  35. using System.Runtime.CompilerServices;
  36. #if NET_2_0
  37. using System.Collections.Generic;
  38. #endif
  39. namespace A.B.C {
  40. public struct MethodInfoTestStruct {
  41. int p;
  42. }
  43. }
  44. namespace MonoTests.System.Reflection
  45. {
  46. [TestFixture]
  47. public class MethodInfoTest
  48. {
  49. #if !TARGET_JVM
  50. [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
  51. public static extern void dllImportMethod ();
  52. #endif
  53. [MethodImplAttribute(MethodImplOptions.PreserveSig)]
  54. public void preserveSigMethod ()
  55. {
  56. }
  57. [MethodImplAttribute(MethodImplOptions.Synchronized)]
  58. public void synchronizedMethod ()
  59. {
  60. }
  61. [Test]
  62. public void IsDefined_AttributeType_Null ()
  63. {
  64. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
  65. try {
  66. mi.IsDefined ((Type) null, false);
  67. Assert.Fail ("#1");
  68. } catch (ArgumentNullException ex) {
  69. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  70. Assert.IsNull (ex.InnerException, "#3");
  71. Assert.IsNotNull (ex.Message, "#4");
  72. Assert.IsNotNull (ex.ParamName, "#5");
  73. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  74. }
  75. }
  76. #if NET_2_0
  77. [Test]
  78. public void PseudoCustomAttributes ()
  79. {
  80. Type t = typeof (MethodInfoTest);
  81. DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
  82. Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
  83. Assert.AreEqual ("foo", attr.EntryPoint, "#2");
  84. Assert.AreEqual ("libfoo", attr.Value, "#3");
  85. Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
  86. Assert.AreEqual (false, attr.ExactSpelling, "#5");
  87. Assert.AreEqual (true, attr.PreserveSig, "#6");
  88. Assert.AreEqual (true, attr.SetLastError, "#7");
  89. Assert.AreEqual (true, attr.BestFitMapping, "#8");
  90. Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
  91. PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
  92. // This doesn't work under MS.NET
  93. /*
  94. MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
  95. */
  96. }
  97. [return: MarshalAs (UnmanagedType.Interface)]
  98. public void ReturnTypeMarshalAs ()
  99. {
  100. }
  101. [Test]
  102. [Category ("TargetJvmNotWorking")]
  103. public void ReturnTypePseudoCustomAttributes ()
  104. {
  105. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
  106. Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
  107. }
  108. #endif
  109. public static int foo (int i, int j)
  110. {
  111. return i + j;
  112. }
  113. [Test]
  114. public void StaticInvokeWithObject ()
  115. {
  116. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
  117. mi.Invoke (new Object (), new object [] { 1, 2 });
  118. }
  119. [Test]
  120. public void ByRefInvoke ()
  121. {
  122. MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
  123. object[] parms = new object[] {1};
  124. met.Invoke (null, parms);
  125. Assert.AreEqual (2, parms[0]);
  126. }
  127. public static void ByRefTest (ref int a1)
  128. {
  129. if (a1 == 1)
  130. a1 = 2;
  131. }
  132. static int byref_arg;
  133. public static void ByrefVtype (ref int i) {
  134. byref_arg = i;
  135. i = 5;
  136. }
  137. [Test]
  138. #if ONLY_1_1
  139. [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
  140. #endif
  141. public void ByrefVtypeInvoke ()
  142. {
  143. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
  144. object o = 1;
  145. object[] args = new object [] { o };
  146. mi.Invoke (null, args);
  147. Assert.AreEqual (1, byref_arg, "#A1");
  148. Assert.AreEqual (1, o, "#A2");
  149. Assert.AreEqual (5, args[0], "#A3");
  150. args [0] = null;
  151. mi.Invoke (null, args);
  152. Assert.AreEqual (0, byref_arg, "#B1");
  153. Assert.AreEqual (5, args[0], "#B2");
  154. }
  155. public void HeyHey (out string out1, ref string ref1)
  156. {
  157. out1 = null;
  158. }
  159. public void SignatureTest (__arglist)
  160. {
  161. }
  162. public static unsafe int* PtrFunc (int* a)
  163. {
  164. return (int*) 0;
  165. }
  166. [Test] // bug #81538
  167. public void InvokeThreadAbort ()
  168. {
  169. MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
  170. try {
  171. method.Invoke (null, new object [0]);
  172. Assert.Fail ("#1");
  173. }
  174. #if NET_2_0
  175. catch (ThreadAbortException ex) {
  176. Thread.ResetAbort ();
  177. Assert.IsNull (ex.InnerException, "#2");
  178. }
  179. #else
  180. catch (TargetInvocationException ex) {
  181. Thread.ResetAbort ();
  182. Assert.IsNotNull (ex.InnerException, "#2");
  183. Assert.AreEqual (typeof (ThreadAbortException), ex.InnerException.GetType (), "#3");
  184. }
  185. #endif
  186. }
  187. public static void AbortIt ()
  188. {
  189. Thread.CurrentThread.Abort ();
  190. }
  191. [Test] // bug #76541
  192. public void ToStringByRef ()
  193. {
  194. Assert.AreEqual ("Void HeyHey(System.String ByRef, System.String ByRef)",
  195. this.GetType ().GetMethod ("HeyHey").ToString ());
  196. }
  197. [Test]
  198. public void ToStringArgList ()
  199. {
  200. Assert.AreEqual ("Void SignatureTest(...)",
  201. this.GetType ().GetMethod ("SignatureTest").ToString ());
  202. }
  203. [Test]
  204. public void ToStringWithPointerSignatures () //bug #409583
  205. {
  206. Assert.AreEqual ("Int32* PtrFunc(Int32*)", this.GetType ().GetMethod ("PtrFunc").ToString ());
  207. }
  208. #if NET_2_0
  209. public struct SimpleStruct
  210. {
  211. int a;
  212. }
  213. public static unsafe SimpleStruct* PtrFunc2 (SimpleStruct* a, A.B.C.MethodInfoTestStruct *b)
  214. {
  215. return (SimpleStruct*) 0;
  216. }
  217. [Test]
  218. public void ToStringWithPointerSignaturesToNonPrimitiveType ()
  219. {
  220. Assert.AreEqual ("SimpleStruct* PtrFunc2(SimpleStruct*, A.B.C.MethodInfoTestStruct*)",
  221. this.GetType ().GetMethod ("PtrFunc2").ToString ());
  222. }
  223. [Test]
  224. public void ToStringGenericMethod ()
  225. {
  226. Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](T[])",
  227. typeof (Array).GetMethod ("AsReadOnly").ToString ());
  228. }
  229. #endif
  230. class GBD_A { public virtual void f () {} }
  231. class GBD_B : GBD_A { public override void f () {} }
  232. class GBD_C : GBD_B { public override void f () {} }
  233. class GBD_D : GBD_C { public new virtual void f () {} }
  234. class GBD_E : GBD_D { public override void f () {} }
  235. [Test]
  236. public void GetBaseDefinition ()
  237. {
  238. Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
  239. Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
  240. Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
  241. }
  242. class TestInheritedMethodA {
  243. private void TestMethod ()
  244. {
  245. }
  246. public void TestMethod2 ()
  247. {
  248. }
  249. }
  250. class TestInheritedMethodB : TestInheritedMethodA {
  251. }
  252. [Test]
  253. public void InheritanceTestGetMethodTest ()
  254. {
  255. MethodInfo inheritedMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  256. MethodInfo baseMethod = typeof(TestInheritedMethodB).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  257. Assert.AreSame (inheritedMethod, baseMethod);
  258. MethodInfo inheritedMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  259. MethodInfo baseMethod2 = typeof(TestInheritedMethodB).GetMethod("TestMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  260. Assert.AreSame (inheritedMethod, baseMethod);
  261. }
  262. #if NET_2_0
  263. #if !TARGET_JVM // MethodBody is not supported for TARGET_JVM
  264. [Test]
  265. public void GetMethodBody_Abstract ()
  266. {
  267. MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
  268. Assert.IsNull (mb);
  269. }
  270. [Test]
  271. public void GetMethodBody_Runtime ()
  272. {
  273. MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
  274. Assert.IsNull (mb);
  275. }
  276. [Test]
  277. public void GetMethodBody_Pinvoke ()
  278. {
  279. MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
  280. Assert.IsNull (mb);
  281. }
  282. [Test]
  283. public void GetMethodBody_Icall ()
  284. {
  285. foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
  286. if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
  287. MethodBody mb = mi.GetMethodBody ();
  288. Assert.IsNull (mb);
  289. }
  290. }
  291. public static void locals_method ()
  292. {
  293. byte[] b = new byte [10];
  294. unsafe {
  295. /* This generates a pinned local */
  296. fixed (byte *p = &b [0]) {
  297. }
  298. }
  299. }
  300. [Test]
  301. public void GetMethodBody ()
  302. {
  303. MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
  304. Assert.IsTrue (mb.InitLocals, "#1");
  305. Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
  306. IList<LocalVariableInfo> locals = mb.LocalVariables;
  307. // This might break with different compilers etc.
  308. Assert.AreEqual (2, locals.Count, "#3");
  309. Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
  310. if (locals [0].LocalType == typeof (byte[]))
  311. Assert.AreEqual (false, locals [0].IsPinned, "#5");
  312. else
  313. Assert.AreEqual (false, locals [1].IsPinned, "#6");
  314. }
  315. #endif // TARGET_JVM
  316. public int return_parameter_test ()
  317. {
  318. return 0;
  319. }
  320. [Test]
  321. public void GetMethodFromHandle_Generic ()
  322. {
  323. MethodHandleTest<int> test = new MethodHandleTest<int> ();
  324. RuntimeMethodHandle mh = test.GetType ().GetProperty ("MyList")
  325. .GetGetMethod ().MethodHandle;
  326. MethodBase mb = MethodInfo.GetMethodFromHandle (mh,
  327. typeof (MethodHandleTest<int>).TypeHandle);
  328. Assert.IsNotNull (mb, "#1");
  329. List<int> list = (List<int>) mb.Invoke (test, null);
  330. Assert.IsNotNull (list, "#2");
  331. Assert.AreEqual (1, list.Count, "#3");
  332. }
  333. [Test]
  334. public void ReturnParameter ()
  335. {
  336. ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
  337. Assert.AreEqual (typeof (int), pi.ParameterType, "#1");
  338. Assert.AreEqual (-1, pi.Position, "#2");
  339. // MS always return false here
  340. //Assert.IsTrue (pi.IsRetval, "#3");
  341. }
  342. #if !TARGET_JVM // ReflectionOnly is not supported yet on TARGET_JVM
  343. [Test]
  344. public void InvokeOnRefOnlyAssembly ()
  345. {
  346. Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
  347. Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
  348. MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
  349. try {
  350. m.Invoke (null, new object [0]);
  351. Assert.Fail ("#1");
  352. } catch (InvalidOperationException ex) {
  353. // The requested operation is invalid in the
  354. // ReflectionOnly context
  355. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  356. Assert.IsNull (ex.InnerException, "#3");
  357. Assert.IsNotNull (ex.Message, "#4");
  358. }
  359. }
  360. #endif // TARGET_JVM
  361. [Test]
  362. [ExpectedException (typeof (TargetInvocationException))]
  363. public void InvokeInvalidOpExceptionThrow () {
  364. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ThrowMethod");
  365. mi.Invoke(null, null);
  366. }
  367. public static void ThrowMethod () {
  368. throw new InvalidOperationException ();
  369. }
  370. [Test]
  371. public void InvokeGenericVtype ()
  372. {
  373. KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
  374. Type type = kvp.GetType ();
  375. Type [] arguments = type.GetGenericArguments ();
  376. MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
  377. MethodInfo generic_method = method.MakeGenericMethod (arguments);
  378. kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
  379. Assert.AreEqual ("a", kvp.Key, "#1");
  380. Assert.AreEqual (21, kvp.Value, "#2");
  381. }
  382. public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
  383. {
  384. return kvp;
  385. }
  386. [Test] // bug #81997
  387. public void InvokeGenericInst ()
  388. {
  389. List<string> str = null;
  390. object [] methodArgs = new object [] { str };
  391. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
  392. mi.Invoke (null, methodArgs);
  393. Assert.IsNotNull (methodArgs [0], "#A1");
  394. Assert.IsNull (str, "#A2");
  395. Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
  396. List<string> refStr = methodArgs [0] as List<string>;
  397. Assert.IsNotNull (refStr, "#B1");
  398. Assert.AreEqual (1, refStr.Count, "#B2");
  399. Assert.AreEqual ("test", refStr [0], "#B3");
  400. }
  401. public static void GenericRefMethod (ref List<string> strArg)
  402. {
  403. strArg = new List<string> ();
  404. strArg.Add ("test");
  405. }
  406. public void MakeGenericMethodArgsMismatchFoo<T> ()
  407. {
  408. }
  409. [Test]
  410. public void MakeGenericMethodArgsMismatch ()
  411. {
  412. MethodInfo gmi = this.GetType ().GetMethod (
  413. "MakeGenericMethodArgsMismatchFoo");
  414. try {
  415. gmi.MakeGenericMethod ();
  416. Assert.Fail ("#1");
  417. } catch (ArgumentException ex) {
  418. // The type or method has 1 generic parameter(s),
  419. // but 0 generic argument(s) were provided. A
  420. // generic argument must be provided for each
  421. // generic parameter
  422. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  423. Assert.IsNull (ex.InnerException, "#3");
  424. Assert.IsNotNull (ex.Message, "#4");
  425. Assert.IsNull (ex.ParamName, "#5");
  426. }
  427. }
  428. public void SimpleGenericMethod<TFoo, TBar> () {}
  429. [Test]
  430. public void MakeGenericMethodWithNullArray ()
  431. {
  432. MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
  433. try {
  434. gmi.MakeGenericMethod ((Type []) null);
  435. Assert.Fail ("#1");
  436. } catch (ArgumentNullException ex) {
  437. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  438. Assert.IsNull (ex.InnerException, "#3");
  439. Assert.IsNotNull (ex.Message, "#4");
  440. Assert.AreEqual ("methodInstantiation", ex.ParamName, "#5");
  441. }
  442. }
  443. [Test]
  444. public void MakeGenericMethodWithNullValueInTypesArray ()
  445. {
  446. MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
  447. try {
  448. gmi.MakeGenericMethod (new Type [] { typeof (int), null });
  449. Assert.Fail ("#1");
  450. } catch (ArgumentNullException ex) {
  451. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  452. Assert.IsNull (ex.InnerException, "#3");
  453. Assert.IsNotNull (ex.Message, "#4");
  454. Assert.IsNull (ex.ParamName, "#5");
  455. }
  456. }
  457. [Test]
  458. public void MakeGenericMethodWithNonGenericMethodDefinitionMethod ()
  459. {
  460. MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod");
  461. MethodInfo inst = gmi.MakeGenericMethod (typeof (int), typeof (double));
  462. try {
  463. inst.MakeGenericMethod (typeof (int), typeof (double));
  464. Assert.Fail ("#1");
  465. } catch (InvalidOperationException ex) {
  466. }
  467. }
  468. public TFoo SimpleGenericMethod2<TFoo, TBar> () { return default (TFoo); }
  469. /*Test for the uggly broken behavior of SRE.*/
  470. [Test]
  471. public void MakeGenericMethodWithSreTypeResultsInStupidMethodInfo ()
  472. {
  473. AssemblyName assemblyName = new AssemblyName ();
  474. assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodInfoTest";
  475. AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
  476. ModuleBuilder module = assembly.DefineDynamicModule ("module1", "tst.dll");
  477. TypeBuilder tb = module.DefineType ("Test", TypeAttributes.Public);
  478. MethodInfo gmi = this.GetType ().GetMethod ("SimpleGenericMethod2");
  479. MethodInfo ins = gmi.MakeGenericMethod (typeof (int), tb);
  480. Assert.AreSame (tb, ins.GetGenericArguments () [1], "#1");
  481. /*broken ReturnType*/
  482. Assert.AreSame (gmi.GetGenericArguments () [0], ins.ReturnType, "#2");
  483. }
  484. public static int? pass_nullable (int? i)
  485. {
  486. return i;
  487. }
  488. [Test]
  489. public void NullableTests ()
  490. {
  491. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
  492. Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
  493. Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
  494. // Test conversion of vtype to a nullable type for the this argument
  495. PropertyInfo pi = typeof (Nullable <int>).GetProperty ("HasValue");
  496. Assert.AreEqual (true, pi.GetGetMethod ().Invoke (10, null));
  497. PropertyInfo pi2 = typeof (Nullable <int>).GetProperty ("Value");
  498. Assert.AreEqual (10, pi2.GetGetMethod ().Invoke (10, null));
  499. }
  500. public static void foo_generic<T> ()
  501. {
  502. }
  503. [Test]
  504. public void IsGenericMethod ()
  505. {
  506. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
  507. Assert.AreEqual (true, mi.IsGenericMethod, "#1");
  508. MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
  509. Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
  510. MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
  511. Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
  512. }
  513. class A<T>
  514. {
  515. public static void Foo<T2> (T2 i)
  516. {
  517. }
  518. public static void Bar ()
  519. {
  520. }
  521. public class B
  522. {
  523. public static void Baz ()
  524. {
  525. }
  526. }
  527. }
  528. [Test]
  529. public void ContainsGenericParameters ()
  530. {
  531. // Non-generic method in open generic type
  532. Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
  533. // open generic method in closed generic type
  534. Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
  535. // non-generic method in closed generic type
  536. Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
  537. // closed generic method in closed generic type
  538. Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
  539. // non-generic method in non-generic nested type of closed generic type
  540. Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
  541. // non-generic method in non-generic nested type of open generic type
  542. Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
  543. }
  544. [Test]
  545. public void IsGenericMethodDefinition ()
  546. {
  547. MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
  548. Assert.IsTrue (m1.IsGenericMethod, "#A1");
  549. Assert.IsTrue (m1.IsGenericMethodDefinition, "#A2");
  550. MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
  551. Assert.IsTrue (m2.IsGenericMethod, "#B1");
  552. Assert.IsTrue (m2.IsGenericMethodDefinition, "#B2");
  553. MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
  554. Assert.IsTrue (m3.IsGenericMethod, "#C1");
  555. Assert.IsFalse (m3.IsGenericMethodDefinition, "#C2");
  556. }
  557. [Test]
  558. public void GetGenericMethodDefinition ()
  559. {
  560. MethodInfo mi1 = typeof (MyList<>).GetMethod ("ConvertAll");
  561. MethodInfo mi2 = typeof (MyList<int>).GetMethod ("ConvertAll");
  562. Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[T,TOutput]",
  563. mi1.GetParameters () [0].ParameterType.ToString (), "#A1");
  564. Assert.AreEqual ("MonoTests.System.Reflection.MethodInfoTest+Foo`2[System.Int32,TOutput]",
  565. mi2.GetParameters () [0].ParameterType.ToString (), "#A2");
  566. Assert.IsTrue (mi1.IsGenericMethod, "#A3");
  567. Assert.IsTrue (mi1.IsGenericMethodDefinition, "#A4");
  568. Assert.IsTrue (mi2.IsGenericMethod, "#A5");
  569. Assert.IsTrue (mi2.IsGenericMethodDefinition, "#A6");
  570. MethodInfo mi3 = mi2.GetGenericMethodDefinition ();
  571. Assert.IsTrue (mi3.IsGenericMethod, "#B1");
  572. Assert.IsTrue (mi3.IsGenericMethodDefinition, "#B2");
  573. Assert.AreSame (mi2, mi3, "#B3");
  574. MethodInfo mi4 = mi2.MakeGenericMethod (typeof (short));
  575. Assert.IsTrue (mi4.IsGenericMethod, "#C1");
  576. Assert.IsFalse (mi4.IsGenericMethodDefinition, "#C2");
  577. Assert.AreSame (mi2, mi4.GetGenericMethodDefinition (), "#C3");
  578. }
  579. public void TestMethod123(int a, int b) {}
  580. [Test]
  581. public void GetParametersDontReturnInternedArray ()
  582. {
  583. var method = typeof (MethodInfoTest).GetMethod ("TestMethod123");
  584. var parms = method.GetParameters ();
  585. Assert.AreNotSame (parms, method.GetParameters (), "#1");
  586. parms [0] = null;
  587. Assert.IsNotNull (method.GetParameters () [0], "#2");
  588. }
  589. [Test]
  590. public void Bug354757 ()
  591. {
  592. MethodInfo gmd = (typeof (MyList <int>)).GetMethod ("ConvertAll");
  593. MethodInfo oi = gmd.MakeGenericMethod (gmd.GetGenericArguments ());
  594. Assert.AreSame (gmd, oi);
  595. }
  596. public class MyList<T>
  597. {
  598. public TOutput ConvertAll<TOutput> (Foo<T,TOutput> arg)
  599. {
  600. return default (TOutput);
  601. }
  602. public T ConvertAll2 (MyList<T> arg)
  603. {
  604. return default (T);
  605. }
  606. }
  607. public class Foo<T,TOutput>
  608. {
  609. }
  610. class GenericHelper<T>
  611. {
  612. public void Test (T t)
  613. {
  614. }
  615. }
  616. #endif
  617. #if NET_4_0
  618. interface IMethodInvoke<out T>
  619. {
  620. T Test ();
  621. }
  622. class MethodInvoke : IMethodInvoke<object>
  623. {
  624. public object Test ()
  625. {
  626. return "MethodInvoke";
  627. }
  628. }
  629. [Test]
  630. public void GetInterfaceMapWorksWithVariantIfaces ()
  631. {
  632. var m0 = typeof (IMethodInvoke<object>).GetMethod ("Test");
  633. var m1 = typeof (IMethodInvoke<string>).GetMethod ("Test");
  634. var obj = new MethodInvoke ();
  635. Assert.AreEqual ("MethodInvoke", m0.Invoke (obj, new Object [0]));
  636. Assert.AreEqual ("MethodInvoke", m1.Invoke (obj, new Object [0]));
  637. }
  638. #endif
  639. }
  640. #if NET_2_0
  641. // Helper class
  642. class RefOnlyMethodClass
  643. {
  644. // Helper static method
  645. static void RefOnlyMethod ()
  646. {
  647. }
  648. }
  649. public class MethodHandleTest<T>
  650. {
  651. private List<T> _myList = new List<T> ();
  652. public MethodHandleTest ()
  653. {
  654. _myList.Add (default (T));
  655. }
  656. public List<T> MyList {
  657. get { return _myList; }
  658. set { _myList = value; }
  659. }
  660. }
  661. #endif
  662. }