PageRenderTime 55ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Reflection/MethodInfoTest.cs

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