MethodBaseTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. class MethodBaseOverloadTestDoesNotCauseCompilerError : MethodBase
  48. {
  49. public override MethodAttributes Attributes
  50. {
  51. get { throw new NotImplementedException (); }
  52. }
  53. public override MethodImplAttributes GetMethodImplementationFlags ()
  54. {
  55. throw new NotImplementedException ();
  56. }
  57. public override ParameterInfo[] GetParameters ()
  58. {
  59. throw new NotImplementedException ();
  60. }
  61. public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, global::System.Globalization.CultureInfo culture)
  62. {
  63. throw new NotImplementedException ();
  64. }
  65. public override RuntimeMethodHandle MethodHandle
  66. {
  67. get { throw new NotImplementedException (); }
  68. }
  69. public override Type DeclaringType
  70. {
  71. get { throw new NotImplementedException (); }
  72. }
  73. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  74. {
  75. throw new NotImplementedException ();
  76. }
  77. public override object[] GetCustomAttributes (bool inherit)
  78. {
  79. throw new NotImplementedException ();
  80. }
  81. public override bool IsDefined (Type attributeType, bool inherit)
  82. {
  83. throw new NotImplementedException ();
  84. }
  85. public override MemberTypes MemberType
  86. {
  87. get { throw new NotImplementedException (); }
  88. }
  89. public override string Name
  90. {
  91. get { throw new NotImplementedException (); }
  92. }
  93. public override Type ReflectedType
  94. {
  95. get { throw new NotImplementedException (); }
  96. }
  97. }
  98. [TestFixture]
  99. public class MethodBaseTest
  100. {
  101. public static MethodInfo Where<T> (T a) {
  102. return (MethodInfo) MethodBase.GetCurrentMethod ();
  103. }
  104. public class Foo<K>
  105. {
  106. public static MethodInfo Where<T> (T a, K b) {
  107. return (MethodInfo) MethodBase.GetCurrentMethod ();
  108. }
  109. }
  110. [Test]
  111. public void GetCurrentMethodDropsAllGenericArguments ()
  112. {
  113. MethodInfo a = Where<int> (10);
  114. MethodInfo b = Foo<int>.Where <double> (10, 10);
  115. Assert.IsTrue (a.IsGenericMethodDefinition, "#1");
  116. Assert.IsTrue (b.IsGenericMethodDefinition, "#2");
  117. Assert.IsTrue (b.DeclaringType.IsGenericTypeDefinition, "#3");
  118. Assert.AreSame (a, typeof (MethodBaseTest).GetMethod ("Where"), "#4");
  119. Assert.AreSame (b, typeof (Foo<>).GetMethod ("Where"), "#5");
  120. }
  121. [Test] // GetMethodFromHandle (RuntimeMethodHandle)
  122. public void GetMethodFromHandle1_Handle_Generic ()
  123. {
  124. G<string> instance = new G<string> ();
  125. Type t = instance.GetType ();
  126. MethodBase mb1 = t.GetMethod ("M");
  127. RuntimeMethodHandle mh = mb1.MethodHandle;
  128. RuntimeTypeHandle th = t.TypeHandle;
  129. try {
  130. MethodBase.GetMethodFromHandle (mh);
  131. Assert.Fail ("#1");
  132. } catch (ArgumentException ex) {
  133. // Cannot resolve method Void M(System.__Canon)
  134. // because the declaring type of the method
  135. // handle MonoTests.System.Reflection.MethodBaseTest+G`1[T]
  136. // is generic. Explicitly provide the declaring type to
  137. // GetMethodFromHandle
  138. }
  139. }
  140. [Test] // GetMethodFromHandle (RuntimeMethodHandle)
  141. public void GetMethodFromHandle1_Handle_Zero ()
  142. {
  143. RuntimeMethodHandle mh = new RuntimeMethodHandle ();
  144. try {
  145. MethodBase.GetMethodFromHandle (mh);
  146. Assert.Fail ("#1");
  147. } catch (ArgumentException ex) {
  148. // Handle is not initialized
  149. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  150. Assert.IsNull (ex.InnerException, "#3");
  151. Assert.IsNotNull (ex.Message, "#4");
  152. Assert.IsNull (ex.ParamName, "#5");
  153. }
  154. }
  155. [Test]
  156. public void GetMethodFromHandle ()
  157. {
  158. Type t = typeof (object);
  159. RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
  160. MethodBase mb = MethodBase.GetMethodFromHandle (rmh);
  161. Assert.IsNotNull (mb, "#1");
  162. Assert.AreEqual (t, mb.DeclaringType, "#2");
  163. Assert.AreEqual (".ctor", mb.Name, "#3");
  164. ParameterInfo [] parameters = mb.GetParameters ();
  165. Assert.IsNotNull (parameters, "#4");
  166. Assert.AreEqual (0, parameters.Length, "#5");
  167. }
  168. [Test]
  169. public void GetMethodFromHandle_NonGenericType_DeclaringTypeZero ()
  170. {
  171. Type t = typeof (object);
  172. RuntimeMethodHandle rmh = t.GetConstructor (Type.EmptyTypes).MethodHandle;
  173. MethodBase mb = MethodBase.GetMethodFromHandle (rmh, new RuntimeTypeHandle ());
  174. Assert.IsNotNull (mb, "#1");
  175. Assert.AreEqual (t, mb.DeclaringType, "#2");
  176. Assert.AreEqual (".ctor", mb.Name, "#3");
  177. ParameterInfo [] parameters = mb.GetParameters ();
  178. Assert.IsNotNull (parameters, "#4");
  179. Assert.AreEqual (0, parameters.Length, "#5");
  180. }
  181. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  182. public void GetMethodFromHandle2_DeclaringType_Zero ()
  183. {
  184. RuntimeTypeHandle th = new RuntimeTypeHandle ();
  185. Type t = typeof (G<>);
  186. RuntimeMethodHandle mh = t.GetMethod ("M").MethodHandle;
  187. MethodBase mb = MethodBase.GetMethodFromHandle (mh, th);
  188. Assert.IsNotNull (mb, "#1");
  189. Assert.AreEqual (t, mb.DeclaringType, "#2");
  190. Assert.AreEqual ("M", mb.Name, "#3");
  191. ParameterInfo [] parameters = mb.GetParameters ();
  192. Assert.IsNotNull (parameters, "#4");
  193. Assert.AreEqual (1, parameters.Length, "#5");
  194. Assert.AreEqual (t.GetGenericArguments () [0] , parameters [0].ParameterType, "#6");
  195. }
  196. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  197. public void GetMethodFromHandle2_Handle_Generic ()
  198. {
  199. G<string> instance = new G<string> ();
  200. Type t = instance.GetType ();
  201. MethodBase mb1 = t.GetMethod ("M");
  202. RuntimeMethodHandle mh = mb1.MethodHandle;
  203. RuntimeTypeHandle th = t.TypeHandle;
  204. MethodBase mb2 = MethodBase.GetMethodFromHandle (mh, th);
  205. Assert.IsNotNull (mb2, "#1");
  206. Assert.AreEqual (t, mb2.DeclaringType, "#2");
  207. Assert.AreEqual ("M", mb2.Name, "#3");
  208. ParameterInfo [] parameters = mb2.GetParameters ();
  209. Assert.IsNotNull (parameters, "#4");
  210. Assert.AreEqual (1, parameters.Length, "#5");
  211. Assert.AreEqual (typeof (string), parameters [0].ParameterType, "#6");
  212. }
  213. [Test] // GetMethodFromHandle (RuntimeMethodHandle, RuntimeTypeHandle)
  214. public void GetMethodFromHandle2_Handle_Zero ()
  215. {
  216. RuntimeTypeHandle th = typeof (G<>).TypeHandle;
  217. RuntimeMethodHandle mh = new RuntimeMethodHandle ();
  218. try {
  219. MethodBase.GetMethodFromHandle (mh, th);
  220. Assert.Fail ("#1");
  221. } catch (ArgumentException ex) {
  222. // Handle is not initialized
  223. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  224. Assert.IsNull (ex.InnerException, "#3");
  225. Assert.IsNotNull (ex.Message, "#4");
  226. Assert.IsNull (ex.ParamName, "#5");
  227. }
  228. }
  229. public class G<T>
  230. {
  231. public void M (T t)
  232. {
  233. }
  234. }
  235. [Test]
  236. public void GetMethodFromHandle_Handle_Generic_Method ()
  237. {
  238. MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo");
  239. RuntimeMethodHandle handle = mi.MethodHandle;
  240. MethodBase res = MethodBase.GetMethodFromHandle (handle);
  241. Assert.AreEqual (mi, res, "#1");
  242. res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
  243. Assert.AreEqual (mi, res, "#2");
  244. }
  245. [Test]
  246. public void GetMethodFromHandle_Handle_Generic_Method_Instance ()
  247. {
  248. MethodInfo mi = typeof (SimpleClass).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  249. RuntimeMethodHandle handle = mi.MethodHandle;
  250. MethodBase res = MethodBase.GetMethodFromHandle (handle);
  251. Assert.AreEqual (mi, res, "#1");
  252. res = MethodBase.GetMethodFromHandle (handle, typeof (SimpleClass).TypeHandle);
  253. Assert.AreEqual (mi, res, "#2");
  254. }
  255. [Test]
  256. public void GetMethodFromHandle_Handle_Generic_Method_On_Generic_Class ()
  257. {
  258. MethodInfo mi = typeof (Generic<>).GetMethod ("GenericFoo");
  259. RuntimeMethodHandle handle = mi.MethodHandle;
  260. MethodBase res;
  261. try {
  262. MethodBase.GetMethodFromHandle (handle);
  263. Assert.Fail ("#1");
  264. } catch (ArgumentException) {
  265. }
  266. mi = typeof (Generic<int>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  267. handle = mi.MethodHandle;
  268. try {
  269. MethodBase.GetMethodFromHandle (handle);
  270. Assert.Fail ("#2");
  271. } catch (ArgumentException) {
  272. }
  273. mi = typeof (Generic<>).GetMethod ("GenericFoo").MakeGenericMethod (typeof (int));
  274. handle = mi.MethodHandle;
  275. try {
  276. MethodBase.GetMethodFromHandle (handle);
  277. Assert.Fail ("#3");
  278. } catch (ArgumentException) {
  279. }
  280. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
  281. Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
  282. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
  283. Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
  284. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  285. Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
  286. try {
  287. MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
  288. Assert.Fail ("#7");
  289. } catch (ArgumentException) {
  290. }
  291. }
  292. [Test]
  293. public void GetMethodFromHandle_Handle_Method_On_Generic_Class ()
  294. {
  295. MethodInfo mi = typeof (Generic<>).GetMethod ("Foo");
  296. RuntimeMethodHandle handle = mi.MethodHandle;
  297. MethodBase res;
  298. try {
  299. MethodBase.GetMethodFromHandle(handle);
  300. Assert.Fail ("#1");
  301. } catch (ArgumentException) {
  302. }
  303. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  304. Assert.AreEqual (res, mi, "#2");
  305. mi = typeof (Generic<int>).GetMethod ("Foo");
  306. handle = mi.MethodHandle;
  307. try {
  308. MethodBase.GetMethodFromHandle(handle);
  309. Assert.Fail ("#3");
  310. } catch (ArgumentException) {
  311. }
  312. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<int>).TypeHandle);
  313. Assert.AreEqual (typeof (Generic<int>), res.DeclaringType, "#4");
  314. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<double>).TypeHandle);
  315. Assert.AreEqual (typeof (Generic<double>), res.DeclaringType, "#5");
  316. res = MethodBase.GetMethodFromHandle(handle, typeof (Generic<>).TypeHandle);
  317. Assert.AreEqual (typeof (Generic<>), res.DeclaringType, "#6");
  318. try {
  319. MethodBase.GetMethodFromHandle(handle, typeof (AnotherGeneric<double>).TypeHandle);
  320. Assert.Fail ("#7");
  321. } catch (ArgumentException) {
  322. }
  323. }
  324. // test case adapted from http://www.chrishowie.com/2010/11/24/mutable-strings-in-mono/
  325. public class FakeString {
  326. public int length;
  327. public char start_char;
  328. }
  329. private static FakeString UnsafeConversion<T> (T thing) where T : FakeString
  330. {
  331. return thing;
  332. }
  333. [Test]
  334. public void MutableString ()
  335. {
  336. var m = typeof (MethodBaseTest).GetMethod ("UnsafeConversion", BindingFlags.NonPublic | BindingFlags.Static);
  337. try {
  338. var m2 = m.MakeGenericMethod (typeof (string));
  339. Assert.Fail ("MakeGenericMethod");
  340. }
  341. catch (ArgumentException) {
  342. }
  343. }
  344. }
  345. }