DynamicMethodTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // DynamicMethodTest.cs - NUnit Test Cases for the DynamicMethod class
  3. //
  4. // Gert Driesen ([email protected])
  5. //
  6. // (C) 2006 Novell
  7. #if NET_2_0
  8. using System;
  9. using System.Reflection;
  10. using System.Reflection.Emit;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Reflection.Emit
  13. {
  14. [TestFixture]
  15. public class DynamicMethodTest
  16. {
  17. private delegate int HelloInvoker (string msg);
  18. [Test]
  19. public void Constructor1_Name_Null ()
  20. {
  21. try {
  22. new DynamicMethod (null,
  23. typeof (void),
  24. new Type[] { typeof (string) },
  25. typeof (DynamicMethodTest).Module);
  26. Assert.Fail ("#1");
  27. } catch (ArgumentNullException ex) {
  28. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  29. Assert.AreEqual ("name", ex.ParamName, "#3");
  30. Assert.IsNull (ex.InnerException, "#4");
  31. }
  32. }
  33. [Test]
  34. public void Constructor2_Name_Null ()
  35. {
  36. try {
  37. new DynamicMethod (null,
  38. typeof (void),
  39. new Type[] { typeof (string) },
  40. typeof (DynamicMethodTest));
  41. Assert.Fail ("#1");
  42. } catch (ArgumentNullException ex) {
  43. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  44. Assert.AreEqual ("name", ex.ParamName, "#3");
  45. Assert.IsNull (ex.InnerException, "#4");
  46. }
  47. }
  48. [Test]
  49. public void Constructor3_Name_Null ()
  50. {
  51. try {
  52. new DynamicMethod (null,
  53. typeof (void),
  54. new Type[] { typeof (string) },
  55. typeof (DynamicMethodTest).Module, true);
  56. Assert.Fail ("#1");
  57. } catch (ArgumentNullException ex) {
  58. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  59. Assert.AreEqual ("name", ex.ParamName, "#3");
  60. Assert.IsNull (ex.InnerException, "#4");
  61. }
  62. }
  63. [Test]
  64. public void Constructor4_Name_Null ()
  65. {
  66. try {
  67. new DynamicMethod (null,
  68. typeof (void),
  69. new Type[] { typeof (string) },
  70. typeof (DynamicMethodTest), true);
  71. Assert.Fail ("#1");
  72. } catch (ArgumentNullException ex) {
  73. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  74. Assert.AreEqual ("name", ex.ParamName, "#3");
  75. Assert.IsNull (ex.InnerException, "#4");
  76. }
  77. }
  78. [Test]
  79. public void Constructor5_Name_Null ()
  80. {
  81. try {
  82. new DynamicMethod (null,
  83. MethodAttributes.Public | MethodAttributes.Static,
  84. CallingConventions.Standard,
  85. typeof (void),
  86. new Type[] { typeof (string) },
  87. typeof (DynamicMethodTest).Module, true);
  88. Assert.Fail ("#1");
  89. } catch (ArgumentNullException ex) {
  90. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  91. Assert.AreEqual ("name", ex.ParamName, "#3");
  92. Assert.IsNull (ex.InnerException, "#4");
  93. }
  94. }
  95. [Test]
  96. public void Constructor6_Name_Null ()
  97. {
  98. try {
  99. new DynamicMethod (null,
  100. MethodAttributes.Public | MethodAttributes.Static,
  101. CallingConventions.Standard,
  102. typeof (void),
  103. new Type[] { typeof (string) },
  104. typeof (DynamicMethodTest), true);
  105. Assert.Fail ("#1");
  106. } catch (ArgumentNullException ex) {
  107. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  108. Assert.AreEqual ("name", ex.ParamName, "#3");
  109. Assert.IsNull (ex.InnerException, "#4");
  110. }
  111. }
  112. [Test] // bug #78253
  113. public void DynamicMethodReference ()
  114. {
  115. DynamicMethod hello = new DynamicMethod ("Hello",
  116. typeof (int),
  117. new Type[] { typeof (string) },
  118. typeof (DynamicMethodTest).Module);
  119. Assert.IsNull (hello.DeclaringType, "#1");
  120. DynamicMethod write = new DynamicMethod ("Write",
  121. typeof (int),
  122. new Type[] { typeof (string) },
  123. typeof (DynamicMethodTest));
  124. Assert.IsNull (hello.DeclaringType, "#2");
  125. MethodInfo invokeWrite = write.GetBaseDefinition ();
  126. ILGenerator helloIL = hello.GetILGenerator ();
  127. helloIL.Emit (OpCodes.Ldarg_0);
  128. helloIL.EmitCall (OpCodes.Call, invokeWrite, null);
  129. helloIL.Emit (OpCodes.Ret);
  130. ILGenerator writeIL = write.GetILGenerator ();
  131. writeIL.Emit (OpCodes.Ldc_I4_2);
  132. writeIL.Emit (OpCodes.Ret);
  133. HelloInvoker hi =
  134. (HelloInvoker) hello.CreateDelegate (typeof (HelloInvoker));
  135. int ret = hi ("Hello, World!");
  136. Assert.AreEqual (2, ret, "#3");
  137. object[] invokeArgs = { "Hello, World!" };
  138. object objRet = hello.Invoke (null, invokeArgs);
  139. Assert.AreEqual (2, objRet, "#4");
  140. }
  141. [Test]
  142. public void EmptyMethodBody ()
  143. {
  144. DynamicMethod hello = new DynamicMethod ("Hello",
  145. typeof (int),
  146. new Type[] { typeof (string) },
  147. typeof (DynamicMethodTest).Module);
  148. object[] invokeArgs = { "Hello, World!" };
  149. // no IL generator
  150. try {
  151. hello.Invoke (null, invokeArgs);
  152. Assert.Fail ("#1");
  153. } catch (InvalidOperationException) {
  154. }
  155. // empty method body
  156. hello.GetILGenerator ();
  157. try {
  158. hello.Invoke (null, invokeArgs);
  159. Assert.Fail ("#2");
  160. } catch (InvalidOperationException) {
  161. }
  162. }
  163. private delegate string ReturnString (string msg);
  164. private delegate void DoNothing (string msg);
  165. private static string private_method (string s) {
  166. return s;
  167. }
  168. [Test]
  169. public void SkipVisibility ()
  170. {
  171. DynamicMethod hello = new DynamicMethod ("Hello",
  172. typeof (string),
  173. new Type[] { typeof (string) },
  174. typeof (DynamicMethodTest).Module, true);
  175. ILGenerator helloIL = hello.GetILGenerator ();
  176. helloIL.Emit (OpCodes.Ldarg_0);
  177. helloIL.EmitCall (OpCodes.Call, typeof (DynamicMethodTest).GetMethod ("private_method", BindingFlags.Static|BindingFlags.NonPublic), null);
  178. helloIL.Emit (OpCodes.Ret);
  179. ReturnString del =
  180. (ReturnString) hello.CreateDelegate (typeof (ReturnString));
  181. Assert.AreEqual ("ABCD", del ("ABCD"));
  182. }
  183. [Test]
  184. public void ReturnType_Null ()
  185. {
  186. DynamicMethod hello = new DynamicMethod ("Hello",
  187. null,
  188. new Type[] { typeof (string) },
  189. typeof (DynamicMethodTest).Module, true);
  190. Assert.AreEqual (typeof (void), hello.ReturnType, "#1");
  191. ILGenerator helloIL = hello.GetILGenerator ();
  192. helloIL.Emit (OpCodes.Ret);
  193. DoNothing dn = (DoNothing) hello.CreateDelegate (typeof (DoNothing));
  194. dn ("whatever");
  195. object[] invokeArgs = { "Hello, World!" };
  196. object objRet = hello.Invoke (null, invokeArgs);
  197. Assert.IsNull (objRet, "#2");
  198. }
  199. [Test]
  200. public void Name_Empty ()
  201. {
  202. DynamicMethod hello = new DynamicMethod (string.Empty,
  203. typeof (int),
  204. new Type[] { typeof (string) },
  205. typeof (DynamicMethodTest).Module);
  206. Assert.AreEqual (string.Empty, hello.Name, "#1");
  207. DynamicMethod write = new DynamicMethod ("Write",
  208. typeof (int),
  209. new Type[] { typeof (string) },
  210. typeof (DynamicMethodTest));
  211. MethodInfo invokeWrite = write.GetBaseDefinition ();
  212. ILGenerator helloIL = hello.GetILGenerator ();
  213. helloIL.Emit (OpCodes.Ldarg_0);
  214. helloIL.EmitCall (OpCodes.Call, invokeWrite, null);
  215. helloIL.Emit (OpCodes.Ret);
  216. ILGenerator writeIL = write.GetILGenerator ();
  217. writeIL.Emit (OpCodes.Ldc_I4_2);
  218. writeIL.Emit (OpCodes.Ret);
  219. HelloInvoker hi =
  220. (HelloInvoker) hello.CreateDelegate (typeof (HelloInvoker));
  221. int ret = hi ("Hello, World!");
  222. Assert.AreEqual (2, ret, "#2");
  223. object[] invokeArgs = { "Hello, World!" };
  224. object objRet = hello.Invoke (null, invokeArgs);
  225. Assert.AreEqual (2, objRet, "#3");
  226. }
  227. [Test]
  228. public void Circular_Refs () {
  229. DynamicMethod m1 = new DynamicMethod("f1", typeof(int), new Type[] { typeof (int) },
  230. typeof(object));
  231. DynamicMethod m2 = new DynamicMethod("f2", typeof(int), new Type[] { typeof (int) },
  232. typeof(object));
  233. ILGenerator il1 = m1.GetILGenerator();
  234. ILGenerator il2 = m2.GetILGenerator();
  235. Label l = il1.DefineLabel ();
  236. //il1.EmitWriteLine ("f1");
  237. il1.Emit (OpCodes.Ldarg_0);
  238. il1.Emit (OpCodes.Ldc_I4_0);
  239. il1.Emit (OpCodes.Bne_Un, l);
  240. il1.Emit (OpCodes.Ldarg_0);
  241. il1.Emit (OpCodes.Ret);
  242. il1.MarkLabel (l);
  243. il1.Emit (OpCodes.Ldarg_0);
  244. il1.Emit (OpCodes.Ldc_I4_1);
  245. il1.Emit (OpCodes.Sub);
  246. il1.Emit (OpCodes.Call, m2);
  247. il1.Emit (OpCodes.Ret);
  248. //il2.EmitWriteLine("f2");
  249. il2.Emit(OpCodes.Ldarg_0);
  250. il2.Emit(OpCodes.Call, m1);
  251. il2.Emit(OpCodes.Ret);
  252. m1.Invoke(null, new object[] { 5 });
  253. }
  254. class Host {
  255. static string Field = "foo";
  256. }
  257. [Test]
  258. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297416
  259. public void TestOwnerMemberAccess ()
  260. {
  261. DynamicMethod method = new DynamicMethod ("GetField",
  262. typeof (string), new Type [0], typeof (Host));
  263. ILGenerator il = method.GetILGenerator ();
  264. il.Emit (OpCodes.Ldsfld, typeof (Host).GetField (
  265. "Field", BindingFlags.Static | BindingFlags.NonPublic));
  266. il.Emit (OpCodes.Ret);
  267. string ret = (string) method.Invoke (null, new object [] {});
  268. Assert.AreEqual ("foo", ret, "#1");
  269. }
  270. }
  271. }
  272. #endif