MethodInfoTest.cs 11 KB

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