MethodInfoTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 : Assertion
  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. AssertEquals (CallingConvention.Winapi, attr.CallingConvention);
  58. AssertEquals ("foo", attr.EntryPoint);
  59. AssertEquals ("libfoo", attr.Value);
  60. AssertEquals (CharSet.Unicode, attr.CharSet);
  61. AssertEquals (false, attr.ExactSpelling);
  62. AssertEquals (true, attr.PreserveSig);
  63. AssertEquals (true, attr.SetLastError);
  64. AssertEquals (true, attr.BestFitMapping);
  65. AssertEquals (true, attr.ThrowOnUnmappableChar);
  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. #endif
  73. public static int foo (int i, int j)
  74. {
  75. return i + j;
  76. }
  77. [Test]
  78. public void StaticInvokeWithObject ()
  79. {
  80. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("foo");
  81. mi.Invoke (new Object (), new object [] { 1, 2 });
  82. }
  83. [Test]
  84. public void ByRefInvoke ()
  85. {
  86. MethodInfo met = typeof(MethodInfoTest).GetMethod ("ByRefTest");
  87. object[] parms = new object[] {1};
  88. met.Invoke (null, parms);
  89. AssertEquals (2, parms [0]);
  90. }
  91. public static void ByRefTest (ref int a1)
  92. {
  93. if (a1 == 1)
  94. a1 = 2;
  95. }
  96. public void HeyHey (out string out1, ref string ref1)
  97. {
  98. out1 = null;
  99. }
  100. [Test] // bug #76541
  101. public void ToStringByRef ()
  102. {
  103. AssertEquals ("Void HeyHey(System.String ByRef, System.String ByRef)",
  104. this.GetType ().GetMethod ("HeyHey").ToString ());
  105. }
  106. #if NET_2_0
  107. [Test]
  108. [ExpectedException (typeof (ArgumentException))]
  109. public void GetMethodBody_Abstract () {
  110. typeof (ICloneable).GetMethod ("Clone").GetMethodBody ();
  111. }
  112. [Test]
  113. [ExpectedException (typeof (ArgumentException))]
  114. public void GetMethodBody_Runtime () {
  115. typeof (AsyncCallback).GetMethod ("Invoke").GetMethodBody ();
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. public void GetMethodBody_Pinvoke () {
  120. typeof (MethodInfoTest).GetMethod ("dllImportMethod").GetMethodBody ();
  121. }
  122. [Test]
  123. [ExpectedException (typeof (ArgumentException))]
  124. public void GetMethodBody_Icall () {
  125. foreach (MethodInfo mi in typeof (object).GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance))
  126. if ((mi.GetMethodImplementationFlags () & MethodImplAttributes.InternalCall) != 0)
  127. mi.GetMethodBody ();
  128. }
  129. public static void locals_method () {
  130. byte[] b = new byte [10];
  131. unsafe {
  132. /* This generates a pinned local */
  133. fixed (byte *p = &b [0]) {
  134. }
  135. }
  136. }
  137. [Test]
  138. public void GetMethodBody () {
  139. MethodBody mb = typeof (MethodInfoTest).GetMethod ("locals_method").GetMethodBody ();
  140. Assert (mb.InitLocals);
  141. Assert (mb.LocalSignatureMetadataToken > 0);
  142. IList<LocalVariableInfo> locals = mb.LocalVariables;
  143. // This might break with different compilers etc.
  144. AssertEquals (2, locals.Count);
  145. Assert ((locals [0].LocalType == typeof (byte[])) || (locals [1].LocalType == typeof (byte[])));
  146. if (locals [0].LocalType == typeof (byte[]))
  147. AssertEquals (false, locals [0].IsPinned);
  148. else
  149. AssertEquals (false, locals [1].IsPinned);
  150. }
  151. [Test]
  152. [ExpectedException (typeof (InvalidOperationException))]
  153. public void InvokeOnRefOnlyAssembly ()
  154. {
  155. Assembly a = Assembly.ReflectionOnlyLoad (typeof (MethodInfoTest).Assembly.FullName);
  156. Type t = a.GetType (typeof (RefOnlyMethodClass).FullName);
  157. MethodInfo m = t.GetMethod ("RefOnlyMethod", BindingFlags.Static | BindingFlags.NonPublic);
  158. m.Invoke (null, new object [0]);
  159. }
  160. public void MakeGenericMethodArgsMismatchFoo<T> () {}
  161. [Test]
  162. [ExpectedException (typeof (ArgumentException))]
  163. public void MakeGenericMethodArgsMismatch ()
  164. {
  165. MethodInfo gmi = this.GetType ().GetMethod (
  166. "MakeGenericMethodArgsMismatchFoo")
  167. .MakeGenericMethod ();
  168. }
  169. public static int? pass_nullable (int? i)
  170. {
  171. return i;
  172. }
  173. [Test]
  174. public void NullableTests ()
  175. {
  176. MethodInfo mi = typeof (MethodInfoTest).GetMethod ("pass_nullable");
  177. AssertEquals (102, mi.Invoke (null, new object [] { 102 }));
  178. AssertEquals (null, mi.Invoke (null, new object [] { null }));
  179. }
  180. #endif
  181. }
  182. #if NET_2_0
  183. // Helper class
  184. class RefOnlyMethodClass
  185. {
  186. // Helper static method
  187. static void RefOnlyMethod ()
  188. {
  189. }
  190. }
  191. #endif
  192. }