MethodBuilderTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //
  2. // MethodBuilderTest.cs - NUnit Test Cases for the MethodBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. // TODO:
  8. // - implement 'Signature' (what the hell it does???) and test it
  9. // - implement Equals and test it
  10. // - AddDeclarativeSecurity
  11. using System;
  12. using System.Threading;
  13. using System.Reflection;
  14. using System.Reflection.Emit;
  15. using System.Runtime.CompilerServices;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Reflection.Emit
  18. {
  19. [TestFixture]
  20. public class MethodBuilderTest : Assertion
  21. {
  22. private TypeBuilder genClass;
  23. private ModuleBuilder module;
  24. [SetUp]
  25. protected void SetUp () {
  26. AssemblyName assemblyName = new AssemblyName();
  27. assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
  28. AssemblyBuilder assembly
  29. = Thread.GetDomain().DefineDynamicAssembly(
  30. assemblyName, AssemblyBuilderAccess.Run);
  31. module = assembly.DefineDynamicModule("module1");
  32. genClass = module.DefineType("class1",
  33. TypeAttributes.Public);
  34. }
  35. static int methodIndexer = 0;
  36. // Return a unique method name
  37. private string genMethodName () {
  38. return "m" + (methodIndexer ++);
  39. }
  40. public void TestAttributes () {
  41. MethodBuilder mb = genClass.DefineMethod (
  42. genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
  43. AssertEquals ("Attributes works",
  44. MethodAttributes.Public, mb.Attributes);
  45. }
  46. public void TestCallingConvention () {
  47. MethodBuilder mb = genClass.DefineMethod (
  48. genMethodName (), 0, typeof (void), new Type[0]);
  49. AssertEquals ("CallingConvetion defaults to Standard+HasThis",
  50. CallingConventions.Standard | CallingConventions.HasThis,
  51. mb.CallingConvention);
  52. MethodBuilder mb3 = genClass.DefineMethod (
  53. genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type[0]);
  54. AssertEquals ("CallingConvetion works",
  55. CallingConventions.VarArgs | CallingConventions.HasThis,
  56. mb3.CallingConvention);
  57. MethodBuilder mb4 = genClass.DefineMethod (
  58. genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
  59. typeof (void), new Type [0]);
  60. AssertEquals ("Static implies !HasThis",
  61. CallingConventions.Standard,
  62. mb4.CallingConvention);
  63. }
  64. public void TestDeclaringType () {
  65. MethodBuilder mb = genClass.DefineMethod (
  66. genMethodName (), 0, typeof (void), new Type[0]);
  67. AssertEquals ("DeclaringType works",
  68. genClass, mb.DeclaringType);
  69. }
  70. public void TestInitLocals () {
  71. MethodBuilder mb = genClass.DefineMethod (
  72. genMethodName (), 0, typeof (void), new Type[0]);
  73. Assert ("InitLocals defaults to true", mb.InitLocals);
  74. mb.InitLocals = false;
  75. Assert ("InitLocals is settable", !mb.InitLocals);
  76. }
  77. public void TestMethodHandle () {
  78. MethodBuilder mb = genClass.DefineMethod (
  79. genMethodName (), 0, typeof (void), new Type [0]);
  80. try {
  81. RuntimeMethodHandle handle = mb.MethodHandle;
  82. Fail ();
  83. } catch (NotSupportedException) {
  84. }
  85. }
  86. public void TestName () {
  87. string name = genMethodName ();
  88. MethodBuilder mb = genClass.DefineMethod (
  89. name, 0, typeof (void), new Type [0]);
  90. AssertEquals ("Name works", name, mb.Name);
  91. }
  92. public void TestReflectedType () {
  93. MethodBuilder mb = genClass.DefineMethod (
  94. genMethodName (), 0, typeof (void), new Type [0]);
  95. AssertEquals ("ReflectedType works",
  96. genClass, mb.ReflectedType);
  97. }
  98. public void TestReturnType () {
  99. MethodBuilder mb = genClass.DefineMethod (
  100. genMethodName (), 0, typeof (Console), new Type [0]);
  101. AssertEquals ("ReturnType works", typeof (Console),
  102. mb.ReturnType);
  103. MethodBuilder mb2 = genClass.DefineMethod (
  104. genMethodName (), 0, null, new Type [0]);
  105. AssertEquals ("ReturnType is null", null,
  106. mb2.ReturnType);
  107. }
  108. public void TestReturnTypeCustomAttributes () {
  109. MethodBuilder mb = genClass.DefineMethod (
  110. genMethodName (), 0, typeof (Console), new Type [0]);
  111. AssertEquals ("ReturnTypeCustomAttributes must be null", null,
  112. mb.ReturnTypeCustomAttributes);
  113. }
  114. /*
  115. public void TestSignature () {
  116. MethodBuilder mb = genClass.DefineMethod (
  117. "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
  118. Console.WriteLine (mb.Signature);
  119. }
  120. */
  121. public void TestCreateMethodBody () {
  122. MethodBuilder mb = genClass.DefineMethod (
  123. genMethodName (), 0, typeof (void), new Type [0]);
  124. // Clear body
  125. mb.CreateMethodBody (null, 999);
  126. // Check arguments 1.
  127. try {
  128. mb.CreateMethodBody (new byte[1], -1);
  129. Fail ();
  130. } catch (ArgumentException) {
  131. }
  132. // Check arguments 2.
  133. try {
  134. mb.CreateMethodBody (new byte[1], 2);
  135. Fail ();
  136. } catch (ArgumentException) {
  137. }
  138. mb.CreateMethodBody (new byte[2], 1);
  139. // Could only be called once
  140. try {
  141. mb.CreateMethodBody (new byte[2], 1);
  142. Fail ();
  143. } catch (InvalidOperationException) {
  144. }
  145. // Can not be called on a created type
  146. TypeBuilder tb = module.DefineType ("class6", TypeAttributes.Public);
  147. MethodBuilder mb2 = tb.DefineMethod (
  148. genMethodName (), 0, typeof (void), new Type [0]);
  149. ILGenerator ilgen = mb2.GetILGenerator ();
  150. ilgen.Emit (OpCodes.Ret);
  151. tb.CreateType ();
  152. try {
  153. mb2.CreateMethodBody (new byte[2], 1);
  154. Fail ();
  155. } catch (InvalidOperationException) {
  156. }
  157. }
  158. public void TestDefineParameter () {
  159. TypeBuilder tb = module.DefineType ("class7", TypeAttributes.Public);
  160. MethodBuilder mb = tb.DefineMethod (
  161. genMethodName (), 0, typeof (void),
  162. new Type [2] { typeof(int), typeof(int) });
  163. // index out of range
  164. // This fails on mono because the mono version accepts a 0 index
  165. /*
  166. try {
  167. mb.DefineParameter (0, 0, "param1");
  168. Fail ();
  169. } catch (ArgumentOutOfRangeException) {
  170. }
  171. */
  172. try {
  173. mb.DefineParameter (3, 0, "param1");
  174. Fail ();
  175. } catch (ArgumentOutOfRangeException) {
  176. }
  177. // Normal usage
  178. mb.DefineParameter (1, 0, "param1");
  179. mb.DefineParameter (1, 0, "param1");
  180. mb.DefineParameter (2, 0, null);
  181. // Can not be called on a created type
  182. mb.CreateMethodBody (new byte[2], 0);
  183. tb.CreateType ();
  184. try {
  185. mb.DefineParameter (1, 0, "param1");
  186. Fail ();
  187. }
  188. catch (InvalidOperationException) {
  189. }
  190. }
  191. public void TestGetBaseDefinition () {
  192. MethodBuilder mb = genClass.DefineMethod (
  193. genMethodName (), 0, typeof (void), new Type [0]);
  194. AssertEquals ("GetBaseDefinition works",
  195. mb.GetBaseDefinition (), mb);
  196. }
  197. public void TestGetILGenerator () {
  198. MethodBuilder mb = genClass.DefineMethod (
  199. genMethodName (), 0, typeof (void), new Type [0]);
  200. // The same instance is returned on the second call
  201. ILGenerator ilgen1 = mb.GetILGenerator ();
  202. ILGenerator ilgen2 = mb.GetILGenerator ();
  203. AssertEquals ("The same ilgen is returned on the second call",
  204. ilgen1, ilgen2);
  205. // Does not work on unmanaged code
  206. MethodBuilder mb2 = genClass.DefineMethod (
  207. genMethodName (), 0, typeof (void), new Type [0]);
  208. try {
  209. mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
  210. mb2.GetILGenerator ();
  211. Fail ();
  212. } catch (InvalidOperationException) {
  213. }
  214. try {
  215. mb2.SetImplementationFlags (MethodImplAttributes.Native);
  216. mb2.GetILGenerator ();
  217. Fail ();
  218. } catch (InvalidOperationException) {
  219. }
  220. }
  221. public void TestMethodImplementationFlags () {
  222. TypeBuilder tb = module.DefineType ("class14", TypeAttributes.Public);
  223. MethodBuilder mb = tb.DefineMethod (
  224. genMethodName (), 0, typeof (void), new Type [0]);
  225. AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
  226. MethodImplAttributes.Managed | MethodImplAttributes.IL,
  227. mb.GetMethodImplementationFlags ());
  228. mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
  229. AssertEquals ("SetImplementationFlags works",
  230. MethodImplAttributes.OPTIL,
  231. mb.GetMethodImplementationFlags ());
  232. // Can not be called on a created type
  233. mb.CreateMethodBody (new byte[2], 0);
  234. mb.SetImplementationFlags (MethodImplAttributes.Managed);
  235. tb.CreateType ();
  236. try {
  237. mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
  238. Fail ();
  239. }
  240. catch (InvalidOperationException) {
  241. }
  242. }
  243. public void TestGetModule () {
  244. MethodBuilder mb = genClass.DefineMethod (
  245. genMethodName (), 0, typeof (void), new Type [0]);
  246. AssertEquals ("GetMethod works", module,
  247. mb.GetModule ());
  248. }
  249. public void TestGetParameters () {
  250. TypeBuilder tb = module.DefineType ("class16", TypeAttributes.Public);
  251. MethodBuilder mb = tb.DefineMethod (
  252. genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
  253. /*
  254. * According to the MSDN docs, this method should fail with a
  255. * NotSupportedException. In reality, it throws an
  256. * InvalidOperationException under MS .NET, and returns the
  257. * requested data under mono.
  258. */
  259. /*
  260. try {
  261. mb.GetParameters ();
  262. Fail ("#161");
  263. } catch (InvalidOperationException ex) {
  264. Console.WriteLine (ex);
  265. }
  266. */
  267. }
  268. public void TestGetToken () {
  269. TypeBuilder tb = module.DefineType ("class17", TypeAttributes.Public);
  270. MethodBuilder mb = tb.DefineMethod (
  271. genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
  272. mb.GetToken ();
  273. }
  274. public void TestInvoke () {
  275. MethodBuilder mb = genClass.DefineMethod (
  276. genMethodName (), 0, typeof (void),
  277. new Type [1] {typeof(int)});
  278. try {
  279. mb.Invoke (null, new object [1] { 42 });
  280. Fail ();
  281. } catch (NotSupportedException) {
  282. }
  283. try {
  284. mb.Invoke (null, 0, null, new object [1] { 42 }, null);
  285. Fail ();
  286. } catch (NotSupportedException) {
  287. }
  288. }
  289. public void TestIsDefined () {
  290. MethodBuilder mb = genClass.DefineMethod (
  291. genMethodName (), 0, typeof (void),
  292. new Type [1] {typeof(int)});
  293. try {
  294. mb.IsDefined (null, true);
  295. Fail ();
  296. } catch (NotSupportedException) {
  297. }
  298. }
  299. public void TestGetCustomAttributes () {
  300. MethodBuilder mb = genClass.DefineMethod (
  301. genMethodName (), 0, typeof (void),
  302. new Type [1] {typeof(int)});
  303. try {
  304. mb.GetCustomAttributes (true);
  305. Fail ();
  306. } catch (NotSupportedException) {
  307. }
  308. try {
  309. mb.GetCustomAttributes (null, true);
  310. Fail ();
  311. } catch (NotSupportedException) {
  312. }
  313. }
  314. public void TestSetCustomAttribute () {
  315. TypeBuilder tb = module.DefineType ("class21", TypeAttributes.Public);
  316. string name = genMethodName ();
  317. MethodBuilder mb = tb.DefineMethod (
  318. name, MethodAttributes.Public, typeof (void),
  319. new Type [1] {typeof(int)});
  320. // Null argument
  321. try {
  322. mb.SetCustomAttribute (null);
  323. Fail ();
  324. } catch (ArgumentNullException) {
  325. }
  326. byte[] custAttrData = { 1, 0, 0, 0, 0};
  327. Type attrType = Type.GetType
  328. ("System.Reflection.AssemblyKeyNameAttribute");
  329. Type[] paramTypes = new Type[1];
  330. paramTypes[0] = typeof(String);
  331. ConstructorInfo ctorInfo =
  332. attrType.GetConstructor(paramTypes);
  333. mb.SetCustomAttribute (ctorInfo, custAttrData);
  334. // Test MethodImplAttribute
  335. mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
  336. mb.GetILGenerator ().Emit (OpCodes.Ret);
  337. Type t = tb.CreateType ();
  338. AssertEquals ("Setting MethodImplAttributes works",
  339. t.GetMethod (name).GetMethodImplementationFlags (),
  340. MethodImplAttributes.Synchronized);
  341. // Null arguments again
  342. try {
  343. mb.SetCustomAttribute (null, new byte[2]);
  344. Fail ();
  345. } catch (ArgumentNullException) {
  346. }
  347. try {
  348. mb.SetCustomAttribute (ctorInfo, null);
  349. Fail ();
  350. } catch (ArgumentNullException) {
  351. }
  352. }
  353. }
  354. }