MethodInfoTest.cs 11 KB

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