MethodInfoTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.Runtime.InteropServices;
  34. using System.Runtime.CompilerServices;
  35. #if NET_2_0
  36. using System.Collections.Generic;
  37. #endif
  38. namespace MonoTests.System.Reflection
  39. {
  40. [TestFixture]
  41. public class MethodInfoTest
  42. {
  43. #if !TARGET_JVM
  44. [DllImport ("libfoo", EntryPoint="foo", CharSet=CharSet.Unicode, ExactSpelling=false, PreserveSig=true, SetLastError=true, BestFitMapping=true, ThrowOnUnmappableChar=true)]
  45. public static extern void dllImportMethod ();
  46. #endif
  47. [MethodImplAttribute(MethodImplOptions.PreserveSig)]
  48. public void preserveSigMethod ()
  49. {
  50. }
  51. [MethodImplAttribute(MethodImplOptions.Synchronized)]
  52. public void synchronizedMethod ()
  53. {
  54. }
  55. #if NET_2_0
  56. [Test]
  57. public void PseudoCustomAttributes ()
  58. {
  59. Type t = typeof (MethodInfoTest);
  60. DllImportAttribute attr = (DllImportAttribute)((t.GetMethod ("dllImportMethod").GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
  61. Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
  62. Assert.AreEqual ("foo", attr.EntryPoint, "#2");
  63. Assert.AreEqual ("libfoo", attr.Value, "#3");
  64. Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#4");
  65. Assert.AreEqual (false, attr.ExactSpelling, "#5");
  66. Assert.AreEqual (true, attr.PreserveSig, "#6");
  67. Assert.AreEqual (true, attr.SetLastError, "#7");
  68. Assert.AreEqual (true, attr.BestFitMapping, "#8");
  69. Assert.AreEqual (true, attr.ThrowOnUnmappableChar, "#9");
  70. PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod ("preserveSigMethod").GetCustomAttributes (true)) [0]);
  71. // This doesn't work under MS.NET
  72. /*
  73. MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]);
  74. */
  75. }
  76. [return: MarshalAs (UnmanagedType.Interface)]
  77. public void ReturnTypeMarshalAs ()
  78. {
  79. }
  80. [Test]
  81. [Category ("TargetJvmNotWorking")]
  82. public void ReturnTypePseudoCustomAttributes ()
  83. {
  84. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ReturnTypeMarshalAs");
  85. Assert.IsTrue (mi.ReturnTypeCustomAttributes.GetCustomAttributes (typeof (MarshalAsAttribute), true).Length == 1);
  86. }
  87. #endif
  88. public static int foo (int i, int j)
  89. {
  90. return i + j;
  91. }
  92. [Test]
  93. public void StaticInvokeWithObject ()
  94. {
  95. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
  96. mi.Invoke (new Object (), new object [] { 1, 2 });
  97. }
  98. [Test]
  99. public void ByRefInvoke ()
  100. {
  101. MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
  102. object[] parms = new object[] {1};
  103. met.Invoke (null, parms);
  104. Assert.AreEqual (2, parms[0]);
  105. }
  106. public static void ByRefTest (ref int a1)
  107. {
  108. if (a1 == 1)
  109. a1 = 2;
  110. }
  111. static int byref_arg;
  112. public static void ByrefVtype (ref int i) {
  113. byref_arg = i;
  114. i = 5;
  115. }
  116. [Test]
  117. #if ONLY_1_1
  118. [Category ("NotDotNet")] // #A2 fails on MS.NET 1.x
  119. #endif
  120. public void ByrefVtypeInvoke ()
  121. {
  122. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("ByrefVtype");
  123. object o = 1;
  124. object[] args = new object [] { o };
  125. mi.Invoke (null, args);
  126. Assert.AreEqual (1, byref_arg, "#A1");
  127. Assert.AreEqual (1, o, "#A2");
  128. Assert.AreEqual (5, args[0], "#A3");
  129. args [0] = null;
  130. mi.Invoke (null, args);
  131. Assert.AreEqual (0, byref_arg, "#B1");
  132. Assert.AreEqual (5, args[0], "#B2");
  133. }
  134. public void HeyHey (out string out1, ref string ref1)
  135. {
  136. out1 = null;
  137. }
  138. [Test] // bug #81538
  139. public void InvokeThreadAbort ()
  140. {
  141. MethodInfo method = typeof (MethodInfoTest).GetMethod ("AbortIt");
  142. try {
  143. method.Invoke (null, new object [0]);
  144. Assert.Fail ("#1");
  145. }
  146. #if NET_2_0
  147. catch (ThreadAbortException ex) {
  148. Thread.ResetAbort ();
  149. Assert.IsNull (ex.InnerException, "#2");
  150. }
  151. #else
  152. catch (TargetInvocationException ex) {
  153. Thread.ResetAbort ();
  154. Assert.IsNotNull (ex.InnerException, "#2");
  155. Assert.AreEqual (typeof (ThreadAbortException), ex.InnerException.GetType (), "#3");
  156. }
  157. #endif
  158. }
  159. public static void AbortIt ()
  160. {
  161. Thread.CurrentThread.Abort ();
  162. }
  163. [Test] // bug #76541
  164. public void ToStringByRef ()
  165. {
  166. Assert.AreEqual ("Void HeyHey(System.String ByRef, System.String ByRef)",
  167. this.GetType ().GetMethod ("HeyHey").ToString ());
  168. }
  169. #if NET_2_0
  170. [Test]
  171. public void ToStringGenericMethod ()
  172. {
  173. Assert.AreEqual ("System.Collections.ObjectModel.ReadOnlyCollection`1[T] AsReadOnly[T](.T[])",
  174. typeof (Array).GetMethod ("AsReadOnly").ToString ());
  175. }
  176. #endif
  177. class GBD_A { public virtual void f () {} }
  178. class GBD_B : GBD_A { public override void f () {} }
  179. class GBD_C : GBD_B { public override void f () {} }
  180. class GBD_D : GBD_C { public new virtual void f () {} }
  181. class GBD_E : GBD_D { public override void f () {} }
  182. [Test]
  183. public void GetBaseDefinition ()
  184. {
  185. Assert.AreEqual (typeof (GBD_A), typeof (GBD_C).GetMethod ("f").GetBaseDefinition ().DeclaringType);
  186. Assert.AreEqual (typeof (GBD_D), typeof (GBD_D).GetMethod ("f").GetBaseDefinition ().DeclaringType);
  187. Assert.AreEqual (typeof (GBD_D), typeof (GBD_E).GetMethod ("f").GetBaseDefinition ().DeclaringType);
  188. }
  189. #if NET_2_0
  190. #if !TARGET_JVM // MethodBody is not supported for TARGET_JVM
  191. [Test]
  192. public void GetMethodBody_Abstract ()
  193. {
  194. MethodBody mb = typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
  195. Assert.IsNull (mb);
  196. }
  197. [Test]
  198. public void GetMethodBody_Runtime ()
  199. {
  200. MethodBody mb = typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
  201. Assert.IsNull (mb);
  202. }
  203. [Test]
  204. public void GetMethodBody_Pinvoke ()
  205. {
  206. MethodBody mb = typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
  207. Assert.IsNull (mb);
  208. }
  209. [Test]
  210. public void GetMethodBody_Icall ()
  211. {
  212. foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
  213. if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0) {
  214. MethodBody mb = mi.GetMethodBody ();
  215. Assert.IsNull (mb);
  216. }
  217. }
  218. public static void locals_method ()
  219. {
  220. byte[] b = new byte [10];
  221. unsafe {
  222. /* This generates a pinned local */
  223. fixed (byte *p = &b [0]) {
  224. }
  225. }
  226. }
  227. [Test]
  228. public void GetMethodBody ()
  229. {
  230. MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
  231. Assert.IsTrue (mb.InitLocals, "#1");
  232. Assert.IsTrue (mb.LocalSignatureMetadataToken > 0, "#2");
  233. IList<LocalVariableInfo> locals = mb.LocalVariables;
  234. // This might break with different compilers etc.
  235. Assert.AreEqual (2, locals.Count, "#3");
  236. Assert.IsTrue ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])), "#4");
  237. if (locals [0].LocalType == typeof (byte[]))
  238. Assert.AreEqual (false, locals [0].IsPinned, "#5");
  239. else
  240. Assert.AreEqual (false, locals [1].IsPinned, "#6");
  241. }
  242. #endif // TARGET_JVM
  243. public int return_parameter_test ()
  244. {
  245. return 0;
  246. }
  247. [Test]
  248. public void ReturnParameter ()
  249. {
  250. ParameterInfo pi = typeof (MethodInfoTest).GetMethod ("return_parameter_test").ReturnParameter;
  251. Assert.AreEqual (typeof (int), pi.ParameterType);
  252. Assert.AreEqual (-1, pi.Position);
  253. // This fails on MS
  254. //Assert.AreEqual (True, pi.IsRetval);
  255. }
  256. #if !TARGET_JVM // ReflectionOnly is not supported yet on TARGET_JVM
  257. [Test]
  258. [ExpectedException (typeof (InvalidOperationException))]
  259. public void InvokeOnRefOnlyAssembly ()
  260. {
  261. Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
  262. Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
  263. MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
  264. m.Invoke (null, new object [0]);
  265. }
  266. #endif // TARGET_JVM
  267. [Test]
  268. public void InvokeGenericVtype ()
  269. {
  270. KeyValuePair<string, uint> kvp = new KeyValuePair<string, uint> ("a", 21);
  271. Type type = kvp.GetType ();
  272. Type [] arguments = type.GetGenericArguments ();
  273. MethodInfo method = typeof (MethodInfoTest).GetMethod ("Go");
  274. MethodInfo generic_method = method.MakeGenericMethod (arguments);
  275. kvp = (KeyValuePair<string, uint>)generic_method.Invoke (null, new object [] { kvp });
  276. Assert.AreEqual ("a", kvp.Key);
  277. Assert.AreEqual (21, kvp.Value);
  278. }
  279. public static KeyValuePair<T1, T2> Go <T1, T2> (KeyValuePair <T1, T2> kvp)
  280. {
  281. return kvp;
  282. }
  283. [Test] // bug #81997
  284. public void InvokeGenericInst ()
  285. {
  286. List<string> str = null;
  287. object [] methodArgs = new object [] { str };
  288. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("GenericRefMethod");
  289. mi.Invoke (null, methodArgs);
  290. Assert.IsNotNull (methodArgs [0], "#A1");
  291. Assert.IsNull (str, "#A2");
  292. Assert.IsTrue (methodArgs [0] is List<string>, "#A3");
  293. List<string> refStr = methodArgs [0] as List<string>;
  294. Assert.IsNotNull (refStr, "#B1");
  295. Assert.AreEqual (1, refStr.Count, "#B2");
  296. Assert.AreEqual ("test", refStr [0], "#B3");
  297. }
  298. public static void GenericRefMethod (ref List<string> strArg)
  299. {
  300. strArg = new List<string> ();
  301. strArg.Add ("test");
  302. }
  303. public void MakeGenericMethodArgsMismatchFoo<T> () {}
  304. [Test]
  305. [ExpectedException (typeof (ArgumentException))]
  306. public void MakeGenericMethodArgsMismatch ()
  307. {
  308. MethodInfo gmi = this.GetType ().GetMethod (
  309. "MakeGenericMethodArgsMismatchFoo")
  310. .MakeGenericMethod ();
  311. }
  312. public static int? pass_nullable (int? i)
  313. {
  314. return i;
  315. }
  316. [Test]
  317. public void NullableTests ()
  318. {
  319. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
  320. Assert.AreEqual (102, mi.Invoke (null, new object [] { 102 }), "#1");
  321. Assert.AreEqual (null, mi.Invoke (null, new object [] { null }), "#2");
  322. }
  323. public static void foo_generic<T> ()
  324. {
  325. }
  326. [Test]
  327. public void IsGenericMethod ()
  328. {
  329. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo_generic");
  330. Assert.AreEqual (true, mi.IsGenericMethod, "#1");
  331. MethodInfo mi2 = mi.MakeGenericMethod (new Type[] { typeof (int) });
  332. Assert.AreEqual (true, mi2.IsGenericMethod, "#2");
  333. MethodInfo mi3 = typeof (GenericHelper<int>).GetMethod ("Test");
  334. Assert.AreEqual (false, mi3.IsGenericMethod, "#3");
  335. }
  336. class A<T>
  337. {
  338. public static void Foo<T2> (T2 i)
  339. {
  340. }
  341. public static void Bar ()
  342. {
  343. }
  344. public class B
  345. {
  346. public static void Baz ()
  347. {
  348. }
  349. }
  350. }
  351. [Test]
  352. public void ContainsGenericParameters ()
  353. {
  354. // Non-generic method in open generic type
  355. Assert.IsTrue (typeof (A<int>).GetGenericTypeDefinition ().GetMethod ("Bar").ContainsGenericParameters);
  356. // open generic method in closed generic type
  357. Assert.IsTrue (typeof (A<int>).GetMethod ("Foo").ContainsGenericParameters);
  358. // non-generic method in closed generic type
  359. Assert.IsFalse (typeof (A<int>).GetMethod ("Bar").ContainsGenericParameters);
  360. // closed generic method in closed generic type
  361. Assert.IsFalse (typeof (A<int>).GetMethod ("Foo").MakeGenericMethod (new Type [] { typeof (int) }).ContainsGenericParameters);
  362. // non-generic method in non-generic nested type of closed generic type
  363. Assert.IsFalse (typeof (A<int>.B).GetMethod ("Baz").ContainsGenericParameters);
  364. // non-generic method in non-generic nested type of open generic type
  365. Assert.IsTrue (typeof (A<int>.B).GetGenericTypeDefinition ().GetMethod ("Baz").ContainsGenericParameters);
  366. }
  367. [Test]
  368. public void IsGenericMethodDefinition ()
  369. {
  370. MethodInfo m1 = typeof (A<>).GetMethod ("Foo");
  371. Assert.IsTrue (m1.IsGenericMethod);
  372. Assert.IsTrue (m1.IsGenericMethodDefinition);
  373. MethodInfo m2 = typeof (A<int>).GetMethod ("Foo");
  374. Assert.IsTrue (m2.IsGenericMethod);
  375. Assert.IsTrue (m2.IsGenericMethodDefinition);
  376. MethodInfo m3 = m2.MakeGenericMethod (typeof (int));
  377. Assert.IsTrue (m3.IsGenericMethod);
  378. Assert.IsFalse (m3.IsGenericMethodDefinition);
  379. }
  380. class GenericHelper<T>
  381. {
  382. public void Test (T t)
  383. { }
  384. }
  385. #endif
  386. }
  387. #if NET_2_0
  388. // Helper class
  389. class RefOnlyMethodClass
  390. {
  391. // Helper static method
  392. static void RefOnlyMethod ()
  393. {
  394. }
  395. }
  396. #endif
  397. }