2004-03-15 Nick Drochak <ndrochak@ieee.org>
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / CustomAttributeBuilderTest.cs
1 // CustomAttributeBuilderTest.cs\r
2 //\r
3 // Author: Vineeth N <nvineeth@yahoo.com>\r
4 //\r
5 // (C) 2004 Ximian, Inc. http://www.ximian.com\r
6 //\r
7 using System;\r
8 using System.Reflection;\r
9 using System.Reflection.Emit;\r
10 using System.Threading;\r
11 using NUnit.Framework;\r
12 \r
13 namespace MonoTests.System.Reflection.Emit\r
14 {\r
15         /// <summary>\r
16         /// TestFixture for CustomAttributeBuilderTest.\r
17         /// The members to be tested are as follows:\r
18         /// 4 constructors:\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
29         /// </summary>\r
30 \r
31         [TestFixture]\r
32         public class CustomAttributeBuilderTest : Assertion\r
33         {\r
34                 \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
37 \r
38                 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Class)]\r
39                 public class CustomAttribute: Attribute\r
40                 {\r
41                         private string attr1;\r
42                         private string attr2;\r
43                         public string Feild; //used for testing the second type of constructor\r
44 \r
45                         public CustomAttribute () {}\r
46                         public CustomAttribute (String s1 , String s2)\r
47                         {\r
48                                 attr1 = s1;\r
49                                 attr2=s2;\r
50                         }\r
51 \r
52                         private CustomAttribute (String s1) {}\r
53                         static CustomAttribute () {}\r
54 \r
55                         public string AttributeOne\r
56                         {\r
57                                 get { return attr1; }\r
58                                 set { attr1 = value; }\r
59                         }\r
60                         \r
61                         public string AttributeTwo\r
62                         {\r
63                                 get { return attr2; }\r
64                                 //the set is skipped and is used later in testing\r
65                         }\r
66 \r
67                 }\r
68                 \r
69                 private class TempClass\r
70                 {\r
71                         //used for testing the ArgumentException\r
72                         public string Field;\r
73                         public string FieldProperty\r
74                         {\r
75                                 get { return Field; }\r
76                                 set { Field = value; }\r
77                         }\r
78                 }\r
79 \r
80                 \r
81 \r
82                 \r
83                 [Test]\r
84                 public void CtorOneTest ()\r
85                 {\r
86                         //test for the constructor with signature--\r
87                         // public CustomAttributeBuilder(ConstructorInfo, object[]);\r
88                         /*\r
89                          * WE build a imaginary type as follows\r
90                          * class TestType\r
91                          * {\r
92                          *      [CustomAttribute("one","two")]\r
93                          *      public string Str;\r
94                          * \r
95                          *      [CustomAttribute("hello","world")]\r
96                          *      public void Print()\r
97                          *      {Console.WriteLine("Hello World"); }\r
98                          * \r
99                          * }\r
100                          * And then check for the validity of attributes in the test functions\r
101                          */\r
102                         AssemblyName asmName = new AssemblyName ();\r
103                         asmName.Name = "TestAssembly.dll";\r
104 \r
105                         AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (\r
106                                 asmName , AssemblyBuilderAccess.Run);\r
107                         \r
108                         ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");\r
109                         \r
110                         TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",\r
111                                 TypeAttributes.Public);\r
112 \r
113                         Type[] ctorParams = new Type[] { typeof (string),typeof (string) };\r
114                         \r
115                         ConstructorInfo classCtorInfo = \r
116                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
117 \r
118                         CustomAttributeBuilder feildCABuilder = new CustomAttributeBuilder (\r
119                                 classCtorInfo,\r
120                                 new object [] { "one","two" }\r
121                                 ),\r
122                                 methodCABuilder = new CustomAttributeBuilder (\r
123                                 classCtorInfo,\r
124                                 new object [] { "hello","world" }\r
125                                 );\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
137                         \r
138                         // create the type\r
139                         Type myType = typeBuilder.CreateType ();\r
140 \r
141                         //Now check for the validity of the attributes.\r
142                         object testInstance = Activator.CreateInstance (myType);\r
143 \r
144                         //check the validity of the attribute associated with Print method \r
145                         \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
151                         \r
152                         //check the validity of the attribute associated with Str feild\r
153 \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
159                 }\r
160 \r
161                 [Test]\r
162                 public void CtorTwoTest ()\r
163                 {\r
164                         //test for the constructor with signature--\r
165                         // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], FieldInfo[], Object[]) ;\r
166                         /*\r
167                          * WE build a imaginary type as follows\r
168                          * [CustomAttribute("Test","Type")]\r
169                          * public class TestType\r
170                          * {\r
171                          * \r
172                          * }\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
175                          */\r
176                                                                         \r
177                         AssemblyName asmName = new AssemblyName ();\r
178                         asmName.Name = "TestAssembly.dll";\r
179 \r
180                         AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (\r
181                                 asmName, AssemblyBuilderAccess.Run);\r
182                         \r
183                         ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");\r
184                         \r
185                         TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",\r
186                                 TypeAttributes.Public);\r
187 \r
188                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
189                         \r
190                         ConstructorInfo classCtorInfo = \r
191                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
192 \r
193                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
194                                 classCtorInfo,\r
195                                 new object [] { "Test","Type" },\r
196                                 typeof(CustomAttribute).GetFields(),\r
197                                 new object [] { "TestCase" }\r
198                                 ); \r
199                                 \r
200                         typeBuilder.SetCustomAttribute (typeCABuilder);\r
201                         \r
202                         // create the type\r
203                         Type myType = typeBuilder.CreateType ();\r
204 \r
205                         //Now check for the validity of the attributes.\r
206                         object testInstance = Activator.CreateInstance (myType);\r
207 \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
211 \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
217 \r
218                 }\r
219 \r
220                 [Test]\r
221                 public void CtorThreeTest ()\r
222                 {\r
223                         //test for the constructor with signature--\r
224                         // CustomAttributeBuilder Constructor (ConstructorInfo, Object[], PropertyInfo[], Object[]) ;\r
225                         /*\r
226                          * WE build a imaginary type as follows\r
227                          * [CustomAttribute()]\r
228                          * public class TestType\r
229                          * {\r
230                          * \r
231                          * }\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
234                          */\r
235                         \r
236                         AssemblyName asmName = new AssemblyName ();\r
237                         asmName.Name = "TestAssembly.dll";\r
238 \r
239                         AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (\r
240                                 asmName, AssemblyBuilderAccess.Run);\r
241                         \r
242                         ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");\r
243                         \r
244                         TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",\r
245                                 TypeAttributes.Public);\r
246                                                                 \r
247                         Type [] ctorParams = new Type [] { };\r
248 \r
249                         ConstructorInfo classCtorInfo = \r
250                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
251 \r
252                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
253                                 classCtorInfo,\r
254                                 new object [] { },\r
255                                 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },\r
256                                 new object [] { "TestCase" }\r
257                                 ); \r
258                                 \r
259                         typeBuilder.SetCustomAttribute (typeCABuilder);\r
260                         \r
261                         // create the type\r
262                         Type myType = typeBuilder.CreateType ();\r
263 \r
264                         //Now check for the validity of the attributes.\r
265                         object testInstance = Activator.CreateInstance (myType);\r
266 \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
270 \r
271                         //Custom Attributes of TestType\r
272                         CustomAttribute attr = customAttrs [0] as CustomAttribute;\r
273                         AssertEquals("#AttributeOne", attr.AttributeOne, "TestCase");\r
274                 }\r
275 \r
276                 [Test]\r
277                 public void CtorFourTest ()\r
278                 {\r
279                         //test for the constructor with signature--\r
280                         //public CustomAttributeBuilder(ConstructorInfo, object[], PropertyInfo[], object[], FieldInfo[], object[]);\r
281                         /*\r
282                          * WE build a imaginary type as follows\r
283                          * [CustomAttribute()]\r
284                          * public class TestType\r
285                          * {\r
286                          * \r
287                          * }\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
292                          */\r
293                                         \r
294                         AssemblyName asmName = new AssemblyName ();\r
295                         asmName.Name = "TestAssembly.dll";\r
296 \r
297                         AssemblyBuilder asmBuilder = Thread.GetDomain ().DefineDynamicAssembly (\r
298                                 asmName , AssemblyBuilderAccess.Run);\r
299                         \r
300                         ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule ("TestModule");\r
301                         \r
302                         TypeBuilder typeBuilder = modBuilder.DefineType ("TestType",\r
303                                 TypeAttributes.Public);\r
304                 \r
305                         Type [] ctorParams = new Type [] { };\r
306                         \r
307                         ConstructorInfo classCtorInfo = \r
308                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
309 \r
310                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder(\r
311                                 classCtorInfo,\r
312                                 new object [] { },\r
313                                 new PropertyInfo []{ typeof (CustomAttribute).GetProperty ("AttributeOne") },\r
314                                 new object [] { "TestCase" },\r
315                                 typeof(CustomAttribute).GetFields (),\r
316                                 new object [] { "FieldValue" }\r
317                                 ); \r
318                                 \r
319                         typeBuilder.SetCustomAttribute (typeCABuilder);\r
320                         \r
321                         // create the type\r
322                         Type myType = typeBuilder.CreateType ();\r
323 \r
324                         //Now check for the validity of the attributes.\r
325                         object testInstance = Activator.CreateInstance (myType);\r
326 \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
330 \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
335                 }\r
336         \r
337                 \r
338                 [Test]\r
339                 [ExpectedException (typeof (ArgumentException))]\r
340                 public void ArgumentExceptionTest_1 ()\r
341                 {\r
342                         //here the constructor is static \r
343                                         \r
344                         Type [] ctorParams = new Type [] { };\r
345                         \r
346                         ConstructorInfo classCtorInfo = \r
347                                 typeof (CustomAttribute).GetConstructor (BindingFlags.Static | BindingFlags.NonPublic,\r
348                                                 null, ctorParams, null);\r
349 \r
350                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
351                                 classCtorInfo,\r
352                                 new object [] { },\r
353                                 new PropertyInfo [] { typeof (CustomAttribute).GetProperty ("AttributeOne") },\r
354                                 new object [] { "TestCase" },\r
355                                 typeof (CustomAttribute).GetFields (),\r
356                                 new object [] { "FieldValue" }\r
357                                 );\r
358                 }\r
359 \r
360                 \r
361                 [Test]\r
362                 [ExpectedException (typeof (ArgumentException))]\r
363                 public void ArgumentExceptionTest_2 ()\r
364                 {\r
365                         //here the consturctor is private\r
366                                         \r
367                         Type [] ctorParams = new Type[] {typeof(string) };\r
368                         \r
369                         ConstructorInfo classCtorInfo = \r
370                                 typeof (CustomAttribute).GetConstructor (BindingFlags.Instance |\r
371                                                 BindingFlags.NonPublic, null, ctorParams, null);\r
372 \r
373                         Assert ("#Custom Attribute has private constuctor ", classCtorInfo != null);\r
374 \r
375                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
376                                 classCtorInfo,\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
382                                 );\r
383                 }\r
384 \r
385                 \r
386                 [Test]\r
387                 [ExpectedException (typeof (ArgumentException))]\r
388                 public void ArgumentExceptionTest_3 ()\r
389                 {\r
390                         // The lengths of the namedProperties and \r
391                         //propertyValues arrays are different. \r
392                         \r
393                         Type [] ctorParams = new Type [] { };\r
394                         \r
395                         ConstructorInfo classCtorInfo = \r
396                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
397 \r
398                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
399                                 classCtorInfo,\r
400                                 new object [] { },\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
405                                 );\r
406                 }\r
407 \r
408 \r
409                 [Test]\r
410                 [ExpectedException (typeof (ArgumentException))]\r
411                 public void ArgumentExceptionTest_4()\r
412                 {\r
413                         //The length of the namedFields and\r
414                         //namedValues are different \r
415                         \r
416                         Type [] ctorParams = new Type [] { };\r
417                         \r
418                         ConstructorInfo classCtorInfo = \r
419                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
420 \r
421                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
422                                 classCtorInfo,\r
423                                 new object [] { },\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
428                                 );\r
429                 }\r
430 \r
431 \r
432                 [Test]\r
433                 [ExpectedException (typeof (ArgumentException))]\r
434                 public void ArgumentExceptionTest_5 ()\r
435                 {\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
439 \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
443                         \r
444                         Type [] ctorParams = new Type [] { };\r
445                         \r
446                         ConstructorInfo classCtorInfo = \r
447                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
448 /*\r
449                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
450                                 classCtorInfo,\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
456                                 );\r
457 */\r
458                 }\r
459 \r
460 \r
461                 [Test]\r
462                 [ExpectedException (typeof (ArgumentException))]\r
463                 public void ArgumentExceptionTest_6 ()\r
464                 {\r
465                         //The type of supplied argument does not\r
466                         //match the type of the parameter declared \r
467                         //in the constructor.\r
468                         \r
469                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
470                         \r
471                         ConstructorInfo classCtorInfo = \r
472                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
473 \r
474                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
475                                 classCtorInfo,\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
481                                 );\r
482                 }\r
483 \r
484 \r
485                 [Test]\r
486                 [ExpectedException (typeof (ArgumentException))]\r
487                 public void ArgumentExceptionTest_7 ()\r
488                 {\r
489                         //A property has no setter.(CustomAttribute.AttributeTwo)\r
490                                                 \r
491                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
492                         \r
493                         ConstructorInfo classCtorInfo = \r
494                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
495 \r
496                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
497                                 classCtorInfo,\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
503                                 ); \r
504                 }\r
505 \r
506 \r
507                 [Test]\r
508                 [ExpectedException (typeof (ArgumentException))]\r
509                 public void ArgumentExceptionTest_8 ()\r
510                 {\r
511                         //A property doesnot belong to same class\r
512                         \r
513                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
514                         \r
515                         ConstructorInfo classCtorInfo = \r
516                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
517 \r
518                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
519                                 classCtorInfo,\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
525                                 );\r
526                 }\r
527 \r
528 \r
529                 [Test]\r
530                 [ExpectedException (typeof (ArgumentException))]\r
531                 public void ArgumentExceptionTest_9 ()\r
532                 {\r
533                         //A field doesnot belong to same class\r
534                         \r
535                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
536                         \r
537                         ConstructorInfo classCtorInfo = \r
538                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
539 \r
540                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
541                                 classCtorInfo,\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
547                                 );\r
548                 }\r
549 \r
550 \r
551                 [Test]\r
552                 [ExpectedException (typeof (ArgumentException))]\r
553                 public void ArgumentExceptionTest_10 ()\r
554                 {\r
555                         //The types of the property values do \r
556                         //not match the types of the named properties.\r
557                         \r
558                                         \r
559                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
560                         \r
561                         ConstructorInfo classCtorInfo = \r
562                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
563 \r
564                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
565                                 classCtorInfo,\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
571                                 );\r
572                 }\r
573 \r
574 \r
575                 [Test]\r
576                 [ExpectedException (typeof (ArgumentException))]\r
577                 public void ArgumentExceptionTest_11 ()\r
578                 {\r
579                         //The types of the field values do \r
580                         //not match the types of the named properties.\r
581                         \r
582                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
583                         \r
584                         ConstructorInfo classCtorInfo = \r
585                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
586 \r
587                         Assert ("#ctor not null", classCtorInfo != null);\r
588                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
589                                 classCtorInfo,\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
595                                 ); \r
596                 }\r
597 \r
598 \r
599                 [Test]\r
600                 [ExpectedException (typeof (ArgumentNullException))]\r
601                 public void ArgumentNullException_1 ()\r
602                 {\r
603                         //the ctor value array (2nd argument) is null\r
604                         Type [] ctorParams = new Type [] { typeof (string),typeof (string) };\r
605                         \r
606                         ConstructorInfo classCtorInfo = \r
607                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
608 \r
609                         Assert ("#ctor not null", classCtorInfo != null);\r
610                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
611                                 classCtorInfo,\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
617                                 );\r
618                 }\r
619 \r
620 \r
621                 [Test]\r
622                 [ExpectedException (typeof (ArgumentNullException))]\r
623                 public void ArgumentNullException_2 ()\r
624                 {\r
625                         //the property value array (4th argument) is null\r
626                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
627                         \r
628                         ConstructorInfo classCtorInfo = \r
629                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
630 \r
631                         Assert ("#ctor not null", classCtorInfo != null);\r
632                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
633                                 classCtorInfo,\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
639                                 );\r
640                 }\r
641 \r
642 \r
643                 [Test]\r
644                 [ExpectedException (typeof (ArgumentNullException))]\r
645                 public void ArgumentNullException_3 ()\r
646                 {\r
647                         //the field value array (6th argument) is null\r
648                         Type [] ctorParams = new Type [] { typeof (string), typeof (string) };\r
649                         \r
650                         ConstructorInfo classCtorInfo = \r
651                                 typeof (CustomAttribute).GetConstructor (ctorParams);\r
652 \r
653                         Assert ("#ctor not null", classCtorInfo != null);\r
654                         CustomAttributeBuilder typeCABuilder = new CustomAttributeBuilder (\r
655                                 classCtorInfo,\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
661                                 );\r
662                 }\r
663         }\r
664 }\r
665 \r