MethodBaseTest.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #if NET_2_0
  34. public class Generic<T> {
  35. public void Foo () {
  36. }
  37. public void GenericFoo<K> () {
  38. }
  39. }
  40. public class AnotherGeneric<T> {
  41. public void Foo () {
  42. }
  43. }
  44. public class SimpleClass {
  45. public void GenericFoo<K> () {
  46. }
  47. }
  48. #endif
  49. [TestFixture]
  50. public class MethodBaseTest
  51. {
  52. #if NET_2_0
  53. [Test] // GetMethodFromHandle (RuntimeMethodHandle)
  54. public void GetMethodFromHandle1_Handle_Generic ()
  55. {
  56. G<string> instance = new G<string> ();
  57. Type t = instance.GetType ();
  58. MethodBase mb1 = t.GetMethod ("M");
  59. RuntimeMethodHandle mh = mb1.MethodHandle;
  60. RuntimeTypeHandle th = t.TypeHandle;
  61. try {
  62. MethodBase.GetMethodFromHandle (mh);
  63. Assert.Fail ("#1");
  64. } catch (ArgumentException ex) {
  65. // Cannot resolve method Void M(System.__Canon)
  66. // because the declaring type of the method
  67. // handle MonoTests.System.Reflection.MethodBaseTest+G`1[T]
  68. // is generic. Explicitly provide the declaring type to
  69. // GetMethodFromHandle
  70. }
  71. }
  72. #endif
  73. [Test] // GetMethodFromHandle (RuntimeMethodHandle)
  74. public void GetMethodFromHandle1_Handle_Zero ()
  75. {
  76. RuntimeMethodHandle mh = new RuntimeMethodHandle ();
  77. try {
  78. MethodBase.GetMethodFromHandle (mh);
  79. Assert.Fail ("#1");
  80. } catch (ArgumentException ex) {
  81. // Handle is not initialized
  82. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  83. Assert.IsNull (ex.InnerException, "#3");
  84. Assert.IsNotNull (ex.Message, "#4");
  85. Assert.IsNull (ex.ParamName, "#5");
  86. }
  87. }
  88. [Test]
  89. public void GetMethodFromHandle ()
  90. {
  91. Type t = typeof (object);
  92. RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
  93. MethodBase mb = MethodBase.GetMethodFromHandle (rmh);
  94. Assert.IsNotNull (mb, "#1");
  95. Assert.AreEqual (t, mb.DeclaringType, "#2");
  96. Assert.AreEqual (".ctor", mb.Name, "#3");
  97. ParameterInfo [] parameters = mb.GetParameters ();
  98. Assert.IsNotNull (parameters, "#4");
  99. Assert.AreEqual (0, parameters.Length, "#5");
  100. }
  101. #if NET_2_0
  102. [Test]
  103. public void GetMethodFromHandle_NonGenericType_DeclaringTypeZero ()
  104. {
  105. Type t = typeof (object);
  106. RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
  107. MethodBase mb = MethodBase.GetMethodFromHandle (rmh, new RuntimeTypeHandle ());
  108. Assert.IsNotNull (mb, "#1");
  109. Assert.AreEqual (t, mb.DeclaringType, "#2");
  110. Assert.AreEqual (".ctor", mb.Name, "#3");
  111. ParameterInfo [] parameters = mb.GetParameters ();
  112. Assert.IsNotNull (parameters, "#4");
  113. Assert.AreEqual (0, parameters.Length, "#5");
  114. }
  115. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  116. public void GetMethodFromHandle2_DeclaringType_Zero ()
  117. {
  118. RuntimeTypeHandle th = new RuntimeTypeHandle ();
  119. Type t = typeof (G<>);
  120. RuntimeMethodHandle mh = t.GetMethod ("M").MethodHandle;
  121. MethodBase mb = MethodBase.GetMethodFromHandle (mh, th);
  122. Assert.IsNotNull (mb, "#1");
  123. Assert.AreEqual (t, mb.DeclaringType, "#2");
  124. Assert.AreEqual ("M", mb.Name, "#3");
  125. ParameterInfo [] parameters = mb.GetParameters ();
  126. Assert.IsNotNull (parameters, "#4");
  127. Assert.AreEqual (1, parameters.Length, "#5");
  128. Assert.AreEqual (t.GetGenericArguments () [0] , parameters [0].ParameterType, "#6");
  129. }
  130. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  131. public void GetMethodFromHandle2_Handle_Generic ()
  132. {
  133. G<string> instance = new G<string> ();
  134. Type t = instance.GetType ();
  135. MethodBase mb1 = t.GetMethod ("M");
  136. RuntimeMethodHandle mh = mb1.MethodHandle;
  137. RuntimeTypeHandle th = t.TypeHandle;
  138. MethodBase mb2 = MethodBase.GetMethodFromHandle (mh, th);
  139. Assert.IsNotNull (mb2, "#1");
  140. Assert.AreEqual (t, mb2.DeclaringType, "#2");
  141. Assert.AreEqual ("M", mb2.Name, "#3");
  142. ParameterInfo [] parameters = mb2.GetParameters ();
  143. Assert.IsNotNull (parameters, "#4");
  144. Assert.AreEqual (1, parameters.Length, "#5");
  145. Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#6");
  146. }
  147. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  148. public void GetMethodFromHandle2_Handle_Zero ()
  149. {
  150. RuntimeTypeHandle th = typeof (G<>).TypeHandle;
  151. RuntimeMethodHandle mh = new RuntimeMethodHandle ();
  152. try {
  153. MethodBase.GetMethodFromHandle (mh, th);
  154. Assert.Fail ("#1");
  155. } catch (ArgumentException ex) {
  156. // Handle is not initialized
  157. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  158. Assert.IsNull (ex.InnerException, "#3");
  159. Assert.IsNotNull (ex.Message, "#4");
  160. Assert.IsNull (ex.ParamName, "#5");
  161. }
  162. }
  163. public class G<T>
  164. {
  165. public void M (T t)
  166. {
  167. }
  168. }
  169. [Test]
  170. public void GetMethodFromHandle_Handle_Generic_Method ()
  171. {
  172. MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo");
  173. RuntimeMethodHandle handle = mi.MethodHandle;
  174. MethodBase res = MethodBase.GetMethodFromHandle (handle);
  175. Assert.AreEqual (mi, res, "#1");
  176. res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
  177. Assert.AreEqual (mi, res, "#2");
  178. }
  179. [Test]
  180. public void GetMethodFromHandle_Handle_Generic_Method_Instance ()
  181. {
  182. MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  183. RuntimeMethodHandle handle = mi.MethodHandle;
  184. MethodBase res = MethodBase.GetMethodFromHandle (handle);
  185. Assert.AreEqual (mi, res, "#1");
  186. res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
  187. Assert.AreEqual (mi, res, "#2");
  188. }
  189. [Test]
  190. public void GetMethodFromHandle_Handle_Generic_Method_On_Generic_Class ()
  191. {
  192. MethodInfo mi = typeof (Generic<>).GetMethod ("GenericFoo");
  193. RuntimeMethodHandle handle = mi.MethodHandle;
  194. MethodBase res;
  195. try {
  196. MethodBase.GetMethodFromHandle (handle);
  197. Assert.Fail ("#1");
  198. } catch (ArgumentException) {
  199. }
  200. mi = typeof (Generic<int>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  201. handle = mi.MethodHandle;
  202. try {
  203. MethodBase.GetMethodFromHandle (handle);
  204. Assert.Fail ("#2");
  205. } catch (ArgumentException) {
  206. }
  207. mi = typeof (Generic<>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  208. handle = mi.MethodHandle;
  209. try {
  210. MethodBase.GetMethodFromHandle (handle);
  211. Assert.Fail ("#3");
  212. } catch (ArgumentException) {
  213. }
  214. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
  215. Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
  216. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
  217. Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
  218. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  219. Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
  220. try {
  221. MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
  222. Assert.Fail ("#7");
  223. } catch (ArgumentException) {
  224. }
  225. }
  226. [Test]
  227. public void GetMethodFromHandle_Handle_Method_On_Generic_Class ()
  228. {
  229. MethodInfo mi = typeof (Generic<>).GetMethod ("Foo");
  230. RuntimeMethodHandle handle = mi.MethodHandle;
  231. MethodBase res;
  232. try {
  233. MethodBase.GetMethodFromHandle(handle);
  234. Assert.Fail ("#1");
  235. } catch (ArgumentException) {
  236. }
  237. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  238. Assert.AreEqual (res, mi, "#2");
  239. mi = typeof (Generic<int>).GetMethod ("Foo");
  240. handle = mi.MethodHandle;
  241. try {
  242. MethodBase.GetMethodFromHandle(handle);
  243. Assert.Fail ("#3");
  244. } catch (ArgumentException) {
  245. }
  246. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
  247. Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
  248. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
  249. Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
  250. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  251. Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
  252. try {
  253. MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
  254. Assert.Fail ("#7");
  255. } catch (ArgumentException) {
  256. }
  257. }
  258. #endif
  259. }
  260. }