2 // MethodBuilderTest.cs - NUnit Test Cases for the MethodBuilder class
\r
4 // Zoltan Varga (vargaz@freemail.hu)
\r
6 // (C) Ximian, Inc. http://www.ximian.com
\r
9 // - implement 'Signature' (what the hell it does???) and test it
\r
10 // - implement Equals and test it
\r
13 using System.Threading;
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
16 using System.Runtime.CompilerServices;
\r
17 using System.Security;
\r
18 using System.Security.Permissions;
\r
20 using NUnit.Framework;
\r
22 namespace MonoTests.System.Reflection.Emit
\r
26 public class MethodBuilderTest : Assertion
\r
28 private TypeBuilder genClass;
\r
30 private ModuleBuilder module;
\r
33 protected void SetUp () {
\r
34 AssemblyName assemblyName = new AssemblyName();
\r
35 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
\r
37 AssemblyBuilder assembly
\r
38 = Thread.GetDomain().DefineDynamicAssembly(
\r
39 assemblyName, AssemblyBuilderAccess.Run);
\r
41 module = assembly.DefineDynamicModule("module1");
\r
43 genClass = module.DefineType(genTypeName (),
\r
44 TypeAttributes.Public);
\r
47 static int methodIndexer = 0;
\r
49 static int typeIndexer = 0;
\r
51 // Return a unique method name
\r
52 private string genMethodName () {
\r
53 return "m" + (methodIndexer ++);
\r
56 // Return a unique type name
\r
57 private string genTypeName () {
\r
58 return "class" + (typeIndexer ++);
\r
61 public void TestAttributes () {
\r
62 MethodBuilder mb = genClass.DefineMethod (
\r
63 genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
\r
65 AssertEquals ("Attributes works",
\r
66 MethodAttributes.Public, mb.Attributes);
\r
69 public void TestCallingConvention () {
\r
70 MethodBuilder mb = genClass.DefineMethod (
\r
71 genMethodName (), 0, typeof (void), new Type[0]);
\r
72 AssertEquals ("CallingConvetion defaults to Standard+HasThis",
\r
73 CallingConventions.Standard | CallingConventions.HasThis,
\r
74 mb.CallingConvention);
\r
76 MethodBuilder mb3 = genClass.DefineMethod (
\r
77 genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type[0]);
\r
78 AssertEquals ("CallingConvetion works",
\r
79 CallingConventions.VarArgs | CallingConventions.HasThis,
\r
80 mb3.CallingConvention);
\r
82 MethodBuilder mb4 = genClass.DefineMethod (
\r
83 genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
\r
84 typeof (void), new Type [0]);
\r
85 AssertEquals ("Static implies !HasThis",
\r
86 CallingConventions.Standard,
\r
87 mb4.CallingConvention);
\r
90 public void TestDeclaringType () {
\r
91 MethodBuilder mb = genClass.DefineMethod (
\r
92 genMethodName (), 0, typeof (void), new Type[0]);
\r
94 AssertEquals ("DeclaringType works",
\r
95 genClass, mb.DeclaringType);
\r
98 public void TestInitLocals () {
\r
99 MethodBuilder mb = genClass.DefineMethod (
\r
100 genMethodName (), 0, typeof (void), new Type[0]);
\r
102 Assert ("InitLocals defaults to true", mb.InitLocals);
\r
103 mb.InitLocals = false;
\r
104 Assert ("InitLocals is settable", !mb.InitLocals);
\r
107 public void TestMethodHandle () {
\r
108 MethodBuilder mb = genClass.DefineMethod (
\r
109 genMethodName (), 0, typeof (void), new Type [0]);
\r
112 RuntimeMethodHandle handle = mb.MethodHandle;
\r
114 } catch (NotSupportedException) {
\r
118 public void TestName () {
\r
119 string name = genMethodName ();
\r
120 MethodBuilder mb = genClass.DefineMethod (
\r
121 name, 0, typeof (void), new Type [0]);
\r
123 AssertEquals ("Name works", name, mb.Name);
\r
126 public void TestReflectedType () {
\r
127 MethodBuilder mb = genClass.DefineMethod (
\r
128 genMethodName (), 0, typeof (void), new Type [0]);
\r
130 AssertEquals ("ReflectedType works",
\r
131 genClass, mb.ReflectedType);
\r
134 public void TestReturnType () {
\r
135 MethodBuilder mb = genClass.DefineMethod (
\r
136 genMethodName (), 0, typeof (Console), new Type [0]);
\r
138 AssertEquals ("ReturnType works", typeof (Console),
\r
142 MethodBuilder mb2 = genClass.DefineMethod (
\r
143 genMethodName (), 0, null, new Type [0]);
\r
145 Assert ("void ReturnType works", (mb2.ReturnType == null) || (mb2.ReturnType == typeof (void)));
\r
148 public void TestReturnTypeCustomAttributes () {
\r
149 MethodBuilder mb = genClass.DefineMethod (
\r
150 genMethodName (), 0, typeof (Console), new Type [0]);
\r
152 AssertEquals ("ReturnTypeCustomAttributes must be null", null,
\r
153 mb.ReturnTypeCustomAttributes);
\r
157 public void TestSignature () {
\r
158 MethodBuilder mb = genClass.DefineMethod (
\r
159 "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
\r
161 Console.WriteLine (mb.Signature);
\r
165 public void TestCreateMethodBody () {
\r
166 MethodBuilder mb = genClass.DefineMethod (
\r
167 genMethodName (), 0, typeof (void), new Type [0]);
\r
170 mb.CreateMethodBody (null, 999);
\r
172 // Check arguments 1.
\r
174 mb.CreateMethodBody (new byte[1], -1);
\r
176 } catch (ArgumentException) {
\r
179 // Check arguments 2.
\r
181 mb.CreateMethodBody (new byte[1], 2);
\r
183 } catch (ArgumentException) {
\r
186 mb.CreateMethodBody (new byte[2], 1);
\r
188 // Could only be called once
\r
190 mb.CreateMethodBody (new byte[2], 1);
\r
192 } catch (InvalidOperationException) {
\r
195 // Can not be called on a created type
\r
196 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
\r
197 MethodBuilder mb2 = tb.DefineMethod (
\r
198 genMethodName (), 0, typeof (void), new Type [0]);
\r
199 ILGenerator ilgen = mb2.GetILGenerator ();
\r
200 ilgen.Emit (OpCodes.Ret);
\r
204 mb2.CreateMethodBody (new byte[2], 1);
\r
206 } catch (InvalidOperationException) {
\r
210 public void TestDefineParameter () {
\r
211 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
\r
212 MethodBuilder mb = tb.DefineMethod (
\r
213 genMethodName (), 0, typeof (void),
\r
214 new Type [2] { typeof(int), typeof(int) });
\r
216 // index out of range
\r
218 // This fails on mono because the mono version accepts a 0 index
\r
221 mb.DefineParameter (0, 0, "param1");
\r
223 } catch (ArgumentOutOfRangeException) {
\r
228 mb.DefineParameter (3, 0, "param1");
\r
230 } catch (ArgumentOutOfRangeException) {
\r
234 mb.DefineParameter (1, 0, "param1");
\r
235 mb.DefineParameter (1, 0, "param1");
\r
236 mb.DefineParameter (2, 0, null);
\r
238 // Can not be called on a created type
\r
239 mb.CreateMethodBody (new byte[2], 0);
\r
242 mb.DefineParameter (1, 0, "param1");
\r
245 catch (InvalidOperationException) {
\r
249 public void TestGetBaseDefinition () {
\r
250 MethodBuilder mb = genClass.DefineMethod (
\r
251 genMethodName (), 0, typeof (void), new Type [0]);
\r
253 AssertEquals ("GetBaseDefinition works",
\r
254 mb.GetBaseDefinition (), mb);
\r
257 public void TestGetILGenerator () {
\r
258 MethodBuilder mb = genClass.DefineMethod (
\r
259 genMethodName (), 0, typeof (void), new Type [0]);
\r
261 // The same instance is returned on the second call
\r
262 ILGenerator ilgen1 = mb.GetILGenerator ();
\r
263 ILGenerator ilgen2 = mb.GetILGenerator ();
\r
265 AssertEquals ("The same ilgen is returned on the second call",
\r
268 // Does not work on unmanaged code
\r
269 MethodBuilder mb2 = genClass.DefineMethod (
\r
270 genMethodName (), 0, typeof (void), new Type [0]);
\r
272 mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
\r
273 mb2.GetILGenerator ();
\r
275 } catch (InvalidOperationException) {
\r
278 mb2.SetImplementationFlags (MethodImplAttributes.Native);
\r
279 mb2.GetILGenerator ();
\r
281 } catch (InvalidOperationException) {
\r
285 public void TestMethodImplementationFlags () {
\r
286 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
\r
287 MethodBuilder mb = tb.DefineMethod (
\r
288 genMethodName (), 0, typeof (void), new Type [0]);
\r
290 AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
\r
291 MethodImplAttributes.Managed | MethodImplAttributes.IL,
\r
292 mb.GetMethodImplementationFlags ());
\r
294 mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
\r
296 AssertEquals ("SetImplementationFlags works",
\r
297 MethodImplAttributes.OPTIL,
\r
298 mb.GetMethodImplementationFlags ());
\r
300 // Can not be called on a created type
\r
301 mb.CreateMethodBody (new byte[2], 0);
\r
302 mb.SetImplementationFlags (MethodImplAttributes.Managed);
\r
305 mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
\r
308 catch (InvalidOperationException) {
\r
312 public void TestGetModule () {
\r
313 MethodBuilder mb = genClass.DefineMethod (
\r
314 genMethodName (), 0, typeof (void), new Type [0]);
\r
316 AssertEquals ("GetMethod works", module,
\r
320 public void TestGetParameters () {
\r
321 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
\r
322 MethodBuilder mb = tb.DefineMethod (
\r
323 genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
\r
326 * According to the MSDN docs, this method should fail with a
\r
327 * NotSupportedException. In reality, it throws an
\r
328 * InvalidOperationException under MS .NET, and returns the
\r
329 * requested data under mono.
\r
333 mb.GetParameters ();
\r
335 } catch (InvalidOperationException ex) {
\r
336 Console.WriteLine (ex);
\r
341 public void TestGetToken () {
\r
342 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
\r
343 MethodBuilder mb = tb.DefineMethod (
\r
344 genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
\r
349 public void TestInvoke () {
\r
350 MethodBuilder mb = genClass.DefineMethod (
\r
351 genMethodName (), 0, typeof (void),
\r
352 new Type [1] {typeof(int)});
\r
355 mb.Invoke (null, new object [1] { 42 });
\r
357 } catch (NotSupportedException) {
\r
361 mb.Invoke (null, 0, null, new object [1] { 42 }, null);
\r
363 } catch (NotSupportedException) {
\r
367 public void TestIsDefined () {
\r
368 MethodBuilder mb = genClass.DefineMethod (
\r
369 genMethodName (), 0, typeof (void),
\r
370 new Type [1] {typeof(int)});
\r
373 mb.IsDefined (null, true);
\r
375 } catch (NotSupportedException) {
\r
379 public void TestGetCustomAttributes () {
\r
380 MethodBuilder mb = genClass.DefineMethod (
\r
381 genMethodName (), 0, typeof (void),
\r
382 new Type [1] {typeof(int)});
\r
385 mb.GetCustomAttributes (true);
\r
387 } catch (NotSupportedException) {
\r
391 mb.GetCustomAttributes (null, true);
\r
393 } catch (NotSupportedException) {
\r
397 public void TestSetCustomAttribute () {
\r
398 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
\r
399 string name = genMethodName ();
\r
400 MethodBuilder mb = tb.DefineMethod (
\r
401 name, MethodAttributes.Public, typeof (void),
\r
402 new Type [1] {typeof(int)});
\r
406 mb.SetCustomAttribute (null);
\r
408 } catch (ArgumentNullException) {
\r
411 byte[] custAttrData = { 1, 0, 0, 0, 0};
\r
412 Type attrType = Type.GetType
\r
413 ("System.Reflection.AssemblyKeyNameAttribute");
\r
414 Type[] paramTypes = new Type[1];
\r
415 paramTypes[0] = typeof(String);
\r
416 ConstructorInfo ctorInfo =
\r
417 attrType.GetConstructor(paramTypes);
\r
419 mb.SetCustomAttribute (ctorInfo, custAttrData);
\r
421 // Test MethodImplAttribute
\r
422 mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
\r
423 mb.GetILGenerator ().Emit (OpCodes.Ret);
\r
425 Type t = tb.CreateType ();
\r
427 AssertEquals ("Setting MethodImplAttributes works",
\r
428 t.GetMethod (name).GetMethodImplementationFlags (),
\r
429 MethodImplAttributes.Synchronized);
\r
431 // Null arguments again
\r
433 mb.SetCustomAttribute (null, new byte[2]);
\r
435 } catch (ArgumentNullException) {
\r
439 mb.SetCustomAttribute (ctorInfo, null);
\r
441 } catch (ArgumentNullException) {
\r
446 [ExpectedException (typeof (InvalidOperationException))]
\r
447 public void TestAddDeclarativeSecurityAlreadyCreated () {
\r
448 MethodBuilder mb = genClass.DefineMethod (
\r
449 genMethodName (), MethodAttributes.Public, typeof (void),
\r
451 ILGenerator ilgen = mb.GetILGenerator ();
\r
452 ilgen.Emit (OpCodes.Ret);
\r
453 genClass.CreateType ();
\r
455 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
\r
456 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
\r
460 [ExpectedException (typeof (ArgumentNullException))]
\r
461 public void TestAddDeclarativeSecurityNullPermissionSet () {
\r
462 MethodBuilder mb = genClass.DefineMethod (
\r
463 genMethodName (), MethodAttributes.Public, typeof (void),
\r
465 mb.AddDeclarativeSecurity (SecurityAction.Demand, null);
\r
469 public void TestAddDeclarativeSecurityInvalidAction () {
\r
470 MethodBuilder mb = genClass.DefineMethod (
\r
471 genMethodName (), MethodAttributes.Public, typeof (void),
\r
474 SecurityAction[] actions = new SecurityAction [] {
\r
475 SecurityAction.RequestMinimum,
\r
476 SecurityAction.RequestOptional,
\r
477 SecurityAction.RequestRefuse };
\r
478 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
\r
480 foreach (SecurityAction action in actions) {
\r
482 mb.AddDeclarativeSecurity (action, set);
\r
485 catch (ArgumentException) {
\r
491 [ExpectedException (typeof (InvalidOperationException))]
\r
492 public void TestAddDeclarativeSecurityDuplicateAction () {
\r
493 MethodBuilder mb = genClass.DefineMethod (
\r
494 genMethodName (), MethodAttributes.Public, typeof (void),
\r
496 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
\r
497 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
\r
498 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
\r