MethodBaseTest.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // System.Reflection.MethodBase Test Cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // Copyright (C) 2008 Gert Driesen
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Reflection;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Reflection
  32. {
  33. public class Generic<T> {
  34. public void Foo () {
  35. }
  36. public void GenericFoo<K> () {
  37. }
  38. }
  39. public class AnotherGeneric<T> {
  40. public void Foo () {
  41. }
  42. }
  43. public class SimpleClass {
  44. public void GenericFoo<K> () {
  45. }
  46. }
  47. [TestFixture]
  48. public class MethodBaseTest
  49. {
  50. public static MethodInfo Where<T> (T a) {
  51. return (MethodInfo) MethodBase.GetCurrentMethod ();
  52. }
  53. public class Foo<K>
  54. {
  55. public static MethodInfo Where<T> (T a, K b) {
  56. return (MethodInfo) MethodBase.GetCurrentMethod ();
  57. }
  58. }
  59. [Test]
  60. public void GetCurrentMethodDropsAllGenericArguments ()
  61. {
  62. MethodInfo a = Where<int> (10);
  63. MethodInfo b = Foo<int>.Where <double> (10, 10);
  64. Assert.IsTrue (a.IsGenericMethodDefinition, "#1");
  65. Assert.IsTrue (b.IsGenericMethodDefinition, "#2");
  66. Assert.IsTrue (b.DeclaringType.IsGenericTypeDefinition, "#3");
  67. Assert.AreSame (a, typeof (MethodBaseTest).GetMethod ("Where"), "#4");
  68. Assert.AreSame (b, typeof (Foo<>).GetMethod ("Where"), "#5");
  69. }
  70. [Test] // GetMethodFromHandle (RuntimeMethodHandle)
  71. public void GetMethodFromHandle1_Handle_Generic ()
  72. {
  73. G<string> instance = new G<string> ();
  74. Type t = instance.GetType ();
  75. MethodBase mb1 = t.GetMethod ("M");
  76. RuntimeMethodHandle mh = mb1.MethodHandle;
  77. RuntimeTypeHandle th = t.TypeHandle;
  78. try {
  79. MethodBase.GetMethodFromHandle (mh);
  80. Assert.Fail ("#1");
  81. } catch (ArgumentException ex) {
  82. // Cannot resolve method Void M(System.__Canon)
  83. // because the declaring type of the method
  84. // handle MonoTests.System.Reflection.MethodBaseTest+G`1[T]
  85. // is generic. Explicitly provide the declaring type to
  86. // GetMethodFromHandle
  87. }
  88. }
  89. [Test] // GetMethodFromHandle (RuntimeMethodHandle)
  90. public void GetMethodFromHandle1_Handle_Zero ()
  91. {
  92. RuntimeMethodHandle mh = new RuntimeMethodHandle ();
  93. try {
  94. MethodBase.GetMethodFromHandle (mh);
  95. Assert.Fail ("#1");
  96. } catch (ArgumentException ex) {
  97. // Handle is not initialized
  98. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  99. Assert.IsNull (ex.InnerException, "#3");
  100. Assert.IsNotNull (ex.Message, "#4");
  101. Assert.IsNull (ex.ParamName, "#5");
  102. }
  103. }
  104. [Test]
  105. public void GetMethodFromHandle ()
  106. {
  107. Type t = typeof (object);
  108. RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
  109. MethodBase mb = MethodBase.GetMethodFromHandle (rmh);
  110. Assert.IsNotNull (mb, "#1");
  111. Assert.AreEqual (t, mb.DeclaringType, "#2");
  112. Assert.AreEqual (".ctor", mb.Name, "#3");
  113. ParameterInfo [] parameters = mb.GetParameters ();
  114. Assert.IsNotNull (parameters, "#4");
  115. Assert.AreEqual (0, parameters.Length, "#5");
  116. }
  117. [Test]
  118. public void GetMethodFromHandle_NonGenericType_DeclaringTypeZero ()
  119. {
  120. Type t = typeof (object);
  121. RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
  122. MethodBase mb = MethodBase.GetMethodFromHandle (rmh, new RuntimeTypeHandle ());
  123. Assert.IsNotNull (mb, "#1");
  124. Assert.AreEqual (t, mb.DeclaringType, "#2");
  125. Assert.AreEqual (".ctor", mb.Name, "#3");
  126. ParameterInfo [] parameters = mb.GetParameters ();
  127. Assert.IsNotNull (parameters, "#4");
  128. Assert.AreEqual (0, parameters.Length, "#5");
  129. }
  130. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  131. public void GetMethodFromHandle2_DeclaringType_Zero ()
  132. {
  133. RuntimeTypeHandle th = new RuntimeTypeHandle ();
  134. Type t = typeof (G<>);
  135. RuntimeMethodHandle mh = t.GetMethod ("M").MethodHandle;
  136. MethodBase mb = MethodBase.GetMethodFromHandle (mh, th);
  137. Assert.IsNotNull (mb, "#1");
  138. Assert.AreEqual (t, mb.DeclaringType, "#2");
  139. Assert.AreEqual ("M", mb.Name, "#3");
  140. ParameterInfo [] parameters = mb.GetParameters ();
  141. Assert.IsNotNull (parameters, "#4");
  142. Assert.AreEqual (1, parameters.Length, "#5");
  143. Assert.AreEqual (t.GetGenericArguments () [0] , parameters [0].ParameterType, "#6");
  144. }
  145. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  146. public void GetMethodFromHandle2_Handle_Generic ()
  147. {
  148. G<string> instance = new G<string> ();
  149. Type t = instance.GetType ();
  150. MethodBase mb1 = t.GetMethod ("M");
  151. RuntimeMethodHandle mh = mb1.MethodHandle;
  152. RuntimeTypeHandle th = t.TypeHandle;
  153. MethodBase mb2 = MethodBase.GetMethodFromHandle (mh, th);
  154. Assert.IsNotNull (mb2, "#1");
  155. Assert.AreEqual (t, mb2.DeclaringType, "#2");
  156. Assert.AreEqual ("M", mb2.Name, "#3");
  157. ParameterInfo [] parameters = mb2.GetParameters ();
  158. Assert.IsNotNull (parameters, "#4");
  159. Assert.AreEqual (1, parameters.Length, "#5");
  160. Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#6");
  161. }
  162. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  163. public void GetMethodFromHandle2_Handle_Zero ()
  164. {
  165. RuntimeTypeHandle th = typeof (G<>).TypeHandle;
  166. RuntimeMethodHandle mh = new RuntimeMethodHandle ();
  167. try {
  168. MethodBase.GetMethodFromHandle (mh, th);
  169. Assert.Fail ("#1");
  170. } catch (ArgumentException ex) {
  171. // Handle is not initialized
  172. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  173. Assert.IsNull (ex.InnerException, "#3");
  174. Assert.IsNotNull (ex.Message, "#4");
  175. Assert.IsNull (ex.ParamName, "#5");
  176. }
  177. }
  178. public class G<T>
  179. {
  180. public void M (T t)
  181. {
  182. }
  183. }
  184. [Test]
  185. public void GetMethodFromHandle_Handle_Generic_Method ()
  186. {
  187. MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo");
  188. RuntimeMethodHandle handle = mi.MethodHandle;
  189. MethodBase res = MethodBase.GetMethodFromHandle (handle);
  190. Assert.AreEqual (mi, res, "#1");
  191. res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
  192. Assert.AreEqual (mi, res, "#2");
  193. }
  194. [Test]
  195. public void GetMethodFromHandle_Handle_Generic_Method_Instance ()
  196. {
  197. MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  198. RuntimeMethodHandle handle = mi.MethodHandle;
  199. MethodBase res = MethodBase.GetMethodFromHandle (handle);
  200. Assert.AreEqual (mi, res, "#1");
  201. res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
  202. Assert.AreEqual (mi, res, "#2");
  203. }
  204. [Test]
  205. public void GetMethodFromHandle_Handle_Generic_Method_On_Generic_Class ()
  206. {
  207. MethodInfo mi = typeof (Generic<>).GetMethod ("GenericFoo");
  208. RuntimeMethodHandle handle = mi.MethodHandle;
  209. MethodBase res;
  210. try {
  211. MethodBase.GetMethodFromHandle (handle);
  212. Assert.Fail ("#1");
  213. } catch (ArgumentException) {
  214. }
  215. mi = typeof (Generic<int>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  216. handle = mi.MethodHandle;
  217. try {
  218. MethodBase.GetMethodFromHandle (handle);
  219. Assert.Fail ("#2");
  220. } catch (ArgumentException) {
  221. }
  222. mi = typeof (Generic<>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  223. handle = mi.MethodHandle;
  224. try {
  225. MethodBase.GetMethodFromHandle (handle);
  226. Assert.Fail ("#3");
  227. } catch (ArgumentException) {
  228. }
  229. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
  230. Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
  231. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
  232. Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
  233. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  234. Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
  235. try {
  236. MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
  237. Assert.Fail ("#7");
  238. } catch (ArgumentException) {
  239. }
  240. }
  241. [Test]
  242. public void GetMethodFromHandle_Handle_Method_On_Generic_Class ()
  243. {
  244. MethodInfo mi = typeof (Generic<>).GetMethod ("Foo");
  245. RuntimeMethodHandle handle = mi.MethodHandle;
  246. MethodBase res;
  247. try {
  248. MethodBase.GetMethodFromHandle(handle);
  249. Assert.Fail ("#1");
  250. } catch (ArgumentException) {
  251. }
  252. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  253. Assert.AreEqual (res, mi, "#2");
  254. mi = typeof (Generic<int>).GetMethod ("Foo");
  255. handle = mi.MethodHandle;
  256. try {
  257. MethodBase.GetMethodFromHandle(handle);
  258. Assert.Fail ("#3");
  259. } catch (ArgumentException) {
  260. }
  261. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
  262. Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
  263. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
  264. Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
  265. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  266. Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
  267. try {
  268. MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
  269. Assert.Fail ("#7");
  270. } catch (ArgumentException) {
  271. }
  272. }
  273. }
  274. }