2010-03-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono-project.git] / mcs / class / corlib / Test / System.Reflection.Emit / ConstructorBuilderTest.cs
blob347189ddc56a459410048f2a337a65972f339166
1 //
2 // ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (C) Ximian, Inc. http://www.ximian.com
8 // TODO:
9 // - implement 'Signature' (what the hell it does???) and test it
10 // - implement Equals and test it
12 using System;
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Security;
17 using System.Security.Permissions;
19 using NUnit.Framework;
21 namespace MonoTests.System.Reflection.Emit
23 [TestFixture]
24 public class ConstructorBuilderTest
26 private TypeBuilder genClass;
27 private ModuleBuilder module;
29 private static int typeIndexer = 0;
31 [SetUp]
32 public void SetUp ()
34 AssemblyName assemblyName = new AssemblyName();
35 assemblyName.Name = "MonoTests.System.Reflection.Emit.ConstructorBuilderTest";
37 AssemblyBuilder assembly
38 = Thread.GetDomain().DefineDynamicAssembly(
39 assemblyName, AssemblyBuilderAccess.Run);
41 module = assembly.DefineDynamicModule("module1");
42 genClass = module.DefineType(genTypeName (), TypeAttributes.Public);
45 // Return a unique type name
46 private string genTypeName ()
48 return "class" + (typeIndexer ++);
51 [Test]
52 public void Attributes ()
54 ConstructorBuilder cb = genClass.DefineConstructor (
55 MethodAttributes.Public, 0, new Type [0]);
57 Assert.IsTrue ((cb.Attributes & MethodAttributes.Public) != 0, "#1");
58 Assert.IsTrue ((cb.Attributes & MethodAttributes.SpecialName) != 0, "#2");
61 [Test]
62 public void CallingConvention ()
64 /* This does not work under MS.NET
65 ConstructorBuilder cb3 = genClass.DefineConstructor (
66 0, CallingConventions.VarArgs, new Type [0]);
67 Assert.AreEqual (CallingConventions.VarArgs | CallingConventions.HasThis,
68 cb3.CallingConvention, "#1");
71 ConstructorBuilder cb4 = genClass.DefineConstructor (
72 MethodAttributes.Static, CallingConventions.Standard, new Type [0]);
73 Assert.AreEqual (CallingConventions.Standard,
74 cb4.CallingConvention, "#2");
77 [Test]
78 public void DeclaringType ()
80 ConstructorBuilder cb = genClass.DefineConstructor (
81 0, 0, new Type[0]);
83 Assert.AreSame (genClass, cb.DeclaringType);
86 [Test]
87 public void InitLocals ()
89 ConstructorBuilder cb = genClass.DefineConstructor (
90 0, 0, new Type[0]);
92 Assert.IsTrue (cb.InitLocals, "#1");
93 cb.InitLocals = false;
94 Assert.IsFalse (cb.InitLocals, "#2");
97 [Test]
98 public void MethodHandle ()
100 ConstructorBuilder cb = genClass.DefineConstructor (
101 0, 0, new Type [0]);
102 cb.GetILGenerator ().Emit (OpCodes.Ret);
104 try {
105 RuntimeMethodHandle handle = cb.MethodHandle;
106 Assert.Fail ("#A1:" + handle);
107 } catch (NotSupportedException ex) {
108 // The invoked member is not supported in a dynamic
109 // module
110 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
111 Assert.IsNull (ex.InnerException, "#A3");
112 Assert.IsNotNull (ex.Message, "#A4");
115 genClass.CreateType ();
117 try {
118 RuntimeMethodHandle handle = cb.MethodHandle;
119 Assert.Fail ("#B1:" + handle);
120 } catch (NotSupportedException ex) {
121 // The invoked member is not supported in a dynamic
122 // module
123 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
124 Assert.IsNull (ex.InnerException, "#B3");
125 Assert.IsNotNull (ex.Message, "#B4");
129 [Test]
130 public void Name ()
132 ConstructorBuilder cb;
134 cb = genClass.DefineConstructor (0, 0, new Type [0]);
135 Assert.AreEqual (".ctor", cb.Name, "#1");
136 cb = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);
137 Assert.AreEqual (".cctor", cb.Name, "#2");
140 [Test]
141 public void TestReflectedType ()
143 ConstructorBuilder cb = genClass.DefineConstructor (0, 0, new Type [0]);
145 Assert.AreSame (genClass, cb.ReflectedType);
148 [Test]
149 public void ReturnType ()
151 ConstructorBuilder cb;
153 cb = genClass.DefineConstructor (0, 0, new Type [] { typeof (string) });
154 Assert.IsNull (cb.ReturnType, "#1");
155 cb = genClass.DefineConstructor (MethodAttributes.Static, 0, new Type [0]);
156 Assert.IsNull (cb.ReturnType, "#2");
159 [Test]
160 public void DefineParameter_Position_Negative ()
162 ConstructorBuilder cb = genClass.DefineConstructor (
163 0, 0, new Type [2] { typeof (int), typeof (int) });
165 try {
166 cb.DefineParameter (-1, ParameterAttributes.None, "param1");
167 Assert.Fail ("#1");
168 } catch (ArgumentOutOfRangeException ex) {
169 // Specified argument was out of the range of valid values
170 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
171 Assert.IsNull (ex.ActualValue, "#3");
172 Assert.IsNull (ex.InnerException, "#4");
173 Assert.IsNotNull (ex.Message, "#5");
174 Assert.IsNotNull (ex.ParamName, "#6");
178 [Test]
179 public void DefineParameter_Position_Max ()
181 ConstructorBuilder cb = genClass.DefineConstructor (
182 0, 0, new Type [2] { typeof (int), typeof (int) });
184 try {
185 cb.DefineParameter (3, 0, "param1");
186 Assert.Fail ("#1");
187 } catch (ArgumentOutOfRangeException ex) {
188 // Specified argument was out of the range of valid values
189 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
190 Assert.IsNull (ex.ActualValue, "#3");
191 Assert.IsNull (ex.InnerException, "#4");
192 Assert.IsNotNull (ex.Message, "#5");
193 Assert.IsNotNull (ex.ParamName, "#6");
197 [Test]
198 #if NET_2_0
199 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=341439
200 #endif
201 public void DefineParameter_Position_Zero ()
203 ConstructorBuilder cb = genClass.DefineConstructor (
204 0, 0, new Type [2] { typeof (int), typeof (int) });
206 try {
207 cb.DefineParameter (0, ParameterAttributes.In, "param1");
208 Assert.Fail ("#1");
209 } catch (ArgumentOutOfRangeException ex) {
210 // Specified argument was out of the range of valid values
211 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
212 Assert.IsNull (ex.ActualValue, "#3");
213 Assert.IsNull (ex.InnerException, "#4");
214 Assert.IsNotNull (ex.Message, "#5");
215 Assert.IsNotNull (ex.ParamName, "#6");
219 [Test]
220 public void DefineParameter ()
222 ConstructorBuilder cb = genClass.DefineConstructor (
223 0, 0, new Type [2] { typeof(int), typeof(int) });
225 cb.DefineParameter (1, 0, "param1");
226 cb.DefineParameter (1, 0, "param1");
227 cb.DefineParameter (2, 0, null);
229 cb.GetILGenerator ().Emit (OpCodes.Ret);
230 genClass.CreateType ();
232 try {
233 cb.DefineParameter (1, 0, "param1");
234 Assert.Fail ("#1");
235 } catch (InvalidOperationException ex) {
236 // Unable to change after type has been created
237 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
238 Assert.IsNull (ex.InnerException, "#3");
239 Assert.IsNotNull (ex.Message, "#4");
243 [Test] // GetCustomAttributes (Boolean)
244 public void GetCustomAttributes1 ()
246 ConstructorBuilder cb = genClass.DefineConstructor (
247 0, 0, new Type [1] {typeof(int)});
248 cb.GetILGenerator ().Emit (OpCodes.Ret);
250 try {
251 cb.GetCustomAttributes (true);
252 Assert.Fail ("#A1");
253 } catch (NotSupportedException ex) {
254 // The invoked member is not supported in a dynamic
255 // module
256 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
257 Assert.IsNull (ex.InnerException, "#A3");
258 Assert.IsNotNull (ex.Message, "#A4");
261 genClass.CreateType ();
263 try {
264 cb.GetCustomAttributes (true);
265 Assert.Fail ("#B1");
266 } catch (NotSupportedException ex) {
267 // The invoked member is not supported in a dynamic
268 // module
269 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
270 Assert.IsNull (ex.InnerException, "#B3");
271 Assert.IsNotNull (ex.Message, "#B4");
275 [Test] // GetCustomAttributes (Type, Boolean)
276 public void GetCustomAttributes2 ()
278 ConstructorBuilder cb = genClass.DefineConstructor (
279 0, 0, new Type [1] { typeof (int) });
280 cb.GetILGenerator ().Emit (OpCodes.Ret);
282 try {
283 cb.GetCustomAttributes (null, true);
284 Assert.Fail ("#A1");
285 } catch (NotSupportedException ex) {
286 // The invoked member is not supported in a dynamic
287 // module
288 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
289 Assert.IsNull (ex.InnerException, "#A3");
290 Assert.IsNotNull (ex.Message, "#A4");
293 genClass.CreateType ();
295 try {
296 cb.GetCustomAttributes (null, true);
297 Assert.Fail ("#B1");
298 } catch (NotSupportedException ex) {
299 // The invoked member is not supported in a dynamic
300 // module
301 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
302 Assert.IsNull (ex.InnerException, "#B3");
303 Assert.IsNotNull (ex.Message, "#B4");
307 [Test]
308 public void TestMethodImplementationFlags ()
310 ConstructorBuilder cb = genClass.DefineConstructor (
311 0, 0, new Type [0]);
313 Assert.AreEqual (MethodImplAttributes.Managed | MethodImplAttributes.IL,
314 cb.GetMethodImplementationFlags (), "#A1");
315 cb.SetImplementationFlags (MethodImplAttributes.OPTIL);
316 Assert.AreEqual (MethodImplAttributes.OPTIL,
317 cb.GetMethodImplementationFlags (), "#A2");
319 // Can not be called on a created type
320 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
321 ConstructorBuilder cb2 = tb.DefineConstructor (
322 0, 0, new Type [0]);
324 cb2.GetILGenerator ().Emit (OpCodes.Ret);
325 cb2.SetImplementationFlags (MethodImplAttributes.Managed);
326 tb.CreateType ();
327 try {
328 cb2.SetImplementationFlags (MethodImplAttributes.OPTIL);
329 Assert.Fail ("#B1");
330 } catch (InvalidOperationException ex) {
331 // Unable to change after type has been created
332 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
333 Assert.IsNull (ex.InnerException, "#B3");
334 Assert.IsNotNull (ex.Message, "#B4");
338 [Test]
339 public void GetModule ()
341 ConstructorBuilder cb = genClass.DefineConstructor (
342 0, 0, new Type [0]);
344 Assert.AreSame (module, cb.GetModule ());
347 [Test]
348 public void GetParameters_Complete1 ()
350 ConstructorBuilder cb;
351 ParameterInfo [] parameters;
353 cb = genClass.DefineConstructor (MethodAttributes.Public,
354 CallingConventions.Standard,
355 new Type [] { typeof (int), typeof (string), typeof (bool) });
356 cb.DefineParameter (3, ParameterAttributes.In, "param3a");
357 cb.DefineParameter (3, ParameterAttributes.In, "param3b");
358 cb.DefineParameter (2, ParameterAttributes.Out, "param2");
359 cb.GetILGenerator ().Emit (OpCodes.Ret);
360 genClass.CreateType ();
362 parameters = cb.GetParameters ();
363 Assert.IsNotNull (parameters, "#A1");
364 Assert.AreEqual (3, parameters.Length, "#A2");
366 Assert.AreEqual (ParameterAttributes.None, parameters [0].Attributes, "#B1");
367 Assert.IsNull (parameters [0].Name, "#B2");
368 Assert.AreEqual (typeof (int), parameters [0].ParameterType, "#B3");
369 Assert.AreEqual (0, parameters [0].Position, "#B4");
371 Assert.AreEqual (ParameterAttributes.Out, parameters [1].Attributes, "#C1");
372 Assert.AreEqual ("param2", parameters [1].Name, "#C2");
373 Assert.AreEqual (typeof (string), parameters [1].ParameterType, "#C3");
374 Assert.AreEqual (1, parameters [1].Position, "#C4");
376 Assert.AreEqual (ParameterAttributes.In, parameters [2].Attributes, "#D1");
377 Assert.AreEqual ("param3b", parameters [2].Name, "#D2");
378 Assert.AreEqual (typeof (bool), parameters [2].ParameterType, "#D3");
379 Assert.AreEqual (2, parameters [2].Position, "#D4");
382 [Test]
383 #if ONLY_1_1
384 [Category ("NotDotNet")] // ArgumentNullException in GetParameters
385 #endif
386 public void GetParameters_Complete2 ()
388 ConstructorBuilder cb = genClass.DefineConstructor (
389 MethodAttributes.Public,
390 CallingConventions.Standard, null);
391 cb.GetILGenerator ().Emit (OpCodes.Ret);
392 genClass.CreateType ();
394 ParameterInfo [] parameters = cb.GetParameters ();
395 Assert.IsNotNull (parameters, "#1");
396 Assert.AreEqual (0, parameters.Length, "#2");
399 [Test]
400 public void GetParameters_Incomplete ()
402 ConstructorBuilder cb = genClass.DefineConstructor (
403 0, 0, new Type [2] { typeof (int), typeof (string) });
404 cb.DefineParameter (1, ParameterAttributes.In, "param1");
405 cb.DefineParameter (2, ParameterAttributes.In, "param2");
406 cb.GetILGenerator ().Emit (OpCodes.Ret);
408 try {
409 cb.GetParameters ();
410 Assert.Fail ("#1");
411 #if NET_2_0
412 } catch (NotSupportedException ex) {
413 // Type has not been created
414 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
415 Assert.IsNull (ex.InnerException, "#3");
416 Assert.IsNotNull (ex.Message, "#4");
418 #else
419 } catch (InvalidOperationException ex) {
420 // Type has not been created
421 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
422 Assert.IsNull (ex.InnerException, "#3");
423 Assert.IsNotNull (ex.Message, "#4");
425 #endif
428 [Test]
429 public void GetToken ()
431 ConstructorBuilder cb = genClass.DefineConstructor (
432 0, 0, new Type [1] { typeof(int) });
433 cb.GetILGenerator ().Emit (OpCodes.Ret);
435 MethodToken tokenA = cb.GetToken ();
436 #if NET_2_0
437 Assert.IsFalse (tokenA == MethodToken.Empty, "#1");
438 #else
439 Assert.IsFalse (tokenA.Token == MethodToken.Empty.Token, "#1");
440 #endif
442 genClass.CreateType ();
444 MethodToken tokenB = cb.GetToken ();
445 #if NET_2_0
446 Assert.AreEqual (tokenA, tokenB, "#2");
447 #else
448 Assert.AreEqual (tokenA.Token, tokenB.Token, "#2");
449 #endif
452 [Test] // Invoke (Object [])
453 public void Invoke1 ()
455 ConstructorBuilder cb = genClass.DefineConstructor (
456 0, 0, new Type [1] { typeof(int) });
457 cb.GetILGenerator ().Emit (OpCodes.Ret);
459 try {
460 cb.Invoke (new object [1] { 42 });
461 Assert.Fail ("#A1");
462 } catch (NotSupportedException ex) {
463 // The invoked member is not supported in a dynamic
464 // module
465 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
466 Assert.IsNull (ex.InnerException, "#A3");
467 Assert.IsNotNull (ex.Message, "#A4");
470 genClass.CreateType ();
472 try {
473 cb.Invoke (new object [1] { 42 });
474 Assert.Fail ("#B1");
475 } catch (NotSupportedException ex) {
476 // The invoked member is not supported in a dynamic
477 // module
478 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
479 Assert.IsNull (ex.InnerException, "#B3");
480 Assert.IsNotNull (ex.Message, "#B4");
484 [Test] // Invoke (Object, Object [])
485 public void Invoke2 ()
487 ConstructorBuilder cb = genClass.DefineConstructor (
488 0, 0, new Type [1] { typeof (int) });
489 cb.GetILGenerator ().Emit (OpCodes.Ret);
491 try {
492 cb.Invoke (null, new object [1] { 42 });
493 Assert.Fail ("#A1");
494 } catch (NotSupportedException ex) {
495 // The invoked member is not supported in a dynamic
496 // module
497 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
498 Assert.IsNull (ex.InnerException, "#A3");
499 Assert.IsNotNull (ex.Message, "#A4");
502 genClass.CreateType ();
504 try {
505 cb.Invoke (null, new object [1] { 42 });
506 Assert.Fail ("#B1");
507 } catch (NotSupportedException ex) {
508 // The invoked member is not supported in a dynamic
509 // module
510 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
511 Assert.IsNull (ex.InnerException, "#B3");
512 Assert.IsNotNull (ex.Message, "#B4");
516 [Test] // Invoke (BindingFlags, Binder, Object [], CultureInfo)
517 public void Invoke3 ()
519 ConstructorBuilder cb = genClass.DefineConstructor (
520 0, 0, new Type [1] { typeof (int) });
521 cb.GetILGenerator ().Emit (OpCodes.Ret);
523 try {
524 cb.Invoke (0, null, new object [1] { 42 }, null);
525 Assert.Fail ("#A1");
526 } catch (NotSupportedException ex) {
527 // The invoked member is not supported in a dynamic
528 // module
529 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
530 Assert.IsNull (ex.InnerException, "#A3");
531 Assert.IsNotNull (ex.Message, "#A4");
534 genClass.CreateType ();
536 try {
537 cb.Invoke (0, null, new object [1] { 42 }, null);
538 Assert.Fail ("#B1");
539 } catch (NotSupportedException ex) {
540 // The invoked member is not supported in a dynamic
541 // module
542 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
543 Assert.IsNull (ex.InnerException, "#B3");
544 Assert.IsNotNull (ex.Message, "#B4");
548 [Test] // Invoke (Object, BindingFlags, Binder, Object [], CultureInfo)
549 public void Invoke4 ()
551 ConstructorBuilder cb = genClass.DefineConstructor (
552 0, 0, new Type [1] { typeof (int) });
553 cb.GetILGenerator ().Emit (OpCodes.Ret);
555 try {
556 cb.Invoke (null, 0, null, new object [1] { 42 }, null);
557 Assert.Fail ("#A1");
558 } catch (NotSupportedException ex) {
559 // The invoked member is not supported in a dynamic
560 // module
561 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
562 Assert.IsNull (ex.InnerException, "#A3");
563 Assert.IsNotNull (ex.Message, "#A4");
566 genClass.CreateType ();
568 try {
569 cb.Invoke (null, 0, null, new object [1] { 42 }, null);
570 Assert.Fail ("#B1");
571 } catch (NotSupportedException ex) {
572 // The invoked member is not supported in a dynamic
573 // module
574 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
575 Assert.IsNull (ex.InnerException, "#B3");
576 Assert.IsNotNull (ex.Message, "#B4");
580 [Test]
581 public void IsDefined ()
583 ConstructorBuilder cb = genClass.DefineConstructor (
584 0, 0,
585 new Type [1] {typeof(int)});
586 cb.GetILGenerator ().Emit (OpCodes.Ret);
588 try {
589 cb.IsDefined (null, true);
590 Assert.Fail ("#A1");
591 } catch (NotSupportedException ex) {
592 // The invoked member is not supported in a dynamic
593 // module
594 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
595 Assert.IsNull (ex.InnerException, "#A3");
596 Assert.IsNotNull (ex.Message, "#A4");
599 genClass.CreateType ();
601 try {
602 cb.IsDefined (null, true);
603 Assert.Fail ("#B1");
604 } catch (NotSupportedException ex) {
605 // The invoked member is not supported in a dynamic
606 // module
607 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
608 Assert.IsNull (ex.InnerException, "#B3");
609 Assert.IsNotNull (ex.Message, "#B4");
613 [Test] // SetCustomAttribute (ConstructorInfo, Byte [])
614 public void SetCustomAttribute1 ()
616 ConstructorBuilder cb = genClass.DefineConstructor (
617 0, 0, new Type [1] {typeof(int)});
618 cb.GetILGenerator ().Emit (OpCodes.Ret);
620 byte[] custAttrData = { 1, 0, 0, 0, 0};
621 Type attrType = Type.GetType
622 ("System.Reflection.AssemblyKeyNameAttribute");
623 Type[] paramTypes = new Type[1];
624 paramTypes[0] = typeof(String);
625 ConstructorInfo ctorInfo =
626 attrType.GetConstructor(paramTypes);
628 cb.SetCustomAttribute (ctorInfo, custAttrData);
630 // Null arguments again
631 try {
632 cb.SetCustomAttribute (null, new byte [2]);
633 Assert.Fail ("#B1");
634 } catch (ArgumentNullException ex) {
635 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
636 Assert.IsNull (ex.InnerException, "#B3");
637 Assert.IsNotNull (ex.Message, "#B4");
638 Assert.AreEqual ("con", ex.ParamName, "#B5");
641 try {
642 cb.SetCustomAttribute (ctorInfo, null);
643 Assert.Fail ("#C1");
644 } catch (ArgumentNullException ex) {
645 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
646 Assert.IsNull (ex.InnerException, "#C3");
647 Assert.IsNotNull (ex.Message, "#C4");
648 Assert.AreEqual ("binaryAttribute", ex.ParamName, "#C5");
652 [Test] // SetCustomAttribute (CustomAttributeBuilder)
653 public void SetCustomAttribute2 ()
655 ConstructorBuilder cb = genClass.DefineConstructor (
656 MethodAttributes.Public, CallingConventions.Standard,
657 new Type [1] { typeof (int) });
658 cb.GetILGenerator ().Emit (OpCodes.Ret);
660 TypeBuilder attrTb = module.DefineType ("TestAttribute",
661 TypeAttributes.Public, typeof (Attribute));
662 ConstructorBuilder attrCb = attrTb.DefineDefaultConstructor (
663 MethodAttributes.Public);
665 CustomAttributeBuilder cab = new CustomAttributeBuilder (
666 attrCb, new object [0]);
667 cb.SetCustomAttribute (cab);
668 attrTb.CreateType ();
670 Type emittedType = genClass.CreateType ();
671 ConstructorInfo ci = emittedType.GetConstructor (
672 new Type [1] { typeof (int) });
674 Assert.IsNotNull (ci, "#1");
675 object [] cas = ci.GetCustomAttributes (false);
676 Assert.IsNotNull (cas, "#2");
677 Assert.AreEqual (1, cas.Length, "#3");
678 Assert.AreEqual ("TestAttribute", cas [0].GetType ().FullName, "#4");
681 [Test]
682 public void SetCustomAttribute2_CustomBuilder_Null ()
684 ConstructorBuilder cb = genClass.DefineConstructor (
685 MethodAttributes.Public, CallingConventions.Standard,
686 new Type [1] { typeof (int) });
687 cb.GetILGenerator ().Emit (OpCodes.Ret);
689 try {
690 cb.SetCustomAttribute ((CustomAttributeBuilder) null);
691 Assert.Fail ("#A1");
692 } catch (ArgumentNullException ex) {
693 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
694 Assert.IsNull (ex.InnerException, "#A3");
695 Assert.IsNotNull (ex.Message, "#A4");
696 Assert.AreEqual ("customBuilder", ex.ParamName, "#A5");
699 genClass.CreateType ();
701 try {
702 cb.SetCustomAttribute ((CustomAttributeBuilder) null);
703 Assert.Fail ("#B1");
704 } catch (ArgumentNullException ex) {
705 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
706 Assert.IsNull (ex.InnerException, "#B3");
707 Assert.IsNotNull (ex.Message, "#B4");
708 Assert.AreEqual ("customBuilder", ex.ParamName, "#B5");
712 [Test]
713 public void GetCustomAttributes_Emitted ()
715 ConstructorBuilder cb = genClass.DefineConstructor (
716 MethodAttributes.Public, 0,
717 new Type [1] {typeof(int)});
718 cb.GetILGenerator ().Emit (OpCodes.Ret);
720 Type attrType = typeof (ObsoleteAttribute);
721 ConstructorInfo ctorInfo =
722 attrType.GetConstructor (new Type [] { typeof (String) });
724 cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
726 Type t = genClass.CreateType ();
728 // Try the created type
730 ConstructorInfo ci = t.GetConstructors () [0];
731 object[] attrs = ci.GetCustomAttributes (true);
733 Assert.AreEqual (1, attrs.Length, "#A1");
734 Assert.IsTrue (attrs [0] is ObsoleteAttribute, "#A2");
735 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message, "#A3");
738 // Try the type builder
740 ConstructorInfo ci = genClass.GetConstructors () [0];
741 object[] attrs = ci.GetCustomAttributes (true);
743 Assert.AreEqual (1, attrs.Length, "#B1");
744 Assert.IsTrue (attrs [0] is ObsoleteAttribute, "#B2");
745 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message, "#B3");
749 [Test] // GetCustomAttributes (Boolean)
750 public void GetCustomAttributes1_Complete ()
752 ConstructorBuilder cb = genClass.DefineConstructor (
753 MethodAttributes.Public, 0,
754 new Type [1] { typeof (int) });
755 cb.GetILGenerator ().Emit (OpCodes.Ret);
757 Type attrType = typeof (ObsoleteAttribute);
758 ConstructorInfo ctorInfo =
759 attrType.GetConstructor (new Type [] { typeof (String) });
760 cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
762 genClass.CreateType ();
764 try {
765 cb.GetCustomAttributes (false);
766 Assert.Fail ("#1");
767 } catch (NotSupportedException ex) {
768 // The invoked member is not supported in a dynamic
769 // module
770 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
771 Assert.IsNull (ex.InnerException, "#3");
772 Assert.IsNotNull (ex.Message, "#4");
776 [Test] // GetCustomAttributes (Boolean)
777 public void GetCustomAttributes1_Incomplete ()
779 ConstructorBuilder cb = genClass.DefineConstructor (
780 MethodAttributes.Public, 0,
781 new Type [1] { typeof (int) });
782 cb.GetILGenerator ().Emit (OpCodes.Ret);
784 Type attrType = typeof (ObsoleteAttribute);
785 ConstructorInfo ctorInfo =
786 attrType.GetConstructor (new Type [] { typeof (String) });
787 cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
789 try {
790 cb.GetCustomAttributes (false);
791 Assert.Fail ("#1");
792 } catch (NotSupportedException ex) {
793 // The invoked member is not supported in a dynamic
794 // module
795 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
796 Assert.IsNull (ex.InnerException, "#3");
797 Assert.IsNotNull (ex.Message, "#4");
801 [Test] // GetCustomAttributes (Type, Boolean)
802 public void GetCustomAttributes2_Complete ()
804 ConstructorBuilder cb = genClass.DefineConstructor (
805 MethodAttributes.Public, 0,
806 new Type [1] { typeof (int) });
807 cb.GetILGenerator ().Emit (OpCodes.Ret);
809 Type attrType = typeof (ObsoleteAttribute);
810 ConstructorInfo ctorInfo =
811 attrType.GetConstructor (new Type [] { typeof (String) });
812 cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
814 genClass.CreateType ();
816 try {
817 cb.GetCustomAttributes (attrType, false);
818 Assert.Fail ("#1");
819 } catch (NotSupportedException ex) {
820 // The invoked member is not supported in a dynamic
821 // module
822 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
823 Assert.IsNull (ex.InnerException, "#3");
824 Assert.IsNotNull (ex.Message, "#4");
828 [Test] // GetCustomAttributes (Type, Boolean)
829 public void GetCustomAttributes2_Incomplete ()
831 ConstructorBuilder cb = genClass.DefineConstructor (
832 MethodAttributes.Public, 0,
833 new Type [1] { typeof (int) });
834 cb.GetILGenerator ().Emit (OpCodes.Ret);
836 Type attrType = typeof (ObsoleteAttribute);
837 ConstructorInfo ctorInfo =
838 attrType.GetConstructor (new Type [] { typeof (String) });
839 cb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
841 try {
842 cb.GetCustomAttributes (attrType, false);
843 Assert.Fail ("#1");
844 } catch (NotSupportedException ex) {
845 // The invoked member is not supported in a dynamic
846 // module
847 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
848 Assert.IsNull (ex.InnerException, "#3");
849 Assert.IsNotNull (ex.Message, "#4");
853 // Same as in MethodBuilderTest
854 [Test]
855 public void AddDeclarativeSecurity_Complete ()
857 ConstructorBuilder cb = genClass.DefineConstructor (
858 MethodAttributes.Public, 0, new Type [0]);
859 ILGenerator ilgen = cb.GetILGenerator ();
860 ilgen.Emit (OpCodes.Ret);
861 genClass.CreateType ();
863 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
864 try {
865 cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
866 Assert.Fail ("#1");
867 } catch (InvalidOperationException ex) {
868 // Type has not been created
869 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
870 Assert.IsNull (ex.InnerException, "#3");
871 Assert.IsNotNull (ex.Message, "#4");
875 [Test]
876 public void AddDeclarativeSecurity_PSet_Null ()
878 ConstructorBuilder cb = genClass.DefineConstructor (
879 MethodAttributes.Public, 0, new Type [0]);
880 try {
881 cb.AddDeclarativeSecurity (SecurityAction.Demand, null);
882 Assert.Fail ("#1");
883 } catch (ArgumentNullException ex) {
884 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
885 Assert.IsNull (ex.InnerException, "#3");
886 Assert.IsNotNull (ex.Message, "#4");
887 Assert.AreEqual ("pset", ex.ParamName, "#5");
891 [Test]
892 public void AddDeclarativeSecurity_Action_Invalid ()
894 ConstructorBuilder cb = genClass.DefineConstructor (
895 MethodAttributes.Public, 0, new Type [0]);
897 SecurityAction[] actions = new SecurityAction [] {
898 SecurityAction.RequestMinimum,
899 SecurityAction.RequestOptional,
900 SecurityAction.RequestRefuse };
901 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
903 foreach (SecurityAction action in actions) {
904 try {
905 cb.AddDeclarativeSecurity (action, set);
906 Assert.Fail ("#1");
907 } catch (ArgumentOutOfRangeException ex) {
908 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
909 Assert.IsNull (ex.ActualValue, "#3");
910 Assert.IsNotNull (ex.Message, "#4");
911 Assert.AreEqual ("action", ex.ParamName, "#5");
916 [Test]
917 public void AddDeclarativeSecurity_Action_Duplicate ()
919 ConstructorBuilder cb = genClass.DefineConstructor (
920 MethodAttributes.Public, 0, new Type [0]);
921 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
922 cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
923 try {
924 cb.AddDeclarativeSecurity (SecurityAction.Demand, set);
925 Assert.Fail ("#1");
926 } catch (InvalidOperationException ex) {
927 // Type has not been created
928 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
929 Assert.IsNull (ex.InnerException, "#3");
930 Assert.IsNotNull (ex.Message, "#4");