1 // CustomAttributeBuilderTest.cs
3 // Author: Vineeth N <nvineeth@yahoo.com>
5 // (C) 2004 Ximian, Inc. http://www.ximian.com
8 using System
.Reflection
;
9 using System
.Reflection
.Emit
;
10 using System
.Threading
;
11 using NUnit
.Framework
;
13 namespace MonoTests
.System
.Reflection
.Emit
16 /// TestFixture for CustomAttributeBuilderTest.
17 /// The members to be tested are as follows:
19 /// 1) public CustomAttributeBuilder(ConstructorInfo, object[]);
20 /// 2) public CustomAttributeBuilder(ConstructorInfo, object[], FieldInfo[], object[]);
21 /// 3) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[]);
22 /// 4) public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
23 /// and the exceptions that are thrown.
24 /// In the implementation , it can be seen that the first
25 /// three type of constructors call the 4th type of ctor, which takes 6 args
26 /// by filling args and substituting null as required.
27 /// For testing constructors we have use 4 different test functions,
28 /// Various exceptions have been checked for 4th type of consturctor.
32 public class CustomAttributeBuilderTest
35 // the CustomAttribute class is used for testing and it has to be public
36 //since it will be associated with a class that belongs to another assembly
38 [AttributeUsage(AttributeTargets
.Method
| AttributeTargets
.Field
| AttributeTargets
.Class
)]
39 public class CustomAttribute
: Attribute
43 public string Feild
; //used for testing the second type of constructor
45 public CustomAttribute () {}
46 public CustomAttribute (String s1
, String s2
)
52 private CustomAttribute (String s1
) {}
53 static CustomAttribute () {}
55 public string AttributeOne
58 set { attr1 = value; }
61 public string AttributeTwo
64 //the set is skipped and is used later in testing
69 private class TempClass
71 //used for testing the ArgumentException
73 public string FieldProperty
76 set { Field = value; }
84 public void CtorOneTest ()
86 //test for the constructor with signature--
87 // public CustomAttributeBuilder(ConstructorInfo, object[]);
89 * WE build a imaginary type as follows
92 * [CustomAttribute("one","two")]
95 * [CustomAttribute("hello","world")]
97 * {Console.WriteLine("Hello World"); }
100 * And then check for the validity of attributes in the test functions
102 AssemblyName asmName
= new AssemblyName ();
103 asmName
.Name
= "TestAssembly.dll";
105 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
106 asmName
, AssemblyBuilderAccess
.Run
);
108 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
110 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
111 TypeAttributes
.Public
);
113 Type
[] ctorParams
= new Type
[] { typeof (string),typeof (string) }
;
115 ConstructorInfo classCtorInfo
=
116 typeof (CustomAttribute
).GetConstructor (ctorParams
);
118 CustomAttributeBuilder feildCABuilder
= new CustomAttributeBuilder (
120 new object [] { "one","two" }
122 methodCABuilder
= new CustomAttributeBuilder (
124 new object [] { "hello","world" }
126 //now let's build a feild of type string and associate a attribute with it
127 FieldBuilder fieldBuilder
= typeBuilder
.DefineField ("Str",
128 typeof (string), FieldAttributes
.Public
);
129 fieldBuilder
.SetCustomAttribute (feildCABuilder
);
131 MethodBuilder methodBuilder
= typeBuilder
.DefineMethod ("Print",
132 MethodAttributes
.Public
, null, null);
133 methodBuilder
.SetCustomAttribute (methodCABuilder
);
134 ILGenerator methodIL
= methodBuilder
.GetILGenerator ();
135 methodIL
.EmitWriteLine ("Hello, world!");
136 methodIL
.Emit (OpCodes
.Ret
);
139 Type myType
= typeBuilder
.CreateType ();
141 //Now check for the validity of the attributes.
142 object testInstance
= Activator
.CreateInstance (myType
);
144 //check the validity of the attribute associated with Print method
146 object [] methodAttrs
= myType
.GetMember ("Print") [0].GetCustomAttributes (true);
147 Assert
.AreEqual (methodAttrs
.Length
, 1, "#1");
148 CustomAttribute methodAttr
= methodAttrs
[0] as CustomAttribute
;
149 Assert
.AreEqual (methodAttr
.AttributeOne
, "hello", "#2");
150 Assert
.AreEqual (methodAttr
.AttributeTwo
, "world", "#3");
152 //check the validity of the attribute associated with Str feild
154 object [] fieldAttrs
= myType
.GetField ("Str").GetCustomAttributes (true);
155 Assert
.AreEqual(fieldAttrs
.Length
, 1, "#4");
156 CustomAttribute fieldAttr
= fieldAttrs
[0] as CustomAttribute
;
157 Assert
.AreEqual(fieldAttr
.AttributeOne
, "one", "#5");
158 Assert
.AreEqual(fieldAttr
.AttributeTwo
, "two", "#6");
162 public void CtorTwoTest ()
164 //test for the constructor with signature--
165 // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], FieldInfo[], Object[]) ;
167 * WE build a imaginary type as follows
168 * [CustomAttribute("Test","Type")]
169 * public class TestType
173 * We also set the "Feild" of class CustomAttribute and the value;
174 * And then check for the validity of attributes in the test functions
177 AssemblyName asmName
= new AssemblyName ();
178 asmName
.Name
= "TestAssembly.dll";
180 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
181 asmName
, AssemblyBuilderAccess
.Run
);
183 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
185 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
186 TypeAttributes
.Public
);
188 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
190 ConstructorInfo classCtorInfo
=
191 typeof (CustomAttribute
).GetConstructor (ctorParams
);
193 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
195 new object [] { "Test","Type" }
,
196 typeof(CustomAttribute
).GetFields(),
197 new object [] { "TestCase" }
200 typeBuilder
.SetCustomAttribute (typeCABuilder
);
203 Type myType
= typeBuilder
.CreateType ();
205 //Now check for the validity of the attributes.
206 object testInstance
= Activator
.CreateInstance (myType
);
208 //check the validity of the attribute associated with Print method
209 object [] customAttrs
= myType
.GetCustomAttributes (false);
210 Assert
.AreEqual (customAttrs
.Length
, 1, "1");
212 //Custom Attributes of TestType
213 CustomAttribute attr
= customAttrs
[0] as CustomAttribute
;
214 Assert
.AreEqual (attr
.AttributeOne
, "Test", "#2");
215 Assert
.AreEqual (attr
.AttributeTwo
, "Type", "#3");
216 Assert
.AreEqual (attr
.Feild
, "TestCase", "#4");
221 public void CtorThreeTest ()
223 //test for the constructor with signature--
224 // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], PropertyInfo[], Object[]) ;
226 * WE build a imaginary type as follows
227 * [CustomAttribute()]
228 * public class TestType
232 * We also set the "AttributeOne" of class CustomAttribute by means of the constuctor
233 * And then check for the validity of attribute state
236 AssemblyName asmName
= new AssemblyName ();
237 asmName
.Name
= "TestAssembly.dll";
239 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
240 asmName
, AssemblyBuilderAccess
.Run
);
242 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
244 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
245 TypeAttributes
.Public
);
247 Type
[] ctorParams
= new Type
[] { }
;
249 ConstructorInfo classCtorInfo
=
250 typeof (CustomAttribute
).GetConstructor (ctorParams
);
252 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
255 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
256 new object [] { "TestCase" }
259 typeBuilder
.SetCustomAttribute (typeCABuilder
);
262 Type myType
= typeBuilder
.CreateType ();
264 //Now check for the validity of the attributes.
265 object testInstance
= Activator
.CreateInstance (myType
);
267 //check the validity of the attribute associated with Print method
268 object [] customAttrs
= myType
.GetCustomAttributes (false);
269 Assert
.AreEqual (customAttrs
.Length
, 1, "#1");
271 //Custom Attributes of TestType
272 CustomAttribute attr
= customAttrs
[0] as CustomAttribute
;
273 Assert
.AreEqual(attr
.AttributeOne
, "TestCase", "#2");
277 public void CtorFourTest ()
279 //test for the constructor with signature--
280 //public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);
282 * WE build a imaginary type as follows
283 * [CustomAttribute()]
284 * public class TestType
288 * We also set the "AttributeOne" property ,
289 * and "Feild" of class CustomAttribute
290 * by means of the constuctor of CustomAttributeBuilder
291 * And then check for the validity
294 AssemblyName asmName
= new AssemblyName ();
295 asmName
.Name
= "TestAssembly.dll";
297 AssemblyBuilder asmBuilder
= Thread
.GetDomain ().DefineDynamicAssembly (
298 asmName
, AssemblyBuilderAccess
.Run
);
300 ModuleBuilder modBuilder
= asmBuilder
.DefineDynamicModule ("TestModule");
302 TypeBuilder typeBuilder
= modBuilder
.DefineType ("TestType",
303 TypeAttributes
.Public
);
305 Type
[] ctorParams
= new Type
[] { }
;
307 ConstructorInfo classCtorInfo
=
308 typeof (CustomAttribute
).GetConstructor (ctorParams
);
310 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder(
313 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
314 new object [] { "TestCase" }
,
315 typeof(CustomAttribute
).GetFields (),
316 new object [] { "FieldValue" }
319 typeBuilder
.SetCustomAttribute (typeCABuilder
);
322 Type myType
= typeBuilder
.CreateType ();
324 //Now check for the validity of the attributes.
325 object testInstance
= Activator
.CreateInstance (myType
);
327 //check the validity of the attribute associated with Print method
328 object [] customAttrs
= myType
.GetCustomAttributes (false);
329 Assert
.AreEqual(customAttrs
.Length
, 1, "#1");
331 //Custom Attributes of TestType
332 CustomAttribute attr
= customAttrs
[0] as CustomAttribute
;
333 Assert
.AreEqual (attr
.AttributeOne
, "TestCase", "#2");
334 Assert
.AreEqual (attr
.Feild
, "FieldValue", "#3");
339 [ExpectedException (typeof (ArgumentException
))]
340 public void ArgumentExceptionTest_1 ()
342 //here the constructor is static
344 Type
[] ctorParams
= new Type
[] { }
;
346 ConstructorInfo classCtorInfo
=
347 typeof (CustomAttribute
).GetConstructor (BindingFlags
.Static
| BindingFlags
.NonPublic
,
348 null, ctorParams
, null);
350 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
353 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
354 new object [] { "TestCase" }
,
355 typeof (CustomAttribute
).GetFields (),
356 new object [] { "FieldValue" }
362 [ExpectedException (typeof (ArgumentException
))]
363 public void ArgumentExceptionTest_2 ()
365 //here the consturctor is private
367 Type
[] ctorParams
= new Type
[] {typeof(string) }
;
369 ConstructorInfo classCtorInfo
=
370 typeof (CustomAttribute
).GetConstructor (BindingFlags
.Instance
|
371 BindingFlags
.NonPublic
, null, ctorParams
, null);
373 Assert
.IsNotNull (classCtorInfo
);
375 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
377 new object [] { "hello" }
,
378 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
379 new object [] { "TestCase" }
,
380 typeof (CustomAttribute
).GetFields (),
381 new object [] { "FieldValue" }
387 [ExpectedException (typeof (ArgumentException
))]
388 public void ArgumentExceptionTest_3 ()
390 // The lengths of the namedProperties and
391 //propertyValues arrays are different.
393 Type
[] ctorParams
= new Type
[] { }
;
395 ConstructorInfo classCtorInfo
=
396 typeof (CustomAttribute
).GetConstructor (ctorParams
);
398 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
401 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
402 new object [] { "TestCase","extra arg" }
,//<--here is the error
403 typeof (CustomAttribute
).GetFields (),
404 new object [] { "FieldValue" }
410 [ExpectedException (typeof (ArgumentException
))]
411 public void ArgumentExceptionTest_4()
413 //The length of the namedFields and
414 //namedValues are different
416 Type
[] ctorParams
= new Type
[] { }
;
418 ConstructorInfo classCtorInfo
=
419 typeof (CustomAttribute
).GetConstructor (ctorParams
);
421 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
424 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
425 new object [] { "TestCase" }
,
426 typeof (CustomAttribute
).GetFields (),
427 new object [] { }
//<--here is the error
433 [ExpectedException (typeof (ArgumentException
))]
434 public void ArgumentExceptionTest_6 ()
436 //The type of supplied argument does not
437 //match the type of the parameter declared
438 //in the constructor.
440 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
442 ConstructorInfo classCtorInfo
=
443 typeof (CustomAttribute
).GetConstructor (ctorParams
);
445 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
447 new object [] { "1", 123 }
,//<--here is the error,(int instead of string)
448 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
449 new object [] { "TestCase" }
,
450 typeof (CustomAttribute
).GetFields (),
451 new object [] { "FeildValue" }
457 [ExpectedException (typeof (ArgumentException
))]
458 public void ArgumentExceptionTest_7 ()
460 //A property has no setter.(CustomAttribute.AttributeTwo)
462 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
464 ConstructorInfo classCtorInfo
=
465 typeof (CustomAttribute
).GetConstructor (ctorParams
);
467 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
469 new object [] { "1","2" }
,
470 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeTwo") }
,
471 new object [] { "TestCase" }
,
472 typeof (CustomAttribute
).GetFields (),
473 new object [] { "FeildValue" }
479 [ExpectedException (typeof (ArgumentException
))]
480 public void ArgumentExceptionTest_8 ()
482 //A property doesnot belong to same class
484 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
486 ConstructorInfo classCtorInfo
=
487 typeof (CustomAttribute
).GetConstructor (ctorParams
);
489 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
491 new object [] { "1","2" }
,
492 new PropertyInfo
[] { typeof (TempClass).GetProperty ("FieldProperty")}
, //here is the error
493 new object [] { "TestCase" }
,
494 typeof (CustomAttribute
).GetFields (),
495 new object [] { "FeildValue" }
501 [ExpectedException (typeof (ArgumentException
))]
502 public void ArgumentExceptionTest_9 ()
504 //A field doesnot belong to same class
506 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
508 ConstructorInfo classCtorInfo
=
509 typeof (CustomAttribute
).GetConstructor (ctorParams
);
511 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
513 new object [] { "1","2" }
,
514 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
515 new object [] {"TestCase"}
,
516 typeof (TempClass
).GetFields (), //<-- fields of TempClass are passed
517 new object [] { "FeildValue" }
523 [ExpectedException (typeof (ArgumentException
))]
524 public void ArgumentExceptionTest_10 ()
526 //The types of the property values do
527 //not match the types of the named properties.
530 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
532 ConstructorInfo classCtorInfo
=
533 typeof (CustomAttribute
).GetConstructor (ctorParams
);
535 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
537 new object [] { "1","2" }
,
538 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
539 new object [] { (long)1212121212 }
, //<---type mismatch error(long for string)
540 typeof (CustomAttribute
).GetFields (),
541 new object [] { "FeildValue" }
547 [ExpectedException (typeof (ArgumentException
))]
548 public void ArgumentExceptionTest_11 ()
550 //The types of the field values do
551 //not match the types of the named properties.
553 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
555 ConstructorInfo classCtorInfo
=
556 typeof (CustomAttribute
).GetConstructor (ctorParams
);
558 Assert
.IsNotNull (classCtorInfo
);
559 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
561 new object [] { "1","2" }
,
562 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
563 new object [] { "One" }
,
564 typeof (CustomAttribute
).GetFields (),
565 new object []{ 12.1212 }
//<---type mismatch error(double for string)
571 [ExpectedException (typeof (ArgumentNullException
))]
572 public void ArgumentNullException_1 ()
574 //the ctor value array (2nd argument) is null
575 Type
[] ctorParams
= new Type
[] { typeof (string),typeof (string) }
;
577 ConstructorInfo classCtorInfo
=
578 typeof (CustomAttribute
).GetConstructor (ctorParams
);
580 Assert
.IsNotNull (classCtorInfo
);
581 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
583 null, //<-- here is the error
584 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
585 new object [] { "One" }
,
586 typeof (CustomAttribute
).GetFields (),
587 new object [] { "feild" }
593 [ExpectedException (typeof (ArgumentNullException
))]
594 public void ArgumentNullException_2 ()
596 //the property value array (4th argument) is null
597 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
599 ConstructorInfo classCtorInfo
=
600 typeof (CustomAttribute
).GetConstructor (ctorParams
);
602 Assert
.IsNotNull (classCtorInfo
);
603 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
605 new object [] { "one","two" }
,
606 new PropertyInfo
[]{ typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
607 null, // <-- here is the error
608 typeof (CustomAttribute
).GetFields (),
609 new object [] { "feild" }
615 [ExpectedException (typeof (ArgumentNullException
))]
616 public void ArgumentNullException_3 ()
618 //the field value array (6th argument) is null
619 Type
[] ctorParams
= new Type
[] { typeof (string), typeof (string) }
;
621 ConstructorInfo classCtorInfo
=
622 typeof (CustomAttribute
).GetConstructor (ctorParams
);
624 Assert
.IsNotNull (classCtorInfo
);
625 CustomAttributeBuilder typeCABuilder
= new CustomAttributeBuilder (
627 new object [] { "one","two" }
,
628 new PropertyInfo
[] { typeof (CustomAttribute).GetProperty ("AttributeOne") }
,
629 new object [] {"property"}
,
630 typeof (CustomAttribute
).GetFields (),
631 null // <-- here is the error
636 public C (object i
) {
641 [ExpectedException (typeof (ArgumentException
))]
642 public void ObjectParam_UserDefinedClass ()
644 var cab
= new CustomAttributeBuilder(
645 typeof (C
).GetConstructors ()[0],
646 new object[] { new C (1) }
);