2 // MethodBuilderTest.cs - NUnit Test Cases for the MethodBuilder class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
9 // - implement 'Signature' (what the hell it does???) and test it
10 // - implement Equals and test it
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Runtime.CompilerServices;
17 using System.Security;
18 using System.Security.Permissions;
19 using System.Runtime.InteropServices;
21 using NUnit.Framework;
23 namespace MonoTests.System.Reflection.Emit
27 public class MethodBuilderTest : Assertion
29 private TypeBuilder genClass;
31 private ModuleBuilder module;
34 protected void SetUp () {
35 AssemblyName assemblyName = new AssemblyName();
36 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
38 AssemblyBuilder assembly
39 = Thread.GetDomain().DefineDynamicAssembly(
40 assemblyName, AssemblyBuilderAccess.Run);
42 module = assembly.DefineDynamicModule("module1");
44 genClass = module.DefineType(genTypeName (),
45 TypeAttributes.Public);
48 static int methodIndexer = 0;
50 static int typeIndexer = 0;
52 // Return a unique method name
53 private string genMethodName () {
54 return "m" + (methodIndexer ++);
57 // Return a unique type name
58 private string genTypeName () {
59 return "class" + (typeIndexer ++);
63 public void TestAttributes () {
64 MethodBuilder mb = genClass.DefineMethod (
65 genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
67 AssertEquals ("Attributes works",
68 MethodAttributes.Public, mb.Attributes);
72 public void TestCallingConvention () {
73 MethodBuilder mb = genClass.DefineMethod (
74 genMethodName (), 0, typeof (void), new Type[0]);
75 AssertEquals ("CallingConvetion defaults to Standard+HasThis",
76 CallingConventions.Standard | CallingConventions.HasThis,
77 mb.CallingConvention);
79 MethodBuilder mb3 = genClass.DefineMethod (
80 genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type[0]);
81 AssertEquals ("CallingConvetion works",
82 CallingConventions.VarArgs | CallingConventions.HasThis,
83 mb3.CallingConvention);
85 MethodBuilder mb4 = genClass.DefineMethod (
86 genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
87 typeof (void), new Type [0]);
88 AssertEquals ("Static implies !HasThis",
89 CallingConventions.Standard,
90 mb4.CallingConvention);
94 public void TestDeclaringType () {
95 MethodBuilder mb = genClass.DefineMethod (
96 genMethodName (), 0, typeof (void), new Type[0]);
98 AssertEquals ("DeclaringType works",
99 genClass, mb.DeclaringType);
103 public void TestInitLocals () {
104 MethodBuilder mb = genClass.DefineMethod (
105 genMethodName (), 0, typeof (void), new Type[0]);
107 Assert ("InitLocals defaults to true", mb.InitLocals);
108 mb.InitLocals = false;
109 Assert ("InitLocals is settable", !mb.InitLocals);
113 [ExpectedException (typeof(NotSupportedException))]
114 public void TestMethodHandleIncomplete () {
115 MethodBuilder mb = genClass.DefineMethod (
116 genMethodName (), 0, typeof (void), new Type [0]);
118 RuntimeMethodHandle handle = mb.MethodHandle;
122 [ExpectedException (typeof(NotSupportedException))]
123 public void TestMethodHandleComplete () {
124 MethodBuilder mb = genClass.DefineMethod (
125 genMethodName (), 0, typeof (void), new Type [0]);
126 mb.CreateMethodBody (new byte[2], 1);
127 genClass.CreateType ();
129 RuntimeMethodHandle handle = mb.MethodHandle;
133 public void TestName () {
134 string name = genMethodName ();
135 MethodBuilder mb = genClass.DefineMethod (
136 name, 0, typeof (void), new Type [0]);
138 AssertEquals ("Name works", name, mb.Name);
142 public void TestReflectedType () {
143 MethodBuilder mb = genClass.DefineMethod (
144 genMethodName (), 0, typeof (void), new Type [0]);
146 AssertEquals ("ReflectedType works",
147 genClass, mb.ReflectedType);
151 public void TestReturnType () {
152 MethodBuilder mb = genClass.DefineMethod (
153 genMethodName (), 0, typeof (Console), new Type [0]);
155 AssertEquals ("ReturnType works", typeof (Console),
159 MethodBuilder mb2 = genClass.DefineMethod (
160 genMethodName (), 0, null, new Type [0]);
162 Assert ("void ReturnType works", (mb2.ReturnType == null) || (mb2.ReturnType == typeof (void)));
166 public void TestReturnTypeCustomAttributes () {
167 MethodBuilder mb = genClass.DefineMethod (
168 genMethodName (), 0, typeof (Console), new Type [0]);
170 AssertEquals ("ReturnTypeCustomAttributes must be null", null,
171 mb.ReturnTypeCustomAttributes);
175 public void TestSignature () {
176 MethodBuilder mb = genClass.DefineMethod (
177 "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
179 Console.WriteLine (mb.Signature);
184 public void TestCreateMethodBody () {
185 MethodBuilder mb = genClass.DefineMethod (
186 genMethodName (), 0, typeof (void), new Type [0]);
189 mb.CreateMethodBody (null, 999);
191 // Check arguments 1.
193 mb.CreateMethodBody (new byte[1], -1);
195 } catch (ArgumentOutOfRangeException) {
198 // Check arguments 2.
200 mb.CreateMethodBody (new byte[1], 2);
202 } catch (ArgumentOutOfRangeException) {
205 mb.CreateMethodBody (new byte[2], 1);
207 // Could only be called once
209 mb.CreateMethodBody (new byte[2], 1);
211 } catch (InvalidOperationException) {
214 // Can not be called on a created type
215 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
216 MethodBuilder mb2 = tb.DefineMethod (
217 genMethodName (), 0, typeof (void), new Type [0]);
218 ILGenerator ilgen = mb2.GetILGenerator ();
219 ilgen.Emit (OpCodes.Ret);
223 mb2.CreateMethodBody (new byte[2], 1);
225 } catch (InvalidOperationException) {
230 [ExpectedException (typeof(InvalidOperationException))]
231 public void TestDefineParameterInvalidIndexComplete ()
233 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
234 MethodBuilder mb = tb.DefineMethod (
235 genMethodName (), 0, typeof (void),
237 typeof(int), typeof(int)
239 mb.CreateMethodBody (new byte[2], 1);
241 mb.DefineParameter (-5, ParameterAttributes.None, "param1");
245 [ExpectedException (typeof(InvalidOperationException))]
246 public void TestDefineParameterValidIndexComplete ()
248 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
249 MethodBuilder mb = tb.DefineMethod (
250 genMethodName (), 0, typeof (void),
252 typeof(int), typeof(int)
254 mb.CreateMethodBody (new byte[2], 1);
256 mb.DefineParameter (1, ParameterAttributes.None, "param1");
260 public void TestDefineParameter () {
261 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
262 MethodBuilder mb = tb.DefineMethod (
263 genMethodName (), 0, typeof (void),
264 new Type [2] { typeof(int), typeof(int) });
266 // index out of range
268 // This fails on mono because the mono version accepts a 0 index
271 mb.DefineParameter (0, 0, "param1");
273 } catch (ArgumentOutOfRangeException) {
278 mb.DefineParameter (3, 0, "param1");
280 } catch (ArgumentOutOfRangeException) {
284 mb.DefineParameter (1, 0, "param1");
285 mb.DefineParameter (1, 0, "param1");
286 mb.DefineParameter (2, 0, null);
288 mb.CreateMethodBody (new byte[2], 1);
291 mb.DefineParameter (1, 0, "param1");
294 catch (InvalidOperationException) {
300 // MS.NET 2.x no longer allows a zero length method body
302 [ExpectedException (typeof (InvalidOperationException))]
304 public void ZeroLengthBodyTest1 ()
306 MethodBuilder mb = genClass.DefineMethod (
307 genMethodName (), 0, typeof (void),
308 new Type [2] { typeof(int), typeof(int) });
309 mb.CreateMethodBody (new byte[2], 0);
310 genClass.CreateType ();
313 // A zero length method body can be created
315 public void ZeroLengthBodyTest2 ()
317 MethodBuilder mb = genClass.DefineMethod (
318 genMethodName (), 0, typeof (void),
319 new Type [2] { typeof(int), typeof(int) });
320 mb.CreateMethodBody (new byte[2], 0);
324 public void TestHashCode ()
326 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
327 string methodName = genMethodName ();
328 MethodBuilder mb = tb.DefineMethod (
329 methodName, 0, typeof (void),
331 typeof(int), typeof(int)
333 AssertEquals ("Hashcode of method should be equal to hashcode of method name",
334 methodName.GetHashCode (), mb.GetHashCode ());
338 public void TestGetBaseDefinition () {
339 MethodBuilder mb = genClass.DefineMethod (
340 genMethodName (), 0, typeof (void), new Type [0]);
342 AssertEquals ("GetBaseDefinition works",
343 mb.GetBaseDefinition (), mb);
347 public void TestGetILGenerator () {
348 MethodBuilder mb = genClass.DefineMethod (
349 genMethodName (), 0, typeof (void), new Type [0]);
351 // The same instance is returned on the second call
352 ILGenerator ilgen1 = mb.GetILGenerator ();
353 ILGenerator ilgen2 = mb.GetILGenerator ();
355 AssertEquals ("The same ilgen is returned on the second call",
358 // Does not work on unmanaged code
359 MethodBuilder mb2 = genClass.DefineMethod (
360 genMethodName (), 0, typeof (void), new Type [0]);
362 mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
363 mb2.GetILGenerator ();
365 } catch (InvalidOperationException) {
368 mb2.SetImplementationFlags (MethodImplAttributes.Native);
369 mb2.GetILGenerator ();
371 } catch (InvalidOperationException) {
376 public void TestMethodImplementationFlags () {
377 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
378 MethodBuilder mb = tb.DefineMethod (
379 genMethodName (), 0, typeof (void), new Type [0]);
381 AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
382 MethodImplAttributes.Managed | MethodImplAttributes.IL,
383 mb.GetMethodImplementationFlags ());
385 mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
387 AssertEquals ("SetImplementationFlags works",
388 MethodImplAttributes.OPTIL,
389 mb.GetMethodImplementationFlags ());
391 mb.CreateMethodBody (new byte[2], 1);
392 mb.SetImplementationFlags (MethodImplAttributes.Managed);
395 mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
398 catch (InvalidOperationException) {
403 public void TestGetModule () {
404 MethodBuilder mb = genClass.DefineMethod (
405 genMethodName (), 0, typeof (void), new Type [0]);
407 AssertEquals ("GetMethod works", module,
412 public void TestGetParameters () {
413 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
414 MethodBuilder mb = tb.DefineMethod (
415 genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
418 * According to the MSDN docs, this method should fail with a
419 * NotSupportedException. In reality, it throws an
420 * InvalidOperationException under MS .NET, and returns the
421 * requested data under mono.
427 } catch (InvalidOperationException ex) {
428 Console.WriteLine (ex);
434 public void TestGetToken () {
435 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
436 MethodBuilder mb = tb.DefineMethod (
437 genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
443 public void TestInvoke () {
444 MethodBuilder mb = genClass.DefineMethod (
445 genMethodName (), 0, typeof (void),
446 new Type [1] {typeof(int)});
449 mb.Invoke (null, new object [1] { 42 });
451 } catch (NotSupportedException) {
455 mb.Invoke (null, 0, null, new object [1] { 42 }, null);
457 } catch (NotSupportedException) {
462 [ExpectedException (typeof (NotSupportedException))]
463 public void TestIsDefined () {
464 MethodBuilder mb = genClass.DefineMethod (
465 genMethodName (), 0, typeof (void),
466 new Type [1] {typeof(int)});
467 mb.IsDefined (null, true);
471 public void TestGetCustomAttributes () {
472 MethodBuilder mb = genClass.DefineMethod (
473 genMethodName (), 0, typeof (void),
474 new Type [1] {typeof(int)});
477 mb.GetCustomAttributes (true);
479 } catch (NotSupportedException) {
483 mb.GetCustomAttributes (null, true);
485 } catch (NotSupportedException) {
490 public void TestSetCustomAttribute () {
491 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
492 string name = genMethodName ();
493 MethodBuilder mb = tb.DefineMethod (
494 name, MethodAttributes.Public, typeof (void),
495 new Type [1] {typeof(int)});
499 mb.SetCustomAttribute (null);
501 } catch (ArgumentNullException) {
504 byte[] custAttrData = { 1, 0, 0, 0, 0};
505 Type attrType = Type.GetType
506 ("System.Reflection.AssemblyKeyNameAttribute");
507 Type[] paramTypes = new Type[1];
508 paramTypes[0] = typeof(String);
509 ConstructorInfo ctorInfo =
510 attrType.GetConstructor(paramTypes);
512 mb.SetCustomAttribute (ctorInfo, custAttrData);
514 // Test MethodImplAttribute
515 mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
516 mb.GetILGenerator ().Emit (OpCodes.Ret);
518 Type t = tb.CreateType ();
520 AssertEquals ("Setting MethodImplAttributes works",
521 t.GetMethod (name).GetMethodImplementationFlags (),
522 MethodImplAttributes.Synchronized);
524 // Null arguments again
526 mb.SetCustomAttribute (null, new byte[2]);
528 } catch (ArgumentNullException) {
532 mb.SetCustomAttribute (ctorInfo, null);
534 } catch (ArgumentNullException) {
539 [ExpectedException (typeof (InvalidOperationException))]
540 public void TestAddDeclarativeSecurityAlreadyCreated () {
541 MethodBuilder mb = genClass.DefineMethod (
542 genMethodName (), MethodAttributes.Public, typeof (void),
544 ILGenerator ilgen = mb.GetILGenerator ();
545 ilgen.Emit (OpCodes.Ret);
546 genClass.CreateType ();
548 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
549 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
553 [ExpectedException (typeof (ArgumentNullException))]
554 public void TestAddDeclarativeSecurityNullPermissionSet () {
555 MethodBuilder mb = genClass.DefineMethod (
556 genMethodName (), MethodAttributes.Public, typeof (void),
558 mb.AddDeclarativeSecurity (SecurityAction.Demand, null);
562 public void TestAddDeclarativeSecurityInvalidAction () {
563 MethodBuilder mb = genClass.DefineMethod (
564 genMethodName (), MethodAttributes.Public, typeof (void),
567 SecurityAction[] actions = new SecurityAction [] {
568 SecurityAction.RequestMinimum,
569 SecurityAction.RequestOptional,
570 SecurityAction.RequestRefuse };
571 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
573 foreach (SecurityAction action in actions) {
575 mb.AddDeclarativeSecurity (action, set);
578 catch (ArgumentException) {
584 [ExpectedException (typeof (InvalidOperationException))]
585 public void TestAddDeclarativeSecurityDuplicateAction () {
586 MethodBuilder mb = genClass.DefineMethod (
587 genMethodName (), MethodAttributes.Public, typeof (void),
589 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
590 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
591 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
594 [AttributeUsage (AttributeTargets.Parameter)]
595 class ParamAttribute : Attribute {
597 public ParamAttribute () {
602 public void TestDynamicParams () {
603 string mname = genMethodName ();
605 MethodBuilder mb = genClass.DefineMethod (
606 mname, MethodAttributes.Public, typeof (void),
607 new Type [] { typeof (int), typeof (string) });
608 ParameterBuilder pb = mb.DefineParameter (1, ParameterAttributes.In, "foo");
610 pb.SetCustomAttribute (new CustomAttributeBuilder (typeof (ParamAttribute).GetConstructors () [0], new object [] { }));
611 ParameterBuilder pb2 = mb.DefineParameter (2, 0, "bar");
612 pb2.SetConstant ("foo");
613 mb.GetILGenerator ().Emit (OpCodes.Ret);
615 Type t = genClass.CreateType ();
616 MethodInfo m = t.GetMethod (mname);
617 ParameterInfo[] pi = m.GetParameters ();
619 AssertEquals ("foo", pi [0].Name);
620 AssertEquals (true, pi [0].IsIn);
621 AssertEquals (52, pi [0].DefaultValue);
622 object[] cattrs = pi [0].GetCustomAttributes (true);
624 AssertEquals ("foo", pi [1].DefaultValue);
627 /* This test does not run under MS.NET: */
629 AssertEquals (1, cattrs.Length);
630 AssertEquals (typeof (ParamAttribute), cattrs [0].GetType ());
636 public void SetCustomAttribute_DllImport1 () {
637 string mname = genMethodName ();
639 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
640 MethodBuilder mb = tb.DefineMethod (
641 mname, MethodAttributes.Public, typeof (void),
642 new Type [] { typeof (int), typeof (string) });
644 // Create an attribute with default values
645 mb.SetCustomAttribute (new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) }), new object[] { "kernel32" }));
647 Type t = tb.CreateType ();
649 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
651 AssertEquals (CallingConvention.Winapi, attr.CallingConvention);
652 AssertEquals (mname, attr.EntryPoint);
653 AssertEquals ("kernel32", attr.Value);
654 AssertEquals (false, attr.ExactSpelling);
655 AssertEquals (true, attr.PreserveSig);
656 AssertEquals (false, attr.SetLastError);
657 AssertEquals (false, attr.BestFitMapping);
658 AssertEquals (false, attr.ThrowOnUnmappableChar);
662 public void SetCustomAttribute_DllImport2 () {
663 string mname = genMethodName ();
665 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
666 MethodBuilder mb = tb.DefineMethod (
667 mname, MethodAttributes.Public, typeof (void),
668 new Type [] { typeof (int), typeof (string) });
670 CustomAttributeBuilder cb = new CustomAttributeBuilder (typeof (DllImportAttribute).GetConstructor (new Type [] {typeof (String)}), new object [] { "foo" }, new FieldInfo [] {typeof (DllImportAttribute).GetField ("EntryPoint"), typeof (DllImportAttribute).GetField ("CallingConvention"), typeof (DllImportAttribute).GetField ("CharSet"), typeof (DllImportAttribute).GetField ("ExactSpelling"), typeof (DllImportAttribute).GetField ("PreserveSig")}, new object [] { "bar", CallingConvention.StdCall, CharSet.Unicode, true, false });
671 mb.SetCustomAttribute (cb);
673 Type t = tb.CreateType ();
675 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
677 AssertEquals (CallingConvention.StdCall, attr.CallingConvention);
678 AssertEquals (CharSet.Unicode, attr.CharSet);
679 AssertEquals ("bar", attr.EntryPoint);
680 AssertEquals ("foo", attr.Value);
681 AssertEquals (true, attr.ExactSpelling);
682 AssertEquals (false, attr.PreserveSig);
683 AssertEquals (false, attr.SetLastError);
684 AssertEquals (false, attr.BestFitMapping);
685 AssertEquals (false, attr.ThrowOnUnmappableChar);
689 public void SetCustomAttribute_DllImport3 () {
690 string mname = genMethodName ();
692 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
693 MethodBuilder mb = tb.DefineMethod (
694 mname, MethodAttributes.Public, typeof (void),
695 new Type [] { typeof (int), typeof (string) });
697 // Test attributes with three values (on/off/missing)
698 CustomAttributeBuilder cb = new CustomAttributeBuilder (typeof (DllImportAttribute).GetConstructor (new Type [] {typeof (String)}), new object [] { "foo" }, new FieldInfo [] { typeof (DllImportAttribute).GetField ("BestFitMapping"), typeof (DllImportAttribute).GetField ("ThrowOnUnmappableChar")}, new object [] { false, false });
699 mb.SetCustomAttribute (cb);
701 Type t = tb.CreateType ();
703 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
705 AssertEquals (false, attr.BestFitMapping);
706 AssertEquals (false, attr.ThrowOnUnmappableChar);
710 public void SetCustomAttribute_DllImport4 () {
711 string mname = genMethodName ();
713 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
714 MethodBuilder mb = tb.DefineMethod (
715 mname, MethodAttributes.Public, typeof (void),
716 new Type [] { typeof (int), typeof (string) });
718 CustomAttributeBuilder cb = new CustomAttributeBuilder (typeof (DllImportAttribute).GetConstructor (new Type [] {typeof (String)}), new object [] { "foo" }, new FieldInfo [] { typeof (DllImportAttribute).GetField ("SetLastError"), typeof (DllImportAttribute).GetField ("BestFitMapping"), typeof (DllImportAttribute).GetField ("ThrowOnUnmappableChar")}, new object [] { true, true, true });
719 mb.SetCustomAttribute (cb);
721 Type t = tb.CreateType ();
723 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
725 AssertEquals (true, attr.SetLastError);
726 AssertEquals (true, attr.BestFitMapping);
727 AssertEquals (true, attr.ThrowOnUnmappableChar);