2 // ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder 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
11 // - AddDeclarativeSecurity
\r
14 using System.Threading;
\r
15 using System.Reflection;
\r
16 using System.Reflection.Emit;
\r
18 using NUnit.Framework;
\r
20 namespace MonoTests.System.Reflection.Emit
\r
24 public class ConstructorBuilderTest : Assertion
\r
26 private TypeBuilder genClass;
\r
28 private ModuleBuilder module;
\r
31 protected void SetUp () {
\r
32 AssemblyName assemblyName = new AssemblyName();
\r
33 assemblyName.Name = "MonoTests.System.Reflection.Emit.ConstructorBuilderTest";
\r
35 AssemblyBuilder assembly
\r
36 = Thread.GetDomain().DefineDynamicAssembly(
\r
37 assemblyName, AssemblyBuilderAccess.Run);
\r
39 module = assembly.DefineDynamicModule("module1");
\r
41 genClass = module.DefineType("class1",
\r
42 TypeAttributes.Public);
\r
45 public void TestAttributes () {
\r
46 ConstructorBuilder cb = genClass.DefineConstructor (
\r
47 MethodAttributes.Public, 0, new Type [0]);
\r
49 AssertEquals ("Attributes works",
\r
50 cb.Attributes, MethodAttributes.Public | MethodAttributes.SpecialName);
\r
53 public void TestCallingConvention () {
\r
54 /* This does not work under MS.NET
\r
55 ConstructorBuilder cb3 = genClass.DefineConstructor (
\r
56 0, CallingConventions.VarArgs, new Type [0]);
\r
57 AssertEquals ("CallingConvetion works",
\r
58 CallingConventions.VarArgs | CallingConventions.HasThis,
\r
59 cb3.CallingConvention);
\r
62 ConstructorBuilder cb4 = genClass.DefineConstructor (
\r
63 MethodAttributes.Static, CallingConventions.Standard, new Type [0]);
\r
64 AssertEquals ("Static implies !HasThis",
\r
65 cb4.CallingConvention,
\r
66 CallingConventions.Standard);
\r
69 public void TestDeclaringType () {
\r
70 ConstructorBuilder cb = genClass.DefineConstructor (
\r
73 AssertEquals ("DeclaringType works",
\r
74 cb.DeclaringType, genClass);
\r
77 public void TestInitLocals () {
\r
78 ConstructorBuilder cb = genClass.DefineConstructor (
\r
81 AssertEquals ("InitLocals defaults to true", cb.InitLocals, true);
\r
82 cb.InitLocals = false;
\r
83 AssertEquals ("InitLocals is settable", cb.InitLocals, false);
\r
86 public void TestMethodHandle () {
\r
87 ConstructorBuilder cb = genClass.DefineConstructor (
\r
88 0, 0, new Type [0]);
\r
91 RuntimeMethodHandle handle = cb.MethodHandle;
\r
93 } catch (NotSupportedException) {
\r
97 public void TestName () {
\r
98 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
\r
100 AssertEquals ("Name works", ".ctor", cb.Name);
\r
102 ConstructorBuilder cb2 = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);
\r
103 AssertEquals ("Static constructors have the right name", ".cctor", cb2.Name);
\r
106 public void TestReflectedType () {
\r
107 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
\r
109 AssertEquals ("ReflectedType works",
\r
110 genClass, cb.ReflectedType);
\r
113 public void TestReturnType () {
\r
114 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
\r
116 AssertEquals ("ReturnType works",
\r
117 null, cb.ReturnType);
\r
120 public void TestDefineParameter () {
\r
121 TypeBuilder tb = module.DefineType ("class7", TypeAttributes.Public);
\r
122 ConstructorBuilder cb = tb.DefineConstructor (
\r
123 0, 0, new Type [2] { typeof(int), typeof(int) });
\r
125 // index out of range
\r
127 cb.DefineParameter (0, 0, "param1");
\r
129 } catch (ArgumentOutOfRangeException) {
\r
132 cb.DefineParameter (3, 0, "param1");
\r
134 } catch (ArgumentOutOfRangeException) {
\r
138 cb.DefineParameter (1, 0, "param1");
\r
139 cb.DefineParameter (1, 0, "param1");
\r
140 cb.DefineParameter (2, 0, null);
\r
142 // Can not be called on a created type
\r
143 cb.GetILGenerator ().Emit (OpCodes.Ret);
\r
146 cb.DefineParameter (1, 0, "param1");
\r
149 catch (InvalidOperationException) {
\r
153 public void TestGetCustomAttributes () {
\r
154 ConstructorBuilder cb = genClass.DefineConstructor (
\r
155 0, 0, new Type [1] {typeof(int)});
\r
158 cb.GetCustomAttributes (true);
\r
160 } catch (NotSupportedException) {
\r
164 cb.GetCustomAttributes (null, true);
\r
166 } catch (NotSupportedException) {
\r
170 public void TestMethodImplementationFlags () {
\r
171 ConstructorBuilder cb = genClass.DefineConstructor (
\r
172 0, 0, new Type [0]);
\r
174 AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
\r
175 cb.GetMethodImplementationFlags (),
\r
176 MethodImplAttributes.Managed | MethodImplAttributes.IL);
\r
178 cb.SetImplementationFlags (MethodImplAttributes.OPTIL);
\r
180 AssertEquals ("SetImplementationFlags works",
\r
181 cb.GetMethodImplementationFlags (),
\r
182 MethodImplAttributes.OPTIL);
\r
184 // Can not be called on a created type
\r
185 TypeBuilder tb = module.DefineType ("class14", TypeAttributes.Public);
\r
186 ConstructorBuilder cb2 = tb.DefineConstructor (
\r
187 0, 0, new Type [0]);
\r
189 cb2.GetILGenerator ().Emit (OpCodes.Ret);
\r
190 cb2.SetImplementationFlags (MethodImplAttributes.Managed);
\r
193 cb2.SetImplementationFlags (MethodImplAttributes.OPTIL);
\r
196 catch (InvalidOperationException) {
\r
200 public void TestGetModule () {
\r
201 ConstructorBuilder cb = genClass.DefineConstructor (
\r
202 0, 0, new Type [0]);
\r
204 AssertEquals ("GetModule works",
\r
205 module, cb.GetModule ());
\r
208 public void TestGetParameters () {
\r
209 TypeBuilder tb = module.DefineType ("class16", TypeAttributes.Public);
\r
210 ConstructorBuilder cb = tb.DefineConstructor (
\r
211 0, 0, new Type [1] {typeof(int)});
\r
212 cb.GetILGenerator ().Emit (OpCodes.Ret);
\r
214 // Can't be called before CreateType ()
\r
215 /* This does not work under mono
\r
217 cb.GetParameters ();
\r
219 } catch (InvalidOperationException) {
\r
225 /* This does not work under MS.NET !
\r
226 cb.GetParameters ();
\r
230 public void TestGetToken () {
\r
231 TypeBuilder tb = module.DefineType ("class17", TypeAttributes.Public);
\r
232 ConstructorBuilder cb = tb.DefineConstructor (
\r
233 0, 0, new Type [1] {typeof(void)});
\r
238 public void TestInvoke () {
\r
239 ConstructorBuilder cb = genClass.DefineConstructor (
\r
241 new Type [1] {typeof(int)});
\r
244 cb.Invoke (null, new object [1] { 42 });
\r
246 } catch (NotSupportedException) {
\r
250 cb.Invoke (null, 0, null, new object [1] { 42 }, null);
\r
252 } catch (NotSupportedException) {
\r
256 public void TestIsDefined () {
\r
257 ConstructorBuilder cb = genClass.DefineConstructor (
\r
259 new Type [1] {typeof(int)});
\r
262 cb.IsDefined (null, true);
\r
264 } catch (NotSupportedException) {
\r
268 public void TestSetCustomAttribute () {
\r
269 TypeBuilder tb = module.DefineType ("class21", TypeAttributes.Public);
\r
270 ConstructorBuilder cb = tb.DefineConstructor (
\r
272 new Type [1] {typeof(int)});
\r
273 cb.GetILGenerator ().Emit (OpCodes.Ret);
\r
277 cb.SetCustomAttribute (null);
\r
279 } catch (ArgumentNullException) {
\r
282 byte[] custAttrData = { 1, 0, 0, 0, 0};
\r
283 Type attrType = Type.GetType
\r
284 ("System.Reflection.AssemblyKeyNameAttribute");
\r
285 Type[] paramTypes = new Type[1];
\r
286 paramTypes[0] = typeof(String);
\r
287 ConstructorInfo ctorInfo =
\r
288 attrType.GetConstructor(paramTypes);
\r
290 cb.SetCustomAttribute (ctorInfo, custAttrData);
\r
292 // Null arguments again
\r
294 cb.SetCustomAttribute (null, new byte[2]);
\r
296 } catch (ArgumentNullException) {
\r
300 cb.SetCustomAttribute (ctorInfo, null);
\r
302 } catch (ArgumentNullException) {
\r