2 // ConstructorBuilderTest.cs - NUnit Test Cases for the ConstructorBuilder 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
.Security
;
17 using System
.Security
.Permissions
;
19 using NUnit
.Framework
;
21 namespace MonoTests
.System
.Reflection
.Emit
24 public class ConstructorBuilderTest
26 private TypeBuilder genClass
;
27 private ModuleBuilder module
;
29 private static int typeIndexer
= 0;
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
++);
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");
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");
78 public void DeclaringType ()
80 ConstructorBuilder cb
= genClass
.DefineConstructor (
83 Assert
.AreSame (genClass
, cb
.DeclaringType
);
87 public void InitLocals ()
89 ConstructorBuilder cb
= genClass
.DefineConstructor (
92 Assert
.IsTrue (cb
.InitLocals
, "#1");
93 cb
.InitLocals
= false;
94 Assert
.IsFalse (cb
.InitLocals
, "#2");
98 public void MethodHandle ()
100 ConstructorBuilder cb
= genClass
.DefineConstructor (
102 cb
.GetILGenerator ().Emit (OpCodes
.Ret
);
105 RuntimeMethodHandle handle
= cb
.MethodHandle
;
106 Assert
.Fail ("#A1:" + handle
);
107 } catch (NotSupportedException ex
) {
108 // The invoked member is not supported in a dynamic
110 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
111 Assert
.IsNull (ex
.InnerException
, "#A3");
112 Assert
.IsNotNull (ex
.Message
, "#A4");
115 genClass
.CreateType ();
118 RuntimeMethodHandle handle
= cb
.MethodHandle
;
119 Assert
.Fail ("#B1:" + handle
);
120 } catch (NotSupportedException ex
) {
121 // The invoked member is not supported in a dynamic
123 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#B2");
124 Assert
.IsNull (ex
.InnerException
, "#B3");
125 Assert
.IsNotNull (ex
.Message
, "#B4");
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");
141 public void TestReflectedType ()
143 ConstructorBuilder cb
= genClass
.DefineConstructor (0, 0, new Type
[0]);
145 Assert
.AreSame (genClass
, cb
.ReflectedType
);
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");
160 public void DefineParameter_Position_Negative ()
162 ConstructorBuilder cb
= genClass
.DefineConstructor (
163 0, 0, new Type
[2] { typeof (int), typeof (int) }
);
166 cb
.DefineParameter (-1, ParameterAttributes
.None
, "param1");
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");
179 public void DefineParameter_Position_Max ()
181 ConstructorBuilder cb
= genClass
.DefineConstructor (
182 0, 0, new Type
[2] { typeof (int), typeof (int) }
);
185 cb
.DefineParameter (3, 0, "param1");
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");
199 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=341439
201 public void DefineParameter_Position_Zero ()
203 ConstructorBuilder cb
= genClass
.DefineConstructor (
204 0, 0, new Type
[2] { typeof (int), typeof (int) }
);
207 cb
.DefineParameter (0, ParameterAttributes
.In
, "param1");
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");
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 ();
233 cb
.DefineParameter (1, 0, "param1");
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
);
251 cb
.GetCustomAttributes (true);
253 } catch (NotSupportedException ex
) {
254 // The invoked member is not supported in a dynamic
256 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
257 Assert
.IsNull (ex
.InnerException
, "#A3");
258 Assert
.IsNotNull (ex
.Message
, "#A4");
261 genClass
.CreateType ();
264 cb
.GetCustomAttributes (true);
266 } catch (NotSupportedException ex
) {
267 // The invoked member is not supported in a dynamic
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
);
283 cb
.GetCustomAttributes (null, true);
285 } catch (NotSupportedException ex
) {
286 // The invoked member is not supported in a dynamic
288 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
289 Assert
.IsNull (ex
.InnerException
, "#A3");
290 Assert
.IsNotNull (ex
.Message
, "#A4");
293 genClass
.CreateType ();
296 cb
.GetCustomAttributes (null, true);
298 } catch (NotSupportedException ex
) {
299 // The invoked member is not supported in a dynamic
301 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#B2");
302 Assert
.IsNull (ex
.InnerException
, "#B3");
303 Assert
.IsNotNull (ex
.Message
, "#B4");
308 public void TestMethodImplementationFlags ()
310 ConstructorBuilder cb
= genClass
.DefineConstructor (
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 (
324 cb2
.GetILGenerator ().Emit (OpCodes
.Ret
);
325 cb2
.SetImplementationFlags (MethodImplAttributes
.Managed
);
328 cb2
.SetImplementationFlags (MethodImplAttributes
.OPTIL
);
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");
339 public void GetModule ()
341 ConstructorBuilder cb
= genClass
.DefineConstructor (
344 Assert
.AreSame (module
, cb
.GetModule ());
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");
384 [Category ("NotDotNet")] // ArgumentNullException in GetParameters
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");
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
);
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");
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");
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 ();
437 Assert
.IsFalse (tokenA
== MethodToken
.Empty
, "#1");
439 Assert
.IsFalse (tokenA
.Token
== MethodToken
.Empty
.Token
, "#1");
442 genClass
.CreateType ();
444 MethodToken tokenB
= cb
.GetToken ();
446 Assert
.AreEqual (tokenA
, tokenB
, "#2");
448 Assert
.AreEqual (tokenA
.Token
, tokenB
.Token
, "#2");
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
);
460 cb
.Invoke (new object [1] { 42 }
);
462 } catch (NotSupportedException ex
) {
463 // The invoked member is not supported in a dynamic
465 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
466 Assert
.IsNull (ex
.InnerException
, "#A3");
467 Assert
.IsNotNull (ex
.Message
, "#A4");
470 genClass
.CreateType ();
473 cb
.Invoke (new object [1] { 42 }
);
475 } catch (NotSupportedException ex
) {
476 // The invoked member is not supported in a dynamic
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
);
492 cb
.Invoke (null, new object [1] { 42 }
);
494 } catch (NotSupportedException ex
) {
495 // The invoked member is not supported in a dynamic
497 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
498 Assert
.IsNull (ex
.InnerException
, "#A3");
499 Assert
.IsNotNull (ex
.Message
, "#A4");
502 genClass
.CreateType ();
505 cb
.Invoke (null, new object [1] { 42 }
);
507 } catch (NotSupportedException ex
) {
508 // The invoked member is not supported in a dynamic
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
);
524 cb
.Invoke (0, null, new object [1] { 42 }
, null);
526 } catch (NotSupportedException ex
) {
527 // The invoked member is not supported in a dynamic
529 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
530 Assert
.IsNull (ex
.InnerException
, "#A3");
531 Assert
.IsNotNull (ex
.Message
, "#A4");
534 genClass
.CreateType ();
537 cb
.Invoke (0, null, new object [1] { 42 }
, null);
539 } catch (NotSupportedException ex
) {
540 // The invoked member is not supported in a dynamic
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
);
556 cb
.Invoke (null, 0, null, new object [1] { 42 }
, null);
558 } catch (NotSupportedException ex
) {
559 // The invoked member is not supported in a dynamic
561 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
562 Assert
.IsNull (ex
.InnerException
, "#A3");
563 Assert
.IsNotNull (ex
.Message
, "#A4");
566 genClass
.CreateType ();
569 cb
.Invoke (null, 0, null, new object [1] { 42 }
, null);
571 } catch (NotSupportedException ex
) {
572 // The invoked member is not supported in a dynamic
574 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#B2");
575 Assert
.IsNull (ex
.InnerException
, "#B3");
576 Assert
.IsNotNull (ex
.Message
, "#B4");
581 public void IsDefined ()
583 ConstructorBuilder cb
= genClass
.DefineConstructor (
585 new Type
[1] {typeof(int)}
);
586 cb
.GetILGenerator ().Emit (OpCodes
.Ret
);
589 cb
.IsDefined (null, true);
591 } catch (NotSupportedException ex
) {
592 // The invoked member is not supported in a dynamic
594 Assert
.AreEqual (typeof (NotSupportedException
), ex
.GetType (), "#A2");
595 Assert
.IsNull (ex
.InnerException
, "#A3");
596 Assert
.IsNotNull (ex
.Message
, "#A4");
599 genClass
.CreateType ();
602 cb
.IsDefined (null, true);
604 } catch (NotSupportedException ex
) {
605 // The invoked member is not supported in a dynamic
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
632 cb
.SetCustomAttribute (null, new byte [2]);
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");
642 cb
.SetCustomAttribute (ctorInfo
, null);
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");
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
);
690 cb
.SetCustomAttribute ((CustomAttributeBuilder
) null);
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 ();
702 cb
.SetCustomAttribute ((CustomAttributeBuilder
) null);
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");
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 ();
765 cb
.GetCustomAttributes (false);
767 } catch (NotSupportedException ex
) {
768 // The invoked member is not supported in a dynamic
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" }
));
790 cb
.GetCustomAttributes (false);
792 } catch (NotSupportedException ex
) {
793 // The invoked member is not supported in a dynamic
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 ();
817 cb
.GetCustomAttributes (attrType
, false);
819 } catch (NotSupportedException ex
) {
820 // The invoked member is not supported in a dynamic
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" }
));
842 cb
.GetCustomAttributes (attrType
, false);
844 } catch (NotSupportedException ex
) {
845 // The invoked member is not supported in a dynamic
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
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
);
865 cb
.AddDeclarativeSecurity (SecurityAction
.Demand
, set);
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");
876 public void AddDeclarativeSecurity_PSet_Null ()
878 ConstructorBuilder cb
= genClass
.DefineConstructor (
879 MethodAttributes
.Public
, 0, new Type
[0]);
881 cb
.AddDeclarativeSecurity (SecurityAction
.Demand
, null);
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");
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
) {
905 cb
.AddDeclarativeSecurity (action
, set);
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");
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);
924 cb
.AddDeclarativeSecurity (SecurityAction
.Demand
, set);
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");