1 // CustomAttributeBuilderTest.cs
\r
3 // Author: Vineeth N <nvineeth@yahoo.com>
\r
5 // (C) 2004 Ximian, Inc. http://www.ximian.com
\r
8 using System.Reflection;
\r
9 using System.Reflection.Emit;
\r
10 using System.Threading;
\r
11 using NUnit.Framework;
\r
13 namespace MonoTests.System.Reflection.Emit
\r
16 /// TestFixture for CustomAttributeBuilderTest.
\r
17 /// The members to be tested are as follows:
\r
19 /// 1) public CustomAttributeBuilder(ConstructorInfo, object[]);
\r
20 /// 2) public CustomAttributeBuilder(ConstructorInfo, object[], FieldInfo[], object[]);
\r
21 /// 3) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[]);
\r
22 /// 4) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
\r
23 /// and the exceptions that are thrown.
\r
24 /// In the implementation , it can be seen that the first
\r
25 /// three type of constructors call the 4th type of ctor, which takes 6 args
\r
26 /// by filling args and substituting null as required.
\r
27 /// For testing constructors we have use 4 different test functions,
\r
28 /// Various exceptions have been checked for 4th type of consturctor.
\r
32 public class CustomAttributeBuilderTest : Assertion
\r
35 // the CustomAttribute class is used for testing and it has to be public
\r
36 //since it will be associated with a class that belongs to another assembly
\r
38 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Class)]
\r
39 public class CustomAttribute: Attribute
\r
41 private string attr1;
\r
42 private string attr2;
\r
43 public string Feild; //used for testing the second type of constructor
\r
45 public CustomAttribute () {}
\r
46 public CustomAttribute (String s1 , String s2)
\r
52 private CustomAttribute (String s1) {}
\r
53 static CustomAttribute () {}
\r
55 public string AttributeOne
\r
57 get { return attr1; }
\r
58 set { attr1 = value; }
\r
61 public string AttributeTwo
\r
63 get { return attr2; }
\r
64 //the set is skipped and is used later in testing
\r
69 private class TempClass
\r
71 //used for testing the ArgumentException
\r
72 public string Field;
\r
73 public string FieldProperty
\r
75 get { return Field; }
\r
76 set { Field = value; }
\r
84 public void CtorOneTest ()
\r
86 //test for the constructor with signature--
\r
87 // public CustomAttributeBuilder(ConstructorInfo, object[]);
\r
89 * WE build a imaginary type as follows
\r
92 * [CustomAttribute("one","two")]
\r
93 * public string Str;
\r
95 * [CustomAttribute("hello","world")]
\r
96 * public void Print()
\r
97 * {Console.WriteLine("Hello World"); }
\r
100 * And then check for the validity of attributes in the test functions
\r
102 AssemblyName asmName = new AssemblyName ();
\r
103 asmName.Name = "TestAssembly.dll";
\r
105 AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
\r
106 asmName , AssemblyBuilderAccess.Run);
\r
108 ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
\r
110 TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
\r
111 TypeAttributes.Public);
\r
113 Type[] ctorParams = new Type[] { typeof (string),typeof (string) };
\r
115 ConstructorInfo classCtorInfo =
\r
116 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
118 CustomAttributeBuilder feildCABuilder = new CustomAttributeBuilder (
\r
120 new object [] { "one","two" }
\r
122 methodCABuilder = new CustomAttributeBuilder (
\r
124 new object [] { "hello","world" }
\r
126 //now let's build a feild of type string and associate a attribute with it
\r
127 FieldBuilder fieldBuilder= typeBuilder.DefineField ("Str",
\r
128 typeof (string), FieldAttributes.Public);
\r
129 fieldBuilder.SetCustomAttribute (feildCABuilder);
\r
130 //now build a method
\r
131 MethodBuilder methodBuilder= typeBuilder.DefineMethod ("Print",
\r
132 MethodAttributes.Public, null, null);
\r
133 methodBuilder.SetCustomAttribute (methodCABuilder);
\r
134 ILGenerator methodIL = methodBuilder.GetILGenerator ();
\r
135 methodIL.EmitWriteLine ("Hello, world!");
\r
136 methodIL.Emit (OpCodes.Ret);
\r
139 Type myType = typeBuilder.CreateType ();
\r
141 //Now check for the validity of the attributes.
\r
142 object testInstance = Activator.CreateInstance (myType);
\r
144 //check the validity of the attribute associated with Print method
\r
146 object [] methodAttrs = myType.GetMember ("Print") [0].GetCustomAttributes (true);
\r
147 AssertEquals ("#method Print has exactly one attribute", methodAttrs.Length, 1);
\r
148 CustomAttribute methodAttr = methodAttrs [0] as CustomAttribute;
\r
149 AssertEquals ("#AttributeOne", methodAttr.AttributeOne, "hello");
\r
150 AssertEquals ("#AttributeTwo", methodAttr.AttributeTwo, "world");
\r
152 //check the validity of the attribute associated with Str feild
\r
154 object [] fieldAttrs = myType.GetField ("Str").GetCustomAttributes (true);
\r
155 AssertEquals("#feild Str has exactly one attribute", fieldAttrs.Length, 1);
\r
156 CustomAttribute fieldAttr = fieldAttrs [0] as CustomAttribute;
\r
157 AssertEquals("#AttributeOne", fieldAttr.AttributeOne, "one");
\r
158 AssertEquals("#AttributeTwo", fieldAttr.AttributeTwo, "two");
\r
162 public void CtorTwoTest ()
\r
164 //test for the constructor with signature--
\r
165 // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], FieldInfo[], Object[]) ;
\r
167 * WE build a imaginary type as follows
\r
168 * [CustomAttribute("Test","Type")]
\r
169 * public class TestType
\r
173 * We also set the "Feild" of class CustomAttribute and the value;
\r
174 * And then check for the validity of attributes in the test functions
\r
177 AssemblyName asmName = new AssemblyName ();
\r
178 asmName.Name = "TestAssembly.dll";
\r
180 AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
\r
181 asmName, AssemblyBuilderAccess.Run);
\r
183 ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
\r
185 TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
\r
186 TypeAttributes.Public);
\r
188 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
190 ConstructorInfo classCtorInfo =
\r
191 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
193 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
195 new object [] { "Test","Type" },
\r
196 typeof(CustomAttribute).GetFields(),
\r
197 new object [] { "TestCase" }
\r
200 typeBuilder.SetCustomAttribute (typeCABuilder);
\r
203 Type myType = typeBuilder.CreateType ();
\r
205 //Now check for the validity of the attributes.
\r
206 object testInstance = Activator.CreateInstance (myType);
\r
208 //check the validity of the attribute associated with Print method
\r
209 object [] customAttrs = myType.GetCustomAttributes (true);
\r
210 AssertEquals ("#TestType has exactly one attribute", customAttrs.Length, 1);
\r
212 //Custom Attributes of TestType
\r
213 CustomAttribute attr = customAttrs [0] as CustomAttribute;
\r
214 AssertEquals ("#AttributeOne", attr.AttributeOne, "Test");
\r
215 AssertEquals ("#AttributeTwo", attr.AttributeTwo, "Type");
\r
216 AssertEquals ("#CustomAttribute.Feild",attr.Feild, "TestCase");
\r
221 public void CtorThreeTest ()
\r
223 //test for the constructor with signature--
\r
224 // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], PropertyInfo[], Object[]) ;
\r
226 * WE build a imaginary type as follows
\r
227 * [CustomAttribute()]
\r
228 * public class TestType
\r
232 * We also set the "AttributeOne" of class CustomAttribute by means of the constuctor
\r
233 * And then check for the validity of attribute state
\r
236 AssemblyName asmName = new AssemblyName ();
\r
237 asmName.Name = "TestAssembly.dll";
\r
239 AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
\r
240 asmName, AssemblyBuilderAccess.Run);
\r
242 ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
\r
244 TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
\r
245 TypeAttributes.Public);
\r
247 Type [] ctorParams = new Type [] { };
\r
249 ConstructorInfo classCtorInfo =
\r
250 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
252 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
255 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
256 new object [] { "TestCase" }
\r
259 typeBuilder.SetCustomAttribute (typeCABuilder);
\r
262 Type myType = typeBuilder.CreateType ();
\r
264 //Now check for the validity of the attributes.
\r
265 object testInstance = Activator.CreateInstance (myType);
\r
267 //check the validity of the attribute associated with Print method
\r
268 object [] customAttrs = myType.GetCustomAttributes (true);
\r
269 AssertEquals ("#TestType has exactly one attribute", customAttrs.Length , 1);
\r
271 //Custom Attributes of TestType
\r
272 CustomAttribute attr = customAttrs [0] as CustomAttribute;
\r
273 AssertEquals("#AttributeOne", attr.AttributeOne, "TestCase");
\r
277 public void CtorFourTest ()
\r
279 //test for the constructor with signature--
\r
280 //public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
\r
282 * WE build a imaginary type as follows
\r
283 * [CustomAttribute()]
\r
284 * public class TestType
\r
288 * We also set the "AttributeOne" property ,
\r
289 * and "Feild" of class CustomAttribute
\r
290 * by means of the constuctor of CustomAttributeBuilder
\r
291 * And then check for the validity
\r
294 AssemblyName asmName = new AssemblyName ();
\r
295 asmName.Name = "TestAssembly.dll";
\r
297 AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (
\r
298 asmName , AssemblyBuilderAccess.Run);
\r
300 ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");
\r
302 TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",
\r
303 TypeAttributes.Public);
\r
305 Type [] ctorParams = new Type [] { };
\r
307 ConstructorInfo classCtorInfo =
\r
308 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
310 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder(
\r
313 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
314 new object [] { "TestCase" },
\r
315 typeof(CustomAttribute).GetFields (),
\r
316 new object [] { "FieldValue" }
\r
319 typeBuilder.SetCustomAttribute (typeCABuilder);
\r
322 Type myType = typeBuilder.CreateType ();
\r
324 //Now check for the validity of the attributes.
\r
325 object testInstance = Activator.CreateInstance (myType);
\r
327 //check the validity of the attribute associated with Print method
\r
328 object [] customAttrs = myType.GetCustomAttributes (true);
\r
329 AssertEquals("#TestType has exactly one attribute",customAttrs.Length , 1);
\r
331 //Custom Attributes of TestType
\r
332 CustomAttribute attr = customAttrs [0] as CustomAttribute;
\r
333 AssertEquals ("#AttributeOne", attr.AttributeOne, "TestCase");
\r
334 AssertEquals ("#Field ", attr.Feild, "FieldValue");
\r
339 [ExpectedException (typeof (ArgumentException))]
\r
340 public void ArgumentExceptionTest_1 ()
\r
342 //here the constructor is static
\r
344 Type [] ctorParams = new Type [] { };
\r
346 ConstructorInfo classCtorInfo =
\r
347 typeof (CustomAttribute).GetConstructor (BindingFlags.Static | BindingFlags.NonPublic,
\r
348 null, ctorParams, null);
\r
350 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
353 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
354 new object [] { "TestCase" },
\r
355 typeof (CustomAttribute).GetFields (),
\r
356 new object [] { "FieldValue" }
\r
362 [ExpectedException (typeof (ArgumentException))]
\r
363 public void ArgumentExceptionTest_2 ()
\r
365 //here the consturctor is private
\r
367 Type [] ctorParams = new Type[] {typeof(string) };
\r
369 ConstructorInfo classCtorInfo =
\r
370 typeof (CustomAttribute).GetConstructor (BindingFlags.Instance |
\r
371 BindingFlags.NonPublic, null, ctorParams, null);
\r
373 Assert ("#Custom Attribute has private constuctor ", classCtorInfo != null);
\r
375 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
377 new object [] { "hello" },
\r
378 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
379 new object [] { "TestCase" },
\r
380 typeof (CustomAttribute).GetFields (),
\r
381 new object [] { "FieldValue" }
\r
387 [ExpectedException (typeof (ArgumentException))]
\r
388 public void ArgumentExceptionTest_3 ()
\r
390 // The lengths of the namedProperties and
\r
391 //propertyValues arrays are different.
\r
393 Type [] ctorParams = new Type [] { };
\r
395 ConstructorInfo classCtorInfo =
\r
396 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
398 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
401 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
402 new object [] { "TestCase","extra arg" },//<--here is the error
\r
403 typeof (CustomAttribute).GetFields (),
\r
404 new object [] { "FieldValue" }
\r
410 [ExpectedException (typeof (ArgumentException))]
\r
411 public void ArgumentExceptionTest_4()
\r
413 //The length of the namedFields and
\r
414 //namedValues are different
\r
416 Type [] ctorParams = new Type [] { };
\r
418 ConstructorInfo classCtorInfo =
\r
419 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
421 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
424 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
425 new object [] { "TestCase" },
\r
426 typeof (CustomAttribute).GetFields (),
\r
427 new object [] { }//<--here is the error
\r
433 [ExpectedException (typeof (ArgumentException))]
\r
434 public void ArgumentExceptionTest_5 ()
\r
436 //This does not throw on .NET 1.1, so manually throw
\r
437 if (Environment.Version.Major == 1 && Environment.Version.Major == 1)
\r
438 throw new ArgumentException();
\r
440 //The number of supplied arguments does not match
\r
441 //the number of parameters of the constructor as
\r
442 //required by the calling convention of the constructor
\r
444 Type [] ctorParams = new Type [] { };
\r
446 ConstructorInfo classCtorInfo =
\r
447 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
449 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
451 new object [] { "extra1", "extra2" }, //<--here is the error
\r
452 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
453 new object [] { "TestCase" },
\r
454 typeof (CustomAttribute).GetFields (),
\r
455 new object [] { "FeildValue" }
\r
462 [ExpectedException (typeof (ArgumentException))]
\r
463 public void ArgumentExceptionTest_6 ()
\r
465 //The type of supplied argument does not
\r
466 //match the type of the parameter declared
\r
467 //in the constructor.
\r
469 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
471 ConstructorInfo classCtorInfo =
\r
472 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
474 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
476 new object [] { "1", 123 },//<--here is the error,(int instead of string)
\r
477 new PropertyInfo[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
478 new object [] { "TestCase" },
\r
479 typeof (CustomAttribute).GetFields (),
\r
480 new object [] { "FeildValue" }
\r
486 [ExpectedException (typeof (ArgumentException))]
\r
487 public void ArgumentExceptionTest_7 ()
\r
489 //A property has no setter.(CustomAttribute.AttributeTwo)
\r
491 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
493 ConstructorInfo classCtorInfo =
\r
494 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
496 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
498 new object [] { "1","2" },
\r
499 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeTwo") },
\r
500 new object [] { "TestCase" },
\r
501 typeof (CustomAttribute).GetFields (),
\r
502 new object [] { "FeildValue" }
\r
508 [ExpectedException (typeof (ArgumentException))]
\r
509 public void ArgumentExceptionTest_8 ()
\r
511 //A property doesnot belong to same class
\r
513 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
515 ConstructorInfo classCtorInfo =
\r
516 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
518 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
520 new object [] { "1","2" },
\r
521 new PropertyInfo [] { typeof (TempClass).GetProperty ("FieldProperty")}, //here is the error
\r
522 new object [] { "TestCase" },
\r
523 typeof (CustomAttribute).GetFields (),
\r
524 new object [] { "FeildValue" }
\r
530 [ExpectedException (typeof (ArgumentException))]
\r
531 public void ArgumentExceptionTest_9 ()
\r
533 //A field doesnot belong to same class
\r
535 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
537 ConstructorInfo classCtorInfo =
\r
538 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
540 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
542 new object [] { "1","2" },
\r
543 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
544 new object [] {"TestCase"},
\r
545 typeof (TempClass).GetFields (), //<-- fields of TempClass are passed
\r
546 new object [] { "FeildValue" }
\r
552 [ExpectedException (typeof (ArgumentException))]
\r
553 public void ArgumentExceptionTest_10 ()
\r
555 //The types of the property values do
\r
556 //not match the types of the named properties.
\r
559 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
561 ConstructorInfo classCtorInfo =
\r
562 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
564 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
566 new object [] { "1","2" },
\r
567 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
568 new object [] { (long)1212121212 }, //<---type mismatch error(long for string)
\r
569 typeof (CustomAttribute).GetFields (),
\r
570 new object [] { "FeildValue" }
\r
576 [ExpectedException (typeof (ArgumentException))]
\r
577 public void ArgumentExceptionTest_11 ()
\r
579 //The types of the field values do
\r
580 //not match the types of the named properties.
\r
582 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
584 ConstructorInfo classCtorInfo =
\r
585 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
587 Assert ("#ctor not null", classCtorInfo != null);
\r
588 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
590 new object [] { "1","2" },
\r
591 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
592 new object [] { "One" },
\r
593 typeof (CustomAttribute).GetFields (),
\r
594 new object []{ 12.1212 } //<---type mismatch error(double for string)
\r
600 [ExpectedException (typeof (ArgumentNullException))]
\r
601 public void ArgumentNullException_1 ()
\r
603 //the ctor value array (2nd argument) is null
\r
604 Type [] ctorParams = new Type [] { typeof (string),typeof (string) };
\r
606 ConstructorInfo classCtorInfo =
\r
607 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
609 Assert ("#ctor not null", classCtorInfo != null);
\r
610 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
612 null, //<-- here is the error
\r
613 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
614 new object [] { "One" },
\r
615 typeof (CustomAttribute).GetFields (),
\r
616 new object [] { "feild" }
\r
622 [ExpectedException (typeof (ArgumentNullException))]
\r
623 public void ArgumentNullException_2 ()
\r
625 //the property value array (4th argument) is null
\r
626 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
628 ConstructorInfo classCtorInfo =
\r
629 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
631 Assert ("#ctor not null", classCtorInfo != null);
\r
632 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
634 new object [] { "one","two" },
\r
635 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
636 null, // <-- here is the error
\r
637 typeof (CustomAttribute).GetFields (),
\r
638 new object [] { "feild" }
\r
644 [ExpectedException (typeof (ArgumentNullException))]
\r
645 public void ArgumentNullException_3 ()
\r
647 //the field value array (6th argument) is null
\r
648 Type [] ctorParams = new Type [] { typeof (string), typeof (string) };
\r
650 ConstructorInfo classCtorInfo =
\r
651 typeof (CustomAttribute).GetConstructor (ctorParams);
\r
653 Assert ("#ctor not null", classCtorInfo != null);
\r
654 CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (
\r
656 new object [] { "one","two" },
\r
657 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },
\r
658 new object [] {"property"},
\r
659 typeof (CustomAttribute).GetFields (),
\r
660 null // <-- here is the error
\r