Moved TestConfiguration.cs to Npgsql.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / TypeBuilderTest.cs
1
2 //
3 // TypeBuilderTest.cs - NUnit Test Cases for the TypeBuilder class
4 //
5 // Zoltan Varga (vargaz@freemail.hu)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9 // TODO:
10 //  - implement a mechnanism for easier testing of null argument exceptions
11 //  - with overloaded methods like DefineNestedType (), check the defaults
12 //    on the shorter versions.
13 //  - ToString on enums with the flags attribute set should print all
14 //    values which match, e.g. 0 == AutoLayou,AnsiClass,NotPublic
15 //
16
17 using System;
18 using System.Threading;
19 using System.Reflection;
20 using System.Reflection.Emit;
21 using System.IO;
22 using System.Security;
23 using System.Security.Permissions;
24 using System.Runtime.InteropServices;
25 using NUnit.Framework;
26
27 namespace MonoTests.System.Reflection.Emit
28 {
29
30 [TestFixture]
31 public class TypeBuilderTest : Assertion
32 {       
33         private interface AnInterface {
34         }
35
36         interface Foo {
37         }
38
39         interface Bar : Foo {
40         }
41
42         interface Baz : Bar {
43         }
44
45         private AssemblyBuilder assembly;
46
47         private ModuleBuilder module;
48
49         static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
50
51         [SetUp]
52         protected void SetUp () {
53                 AssemblyName assemblyName = new AssemblyName();
54                 assemblyName.Name = ASSEMBLY_NAME;
55
56                 assembly = 
57                         Thread.GetDomain().DefineDynamicAssembly(
58                                 assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
59
60                 module = assembly.DefineDynamicModule("module1");
61         }
62
63         static int typeIndexer = 0;
64
65         // Return a unique type name
66         private string genTypeName () {
67                 return "t" + (typeIndexer ++);
68         }
69
70         private string nullName () {
71                 return String.Format ("{0}", (char)0);
72         }
73
74         [Test]
75         public void TestAssembly () {
76                 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
77                 AssertEquals ("Assembly works",
78                                           tb.Assembly, assembly);
79         }
80
81         [Test]
82         public void TestAssemblyQualifiedName () {
83                 TypeBuilder tb = module.DefineType ("A.B.C.D", TypeAttributes.Public);
84
85                 AssertEquals ("AssemblyQualifiedName works",
86                                           tb.AssemblyQualifiedName, "A.B.C.D, " + assembly.GetName ().FullName);
87         }
88
89         [Test]
90         public void TestAttributes () {
91                 TypeAttributes attrs = TypeAttributes.Public | TypeAttributes.BeforeFieldInit;
92                 TypeBuilder tb = module.DefineType (genTypeName (), attrs);
93
94                 AssertEquals ("Attributes works",
95                                           tb.Attributes, attrs);
96         }
97
98         [Test]
99         public void TestBaseTypeClass () {
100                 TypeAttributes attrs = TypeAttributes.Public;
101                 TypeBuilder tb = module.DefineType (genTypeName (), attrs);
102                 AssertEquals ("BaseType defaults to Object",
103                                           tb.BaseType, typeof (object));
104
105                 TypeBuilder tb2 = module.DefineType (genTypeName (), attrs, tb);
106                 AssertEquals ("BaseType works",
107                                           tb2.BaseType, tb);
108         }
109
110         [Test]
111         public void TestBaseTypeInterface () // See bug: 71301
112         {
113                 TypeBuilder tb3 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
114                 AssertEquals ("Interfaces should default to no base type", null, tb3.BaseType);
115         }
116
117         [Test]
118         public void TestDeclaringType () {
119                 TypeAttributes attrs = 0;
120                 TypeBuilder tb = module.DefineType (genTypeName (), attrs);
121
122                 AssertEquals ("Has no declaring type",
123                                           null, tb.DeclaringType);
124
125                 attrs = TypeAttributes.NestedPublic;
126                 TypeBuilder tb2 = tb.DefineNestedType (genTypeName (), attrs);
127                 TypeBuilder tb3 = tb2.DefineNestedType (genTypeName (), attrs);
128                 AssertEquals ("DeclaringType works",
129                                           tb, tb3.DeclaringType.DeclaringType);
130         }
131
132         [Test]
133         public void TestFullName () {
134                 string name = genTypeName ();
135                 TypeAttributes attrs = 0;
136                 TypeBuilder tb = module.DefineType (name, attrs);
137                 AssertEquals ("FullName works",
138                                           name, tb.FullName);
139
140                 string name2 = genTypeName ();
141                 attrs = TypeAttributes.NestedPublic;
142                 TypeBuilder tb2 = tb.DefineNestedType (name2, attrs);
143
144                 string name3 = genTypeName ();
145                 attrs = TypeAttributes.NestedPublic;
146                 TypeBuilder tb3 = tb2.DefineNestedType (name3, attrs);
147
148                 AssertEquals ("FullName works on nested types",
149                                           name + "+" + name2 + "+" + name3, tb3.FullName);
150         }
151
152         [Test]
153         [ExpectedException (typeof(NotSupportedException))]
154         public void TestGUIDIncomplete () {
155                 TypeBuilder tb = module.DefineType (genTypeName ());
156                 Guid g = tb.GUID;
157         }
158
159         [Test]
160         [Category("NotWorking")]
161                 // See bug: 71302
162         public void TestGUIDComplete ()
163         {
164                 TypeBuilder tb = module.DefineType (genTypeName ());
165                 tb.CreateType ();
166                 Assert(tb.GUID != Guid.Empty);
167         }
168
169         [Test]
170         [Category("NotWorking")]
171         public void TestFixedGUIDComplete ()
172         {
173                 TypeBuilder tb = module.DefineType (genTypeName ());
174
175                 Guid guid = Guid.NewGuid ();
176
177                 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor(
178                         new Type[] {typeof(string)});
179
180                 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
181                         new object[] { guid.ToString("D") }, new FieldInfo[0], new object[0]);
182
183                 tb.SetCustomAttribute (caBuilder);
184                 tb.CreateType ();
185                 AssertEquals (guid, tb.GUID);
186         }
187
188         [Test]
189         [ExpectedException (typeof(NotSupportedException))]
190         public void TestHasElementType () {
191                 // According to the MSDN docs, this member works, but in reality, it
192                 // returns a NotSupportedException
193                 TypeBuilder tb = module.DefineType (genTypeName ());
194                 bool b = tb.HasElementType;
195         }
196
197         [Test]
198         public void TestIsAbstract () {
199                 TypeBuilder tb = module.DefineType (genTypeName ());
200                 AssertEquals ("",
201                                           false, tb.IsAbstract);
202
203                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Abstract);
204                 AssertEquals ("",
205                                           true, tb2.IsAbstract);
206         }
207
208         [Test]
209         public void TestIsAnsiClass () {
210                 TypeBuilder tb = module.DefineType (genTypeName ());
211                 AssertEquals ("",
212                                           true, tb.IsAnsiClass);
213
214                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.UnicodeClass);
215                 AssertEquals ("",
216                                           false, tb2.IsAnsiClass);
217         }
218
219         [Test]
220         public void TestIsArray () {
221                 // How can a TypeBuilder be an array ?
222                 string name = genTypeName ();
223                 TypeBuilder tb = module.DefineType (name);
224                 AssertEquals ("IsArray works",
225                                           false, tb.IsArray);
226         }
227
228         [Test]
229         public void TestIsAutoClass () {
230                 TypeBuilder tb = module.DefineType (genTypeName ());
231                 AssertEquals ("",
232                                           false, tb.IsAutoClass);
233
234                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.AutoClass);
235                 AssertEquals ("",
236                                           true, tb2.IsAutoClass);
237         }
238
239         [Test]
240         public void TestIsAutoLayout () {
241                 TypeBuilder tb = module.DefineType (genTypeName ());
242                 AssertEquals ("AutoLayout defaults to true",
243                                           true, tb.IsAutoLayout);
244
245                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.ExplicitLayout);
246                 AssertEquals ("",
247                                           false, tb2.IsAutoLayout);
248         }
249
250         [Test]
251         public void TestIsByRef () {
252                 // How can a TypeBuilder be ByRef ?
253                 TypeBuilder tb = module.DefineType (genTypeName ());
254                 AssertEquals ("IsByRef works",
255                                           false, tb.IsByRef);
256         }
257
258         [Test]
259         public void TestIsClass () {
260                 TypeBuilder tb = module.DefineType (genTypeName ());
261                 AssertEquals ("Most types are classes",
262                                           true, tb.IsClass);
263
264                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
265                 AssertEquals ("Interfaces are not classes",
266                                           false, tb2.IsClass);
267
268                 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ValueType));
269                 AssertEquals ("value types are not classes",
270                                           false, tb3.IsClass);
271
272                 TypeBuilder tb4 = module.DefineType (genTypeName (), 0, typeof (Enum));
273                 AssertEquals ("enums are not classes",
274                                           false, tb4.IsClass);
275         }
276
277         [Test]
278         [Category("NotWorking")]
279         // See bug: 71304
280         public void TestIsCOMObject () {
281                 TypeBuilder tb = module.DefineType (genTypeName ());
282                 AssertEquals ("Probably not", false, tb.IsCOMObject);
283
284                 tb = module.DefineType (genTypeName (), TypeAttributes.Import);
285                 AssertEquals ("type with Import attribute is COM object",
286                         true, tb.IsCOMObject);
287         }
288
289         [Test]
290         public void TestIsContextful () {
291                 TypeBuilder tb = module.DefineType (genTypeName ());
292                 AssertEquals (false, tb.IsContextful);
293
294                 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (ContextBoundObject));
295                 AssertEquals (true, tb2.IsContextful);
296         }
297
298         [Test]
299         public void TestIsEnum () {
300                 TypeBuilder tb = module.DefineType (genTypeName ());
301                 AssertEquals (false, tb.IsEnum);
302
303                 // This returns true under both mono and MS .NET ???
304                 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (ValueType));
305                 AssertEquals ("value types are not necessary enums",
306                         false, tb2.IsEnum);
307
308                 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (Enum));
309                 AssertEquals ("enums are enums", true, tb3.IsEnum);
310         }
311
312         [Test]
313         public void TestIsExplicitLayout () {
314                 TypeBuilder tb = module.DefineType (genTypeName ());
315                 AssertEquals ("ExplicitLayout defaults to false",
316                         false, tb.IsExplicitLayout);
317
318                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.ExplicitLayout);
319                 AssertEquals (true, tb2.IsExplicitLayout);
320         }
321
322         [Test]
323         public void TestIsImport () {
324                 // How can this be true ?
325                 TypeBuilder tb = module.DefineType (genTypeName ());
326                 AssertEquals (false, tb.IsImport);
327         }
328
329         [Test]
330         public void TestIsInterface () {
331                 TypeBuilder tb = module.DefineType (genTypeName ());
332                 AssertEquals ("Most types are not interfaces",
333                         false, tb.IsInterface);
334
335                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
336                 AssertEquals ("Interfaces are interfaces",
337                         true, tb2.IsInterface);
338
339                 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ValueType));
340                 AssertEquals ("value types are not interfaces",
341                         false, tb3.IsInterface);
342         }
343
344         [Test]
345         public void TestIsLayoutSequential () {
346                 TypeBuilder tb = module.DefineType (genTypeName ());
347                 AssertEquals ("SequentialLayout defaults to false",
348                         false, tb.IsLayoutSequential);
349
350                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.SequentialLayout);
351                 AssertEquals (true, tb2.IsLayoutSequential);
352         }
353
354         [Test]
355         public void TestIsMarshalByRef () {
356                 TypeBuilder tb = module.DefineType (genTypeName ());
357                 AssertEquals (false, tb.IsMarshalByRef);
358
359                 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (MarshalByRefObject));
360                 AssertEquals (true, tb2.IsMarshalByRef);
361
362                 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ContextBoundObject));
363                 AssertEquals (true, tb3.IsMarshalByRef);
364         }
365
366         // TODO: Visibility properties
367
368         [Test]
369         public void TestIsPointer () {
370                 // How can this be true?
371                 TypeBuilder tb = module.DefineType (genTypeName ());
372                 AssertEquals (false, tb.IsPointer);
373         }
374
375         [Test]
376         public void TestIsPrimitive () {
377                 TypeBuilder tb = module.DefineType ("int");
378                 AssertEquals (false, tb.IsPrimitive);
379         }
380
381         [Test]
382         public void IsSealed () {
383                 TypeBuilder tb = module.DefineType (genTypeName ());
384                 AssertEquals ("Sealed defaults to false",
385                         false, tb.IsSealed);
386
387                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Sealed);
388                 AssertEquals ("IsSealed works", true, tb2.IsSealed);
389         }
390         
391         static string CreateTempAssembly ()
392         {
393                 FileStream f = null;
394                 string path;
395                 Random rnd;
396                 int num = 0;
397
398                 rnd = new Random ();
399                 do {
400                         num = rnd.Next ();
401                         num++;
402                         path = Path.Combine (Path.GetTempPath(), "tmp" + num.ToString("x") + ".dll");
403
404                         try {
405                                 f = new FileStream (path, FileMode.CreateNew);
406                         } catch {}
407                 } while (f == null);
408                 
409                 f.Close();
410                 
411                 
412                 return "tmp" + num.ToString("x") + ".dll";
413         }
414
415         [Test]
416         public void IsSerializable () {
417                 TypeBuilder tb = module.DefineType (genTypeName ());
418                 AssertEquals (false, tb.IsSerializable);
419
420                 ConstructorInfo[] ctors = typeof (SerializableAttribute).GetConstructors (BindingFlags.Instance | BindingFlags.Public);
421                 Assert ("SerializableAttribute should have more than 0 public instance ctors", 
422                         ctors.Length > 0);
423
424                 tb.SetCustomAttribute (new CustomAttributeBuilder (ctors[0], new object[0]));
425                 Type createdType = tb.CreateType ();
426
427                 string an = CreateTempAssembly ();
428                 assembly.Save (an);
429                 AssertEquals (true, createdType.IsSerializable);
430                 File.Delete (Path.Combine (Path.GetTempPath (), an));
431         }
432
433         [Test]
434         public void TestIsSpecialName () {
435                 TypeBuilder tb = module.DefineType (genTypeName ());
436                 AssertEquals ("SpecialName defaults to false",
437                         false, tb.IsSpecialName);
438
439                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.SpecialName);
440                 AssertEquals ("IsSpecialName works",
441                         true, tb2.IsSpecialName);
442         }
443
444         [Test]
445         public void TestIsUnicodeClass () {
446                 TypeBuilder tb = module.DefineType (genTypeName ());
447                 AssertEquals (false, tb.IsUnicodeClass);
448
449                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.UnicodeClass);
450                 AssertEquals (true, tb2.IsUnicodeClass);
451         }
452
453         [Test]
454         public void TestIsValueType () {
455                 TypeBuilder tb = module.DefineType (genTypeName ());
456                 AssertEquals ("Most types are not value types",
457                         false, tb.IsValueType);
458
459                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
460                 AssertEquals ("Interfaces are not value types",
461                         false, tb2.IsValueType);
462
463                 TypeBuilder tb3 = module.DefineType (genTypeName (), 0, typeof (ValueType));
464                 AssertEquals ("value types are value types",
465                         true, tb3.IsValueType);
466
467                 TypeBuilder tb4 = module.DefineType (genTypeName (), 0, typeof (Enum));
468                 AssertEquals ("enums are value types",
469                         true, tb4.IsValueType);
470         }
471
472         [Test]
473         public void TestMemberType () {
474                 TypeBuilder tb = module.DefineType (genTypeName ());
475                 AssertEquals ("A type is a type",
476                         MemberTypes.TypeInfo, tb.MemberType);
477         }
478
479         [Test]
480         public void TestModule () {
481                 TypeBuilder tb = module.DefineType (genTypeName ());
482                 AssertEquals ("Module works", module, tb.Module);
483         }
484
485         [Test]
486         public void TestName () {
487                 TypeBuilder tb = module.DefineType ("A");
488                 AssertEquals ("A", tb.Name);
489
490                 TypeBuilder tb2 = module.DefineType ("A.B.C.D.E");
491                 AssertEquals ("E", tb2.Name);
492
493                 TypeBuilder tb3 = tb2.DefineNestedType ("A");
494                 AssertEquals ("A", tb3.Name);
495
496                 /* Is .E a valid name ?
497                 TypeBuilder tb4 = module.DefineType (".E");
498                 AssertEquals ("",
499                                           "E", tb4.Name);
500                 */
501         }
502
503         [Test]
504         public void TestNamespace () {
505                 TypeBuilder tb = module.DefineType ("A");
506                 AssertEquals ("", tb.Namespace);
507
508                 TypeBuilder tb2 = module.DefineType ("A.B.C.D.E");
509                 AssertEquals ("A.B.C.D", tb2.Namespace);
510
511                 TypeBuilder tb3 = tb2.DefineNestedType ("A");
512                 AssertEquals ("", tb3.Namespace);
513
514                 /* Is .E a valid name ?
515                 TypeBuilder tb4 = module.DefineType (".E");
516                 AssertEquals ("",
517                                           "E", tb4.Name);
518                 */              
519         }
520
521         [Test]
522         public void TestPackingSize () {
523                 TypeBuilder tb = module.DefineType (genTypeName ());
524                 AssertEquals (PackingSize.Unspecified, tb.PackingSize);
525
526                 TypeBuilder tb2 = module.DefineType (genTypeName (), 0, typeof (object),
527                         PackingSize.Size16, 16);
528                 AssertEquals (PackingSize.Size16, tb2.PackingSize);
529         }
530
531         [Test]
532         public void TestReflectedType () {
533                 // It is the same as DeclaringType, but why?
534                 TypeBuilder tb = module.DefineType (genTypeName ());
535                 AssertEquals (null, tb.ReflectedType);
536
537                 TypeBuilder tb2 = tb.DefineNestedType (genTypeName ());
538                 AssertEquals (tb, tb2.ReflectedType);
539         }
540
541         [Test]
542         [ExpectedException (typeof(ArgumentNullException))]
543         public void TestSetParentNull ()
544         {
545                 TypeBuilder tb = module.DefineType (genTypeName ());
546                 tb.SetParent (null);
547         }
548
549         [Test]
550         public void TestSetParentIncomplete ()
551         {
552                 TypeBuilder tb = module.DefineType (genTypeName ());
553                 tb.SetParent (typeof(Attribute));
554                 AssertEquals (typeof(Attribute), tb.BaseType);
555         }
556
557         [Test]
558         [ExpectedException (typeof(InvalidOperationException))]
559         public void TestSetParentComplete ()
560         {
561                 TypeBuilder tb = module.DefineType (genTypeName ());
562                 tb.CreateType ();
563                 tb.SetParent (typeof(Attribute));
564         }
565
566         [Test]
567         public void TestSize () {
568                 {
569                         TypeBuilder tb = module.DefineType (genTypeName ());
570                         AssertEquals (0, tb.Size);
571                         tb.CreateType ();
572                         AssertEquals (0, tb.Size);
573                 }
574
575                 {
576                         TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (object),
577                                 PackingSize.Size16, 32);
578                         AssertEquals (32, tb.Size);
579                 }
580         }
581
582         [Test]
583         [ExpectedException (typeof(NotSupportedException))]
584         public void TestTypeHandle () {
585                 TypeBuilder tb = module.DefineType (genTypeName ());
586                 RuntimeTypeHandle handle = tb.TypeHandle;
587         }
588
589         [Test]
590         [ExpectedException (typeof(NotSupportedException))]
591         public void TestTypeInitializerIncomplete ()
592         {
593                 TypeBuilder tb = module.DefineType (genTypeName ());
594                 ConstructorInfo cb = tb.TypeInitializer;
595         }
596
597         [Test]
598         public void TestTypeInitializerComplete ()
599         {
600                 TypeBuilder tb = module.DefineType (genTypeName ());
601                 tb.CreateType ();
602                 ConstructorInfo cb = tb.TypeInitializer;
603         }
604
605         [Test]
606         public void TestTypeToken () {
607                 TypeBuilder tb = module.DefineType (genTypeName ());
608                 TypeToken token = tb.TypeToken;
609         }
610
611         [Test]
612         [Category("NotWorking")]
613         public void TestUnderlyingSystemType () {
614                 {
615                         TypeBuilder tb = module.DefineType (genTypeName ());
616                         AssertEquals ("For non-enums this equals itself",
617                                                   tb, tb.UnderlyingSystemType);
618                 }
619                 {
620                         TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
621                         AssertEquals (tb, tb.UnderlyingSystemType);
622                 }
623                 {
624                         TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (ValueType));
625                         AssertEquals (tb, tb.UnderlyingSystemType);
626                 }
627
628                 {
629                         TypeBuilder tb = module.DefineType (genTypeName (), 0, typeof (Enum));
630                         try {
631                                 Type t = tb.UnderlyingSystemType;
632                                 Fail ();
633                         }
634                         catch (InvalidOperationException) {
635                         }
636
637                         tb.DefineField ("val", typeof (int), 0);
638                         AssertEquals (typeof (int), tb.UnderlyingSystemType);
639                 }
640         }
641
642         [Test]
643         public void TestAddInterfaceImplementation () {
644                 TypeBuilder tb = module.DefineType (genTypeName ());
645                 try {
646                         tb.AddInterfaceImplementation (null);
647                         Fail ();
648                 }
649                 catch (ArgumentNullException) {
650                 }
651
652                 tb.AddInterfaceImplementation (typeof (AnInterface));
653                 tb.AddInterfaceImplementation (typeof (AnInterface));
654
655                 Type t = tb.CreateType ();
656                 AssertEquals ("Should merge identical interfaces",
657                                           tb.GetInterfaces ().Length, 1);
658
659                 // Can not be called on a created type
660                 try {
661                         tb.AddInterfaceImplementation (typeof (AnInterface));
662                         Fail ();
663                 }
664                 catch (InvalidOperationException) {
665                 }
666         }
667
668         [Test]
669         [Category("NotWorking")]
670         public void TestCreateType () {
671                 // TODO: LOTS OF TEST SHOULD GO THERE
672                 TypeBuilder tb = module.DefineType (genTypeName ());
673                 tb.CreateType ();
674
675                 // Can not be called on a created type
676                 try {
677                         tb.CreateType ();
678                         Fail ();
679                 }
680                 catch (InvalidOperationException) {
681                 }
682         }
683
684         [Test]
685         public void TestDefineConstructor () {
686                 TypeBuilder tb = module.DefineType (genTypeName ());
687
688                 ConstructorBuilder cb = tb.DefineConstructor (0, 0, null);
689                 cb.GetILGenerator ().Emit (OpCodes.Ret);
690                 tb.CreateType ();
691
692                 // Can not be called on a created type
693                 try {
694                         tb.DefineConstructor (0, 0, null);
695                         Fail ();
696                 }
697                 catch (InvalidOperationException) {
698                 }
699         }
700
701         [Test]
702         public void TestDefineDefaultConstructor () {
703                 TypeBuilder tb = module.DefineType (genTypeName ());
704                 tb.DefineDefaultConstructor (0);
705                 tb.CreateType ();
706
707                 // Can not be called on a created type, altough the MSDN docs does not mention this
708                 try {
709                         tb.DefineDefaultConstructor (0);
710                         Fail ();
711                 }
712                 catch (InvalidOperationException) {
713                 }
714         }
715
716         [Test]
717         [ExpectedException (typeof(InvalidOperationException))]
718         public void TestDefineDefaultConstructorParent () {
719                 TypeBuilder tb = module.DefineType (genTypeName ());
720                 tb.DefineConstructor (MethodAttributes.Public,
721                         CallingConventions.Standard, 
722                         new Type[] { typeof(string) });
723                 Type type = tb.CreateType ();
724
725                 // create TypeBuilder for type that derived from the 
726                 // previously created type (which has no default ctor)
727                 tb = module.DefineType (genTypeName (), TypeAttributes.Class
728                         | TypeAttributes.Public, type);
729
730                 // you cannot create a type with a default ctor that
731                 // derives from a type without a default ctor
732                 tb.CreateType ();
733         }
734
735         [Test]
736         public void TestDefineEvent () {
737                 TypeBuilder tb = module.DefineType (genTypeName ());
738
739                 // Test invalid arguments
740                 try {
741                         tb.DefineEvent (null, 0, typeof (int));
742                         Fail ();
743                 }
744                 catch (ArgumentNullException) {
745                 }
746
747                 try {
748                         tb.DefineEvent ("FOO", 0, null);
749                         Fail ();
750                 }
751                 catch (ArgumentNullException) {
752                 }
753
754                 try {
755                         tb.DefineEvent ("", 0, typeof (int));
756                         Fail ();
757                 }
758                 catch (ArgumentException) {
759                 }
760
761                 tb.CreateType ();
762                 // Can not be called on a created type
763                 try {
764                         tb.DefineEvent ("BAR", 0, typeof (int));
765                         Fail ();
766                 }
767                 catch (InvalidOperationException) {
768                 }
769         }
770
771         [Test]
772         public void TestDefineField () {
773                 TypeBuilder tb = module.DefineType (genTypeName ());
774
775                 // Check invalid arguments
776                 try {
777                         tb.DefineField (null, typeof (int), 0);
778                         Fail ();
779                 }
780                 catch (ArgumentNullException) {
781                 }
782
783                 try {
784                         tb.DefineField ("", typeof (int), 0);
785                         Fail ();
786                 }
787                 catch (ArgumentException) {
788                 }
789
790                 try {
791                         // Strangely, 'A<NULL>' is accepted...
792                         string name = String.Format ("{0}", (char)0);
793                         tb.DefineField (name, typeof (int), 0);
794                         Fail ("Names with embedded nulls should be rejected");
795                 }
796                 catch (ArgumentException) {
797                 }
798
799                 try {
800                         tb.DefineField ("A", typeof (void), 0);
801                         Fail ();
802                 }
803                 catch (ArgumentException) {
804                 }
805
806                 tb.CreateType ();
807                 // Can not be called on a created type
808                 try {
809                         tb.DefineField ("B", typeof (int), 0);
810                         Fail ();
811                 }
812                 catch (InvalidOperationException) {
813                 }
814         }
815
816         [Test]
817         public void TestDefineInitializedData () {
818                 TypeBuilder tb = module.DefineType (genTypeName ());
819                 
820                 // Check invalid arguments
821                 try {
822                         tb.DefineInitializedData (null, new byte[1], 0);
823                         Fail ();
824                 }
825                 catch (ArgumentNullException) {
826                 }
827
828                 try {
829                         tb.DefineInitializedData ("FOO", null, 0);
830                         Fail ();
831                 }
832                 catch (ArgumentNullException) {
833                 }
834
835                 try {
836                         tb.DefineInitializedData ("", new byte[1], 0);
837                         Fail ();
838                 }
839                 catch (ArgumentException) {
840                 }
841
842                 // The size of the data is less than or equal to zero ???
843                 try {
844                         tb.DefineInitializedData ("BAR", new byte[0], 0);
845                         Fail ();
846                 }
847                 catch (ArgumentException) {
848                 }
849
850                 try {
851                         string name = String.Format ("{0}", (char)0);
852                         tb.DefineInitializedData (name, new byte[1], 0);
853                         Fail ("Names with embedded nulls should be rejected");
854                 }
855                 catch (ArgumentException) {
856                 }
857
858                 tb.CreateType ();
859
860                 // Can not be called on a created type, altough the MSDN docs does not mention this
861                 try {
862                         tb.DefineInitializedData ("BAR2", new byte[1], 0);
863                         Fail ();
864                 }
865                 catch (InvalidOperationException) {
866                 }
867         }
868
869         [Test]
870         public void DefineUninitializedDataInvalidArgs () {
871                 TypeBuilder tb = module.DefineType (genTypeName ());
872                 
873                 try {
874                         tb.DefineUninitializedData (null, 1, 0);
875                         Fail ();
876                 }
877                 catch (ArgumentNullException) {
878                 }
879
880                 try {
881                         tb.DefineUninitializedData ("", 1, 0);
882                         Fail ();
883                 }
884                 catch (ArgumentException) {
885                 }
886
887                 // The size of the data is less than or equal to zero ???
888                 try {
889                         tb.DefineUninitializedData ("BAR", 0, 0);
890                         Fail ();
891                 }
892                 catch (ArgumentException) {
893                 }
894
895                 try {
896                         string name = String.Format ("{0}", (char)0);
897                         tb.DefineUninitializedData (name, 1, 0);
898                         Fail ("Names with embedded nulls should be rejected");
899                 }
900                 catch (ArgumentException) {
901                 }
902         }
903
904         [Test]
905         [ExpectedException (typeof (InvalidOperationException))]
906         public void DefineUninitializedDataAlreadyCreated () {
907                 TypeBuilder tb = module.DefineType (genTypeName ());
908                 tb.CreateType ();
909
910                 tb.DefineUninitializedData ("BAR2", 1, 0);
911         }
912
913         [Test]
914         public void DefineUninitializedData () {
915                 TypeBuilder tb = module.DefineType (genTypeName ());
916
917                 tb.DefineUninitializedData ("foo", 4, FieldAttributes.Public);
918
919                 Type t = tb.CreateType ();
920
921                 object o = Activator.CreateInstance (t);
922
923                 FieldInfo fi = t.GetField ("foo");
924
925                 object fieldVal = fi.GetValue (o);
926
927                 IntPtr ptr = Marshal.AllocHGlobal (4);
928                 Marshal.StructureToPtr (fieldVal, ptr, true);
929                 Marshal.FreeHGlobal (ptr);
930         }
931
932         [Test]
933         public void TestDefineMethod () {
934                 TypeBuilder tb = module.DefineType (genTypeName ());
935
936                 // Check invalid arguments
937                 try {
938                         tb.DefineMethod (null, 0, null, null);
939                         Fail ();
940                 }
941                 catch (ArgumentNullException) {
942                 }
943
944                 try {
945                         tb.DefineMethod ("", 0, null, null);
946                         Fail ();
947                 }
948                 catch (ArgumentException) {
949                 }
950
951                 // Check non-virtual methods on an interface
952                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
953                 try {
954                         tb2.DefineMethod ("FOO", MethodAttributes.Abstract, null, null);
955                         Fail ();
956                 }
957                 catch (ArgumentException) {
958                 }
959
960                 // Check static methods on an interface
961                 tb2.DefineMethod ("BAR", MethodAttributes.Public | MethodAttributes.Static,
962                                                   typeof(void),
963                                                   Type.EmptyTypes);
964
965                 tb.CreateType ();
966                 // Can not be called on a created type
967                 try {
968                         tb.DefineMethod ("bar", 0, null, null);
969                         Fail ();
970                 }
971                 catch (InvalidOperationException) {
972                 }
973         }
974
975         // TODO: DefineMethodOverride
976
977         [Test]
978         public void TestDefineNestedType () {
979                 TypeBuilder tb = module.DefineType (genTypeName ());
980
981                 // Check invalid arguments
982                 try {
983                         tb.DefineNestedType (null);
984                         Fail ("Should reject null name");
985                 }
986                 catch (ArgumentNullException) {
987                 }
988
989                 try {
990                         tb.DefineNestedType ("");
991                         Fail ("Should reject empty name");
992                 }
993                 catch (ArgumentException) {
994                 }
995
996                 try {
997                         tb.DefineNestedType (nullName ());
998                         Fail ("Should reject name with embedded 0s");
999                 }
1000                 catch (ArgumentException) {
1001                 }
1002
1003                 // If I fix the code so this works then mcs breaks -> how can mcs
1004                 // works under MS .NET in the first place ???
1005                 /*
1006                 try {
1007                         tb.DefineNestedType ("AA", TypeAttributes.Public, null, null);
1008                         Fail ("Nested visibility must be specified.");
1009                 }
1010                 catch (ArgumentException) {
1011                 }
1012                 */
1013
1014                 try {
1015                         tb.DefineNestedType ("BB", TypeAttributes.NestedPublic, null,
1016                                                                  new Type[1]);
1017                         Fail ("Should reject empty interface");
1018                 }
1019                 catch (ArgumentException) {
1020                 }
1021
1022                 // I think this should reject non-interfaces, but it does not
1023                 tb.DefineNestedType ("BB", TypeAttributes.NestedPublic, null,
1024                                                          new Type[1] { typeof (object) });
1025
1026                 // Normal invocation
1027                 tb.DefineNestedType ("Nest");
1028
1029                 tb.CreateType ();
1030
1031                 // According to the MSDN docs, this cannnot be called after the type
1032                 // is created, but it works.
1033                 tb.DefineNestedType ("Nest2");
1034
1035                 // According to the MSDN docs, a Sealed class can't contain nested 
1036                 // types, but this is not true
1037                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Sealed);
1038                 tb2.DefineNestedType ("AA");
1039
1040                 // According to the MSDN docs, interfaces can only contain interfaces,
1041                 // but this is not true
1042                 TypeBuilder tb3 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
1043
1044                 tb3.DefineNestedType ("AA");
1045
1046                 // Check shorter versions
1047                 {
1048                         TypeBuilder nested = tb.DefineNestedType ("N1");
1049
1050                         AssertEquals (nested.Name, "N1");
1051                         AssertEquals (nested.BaseType, typeof (object));
1052                         AssertEquals (nested.Attributes, TypeAttributes.NestedPrivate);
1053                         AssertEquals (nested.GetInterfaces ().Length, 0);
1054                 }
1055
1056                 // TODO:
1057         }
1058
1059         [Test]
1060         public void TestDefinePInvokeMethod () {
1061                 TypeBuilder tb = module.DefineType (genTypeName ());
1062
1063                 tb.DefinePInvokeMethod ("A", "B", "C", 0, 0, null, null, 0, 0);
1064
1065                 // Try invalid parameters
1066                 try {
1067                         tb.DefinePInvokeMethod (null, "B", "C", 0, 0, null, null, 0, 0);
1068                         Fail ();
1069                 }
1070                 catch (ArgumentNullException) {
1071                 }
1072                 // etc...
1073
1074                 // Try invalid attributes
1075                 try {
1076                         tb.DefinePInvokeMethod ("A2", "B", "C", MethodAttributes.Abstract, 0, null, null, 0, 0);
1077                 }
1078                 catch (ArgumentException) {
1079                 }
1080
1081                 // Try an interface parent
1082                 TypeBuilder tb2 = module.DefineType (genTypeName (), TypeAttributes.Interface | TypeAttributes.Abstract);
1083
1084                 try {
1085                         tb2.DefinePInvokeMethod ("A", "B", "C", 0, 0, null, null, 0, 0);
1086                 }
1087                 catch (ArgumentException) {
1088                 }
1089         }
1090
1091         [Test]
1092         public void TestDefineProperty () {
1093                 TypeBuilder tb = module.DefineType (genTypeName ());
1094
1095                 // Check null parameter types
1096                 try {
1097                         tb.DefineProperty ("A", 0, null, new Type[1]);
1098                 }
1099                 catch (ArgumentNullException) {
1100                 }
1101         }
1102
1103         [Test]
1104         [ExpectedException (typeof(NotSupportedException))]
1105         [Category("NotWorking")]
1106         public void TestIsDefinedIncomplete () {
1107                 TypeBuilder tb = module.DefineType (genTypeName ());
1108                 tb.IsDefined (typeof (int), true);
1109         }
1110
1111         [Test]
1112         public void TestIsDefinedComplete () {
1113                 TypeBuilder tb = module.DefineType (genTypeName ());
1114
1115                 ConstructorInfo obsoleteCtor = typeof(ObsoleteAttribute).GetConstructor(
1116                         new Type[] {typeof(string)});
1117
1118                 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (obsoleteCtor,
1119                         new object[] { "obsolete message" }, new FieldInfo[0], new object[0]);
1120
1121                 tb.SetCustomAttribute (caBuilder);
1122                 tb.CreateType ();
1123                 AssertEquals (true, tb.IsDefined (typeof(ObsoleteAttribute), false));
1124         }
1125
1126         [Test]
1127         [ExpectedException (typeof(NotSupportedException))]
1128         public void TestGetCustomAttributesIncomplete ()
1129         {
1130                 TypeBuilder tb = module.DefineType (genTypeName ());
1131                 tb.GetCustomAttributes (false);
1132         }
1133
1134         [Test]
1135         public void TestGetCustomAttributesComplete ()
1136         {
1137                 TypeBuilder tb = module.DefineType (genTypeName ());
1138
1139                 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
1140                         new Type[] { typeof(string) });
1141
1142                 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
1143                         new object[] { Guid.NewGuid ().ToString ("D") }, new FieldInfo[0], new object[0]);
1144
1145                 tb.SetCustomAttribute (caBuilder);
1146                 tb.CreateType ();
1147
1148                 AssertEquals (1, tb.GetCustomAttributes (false).Length);
1149         }
1150
1151         [Test]
1152         [ExpectedException (typeof(NotSupportedException))]
1153         public void TestGetCustomAttributesOfTypeIncomplete ()
1154         {
1155                 TypeBuilder tb = module.DefineType (genTypeName ());
1156                 tb.GetCustomAttributes (typeof(ObsoleteAttribute), false);
1157         }
1158
1159         [Test]
1160         public void TestGetCustomAttributesOfTypeComplete ()
1161         {
1162                 TypeBuilder tb = module.DefineType (genTypeName ());
1163
1164                 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
1165                         new Type[] { typeof(string) });
1166
1167                 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
1168                         new object[] { Guid.NewGuid ().ToString ("D") }, new FieldInfo[0], new object[0]);
1169
1170                 tb.SetCustomAttribute (caBuilder);
1171                 tb.CreateType ();
1172
1173                 AssertEquals (1, tb.GetCustomAttributes (typeof(GuidAttribute), false).Length);
1174                 AssertEquals (0, tb.GetCustomAttributes (typeof(ObsoleteAttribute), false).Length);
1175         }
1176
1177         [Test]
1178         [ExpectedException (typeof(ArgumentNullException))]
1179         public void TestGetCustomAttributesOfNullTypeComplete ()
1180         {
1181                 TypeBuilder tb = module.DefineType (genTypeName ());
1182                 tb.CreateType ();
1183                 tb.GetCustomAttributes (null, false);
1184         }
1185
1186         [Test]
1187         [ExpectedException (typeof(NotSupportedException))]
1188         [Ignore("mcs depends on this")]
1189         public void TestGetEventsIncomplete () {
1190                 TypeBuilder tb = module.DefineType (genTypeName ());
1191                 tb.GetEvents ();
1192         }
1193
1194         [Test]
1195         public void TestGetEventsComplete () {
1196                 TypeBuilder tb = module.DefineType (genTypeName ());
1197
1198                 MethodBuilder onclickMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public, 
1199                         typeof(void), new Type[] { typeof(Object) });
1200                 onclickMethod.GetILGenerator ().Emit (OpCodes.Ret);
1201
1202                 // create public event
1203                 EventBuilder eventbuilder = tb.DefineEvent ("Change", EventAttributes.None,
1204                         typeof(ResolveEventHandler));
1205                 eventbuilder.SetRaiseMethod (onclickMethod);
1206
1207                 Type emittedType = tb.CreateType ();
1208
1209                 AssertEquals (1, tb.GetEvents ().Length);
1210                 AssertEquals (tb.GetEvents ().Length, emittedType.GetEvents ().Length);
1211         }
1212
1213
1214         [Test]
1215         [ExpectedException (typeof(NotSupportedException))]
1216         [Ignore("mcs depends on this")]
1217         public void TestGetEventsFlagsIncomplete () {
1218                 TypeBuilder tb = module.DefineType (genTypeName ());
1219                 tb.GetEvents (BindingFlags.Public);
1220         }
1221
1222         [Test]
1223         public void TestGetEventsFlagsComplete () {
1224                 TypeBuilder tb = module.DefineType (genTypeName ());
1225
1226                 MethodBuilder onchangeMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1227                         typeof(void), new Type[] { typeof(Object) });
1228                 onchangeMethod.GetILGenerator ().Emit (OpCodes.Ret);
1229
1230                 // create public event
1231                 EventBuilder changeEvent = tb.DefineEvent ("Change", EventAttributes.None,
1232                         typeof(ResolveEventHandler));
1233                 changeEvent.SetRaiseMethod (onchangeMethod);
1234
1235                 // create non-public event
1236                 EventBuilder redoChangeEvent = tb.DefineEvent ("RedoChange", EventAttributes.None,
1237                         typeof(ResolveEventHandler));
1238
1239                 Type emittedType = tb.CreateType ();
1240
1241                 AssertEquals (1, tb.GetEvents (BindingFlags.Instance | BindingFlags.Public).Length);
1242                 AssertEquals (1, tb.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1243                 AssertEquals (2, tb.GetEvents (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length);
1244                 AssertEquals (tb.GetEvents (BindingFlags.Instance | BindingFlags.Public).Length,
1245                         emittedType.GetEvents (BindingFlags.Instance | BindingFlags.Public).Length);
1246                 AssertEquals (tb.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1247                         emittedType.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1248                 AssertEquals (tb.GetEvents (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length,
1249                         emittedType.GetEvents (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length);
1250         }
1251
1252         [Test]
1253         [ExpectedException (typeof(NotSupportedException))]
1254         [Ignore("mcs depends on this")]
1255         public void TestGetEventIncomplete () {
1256                 TypeBuilder tb = module.DefineType (genTypeName ());
1257                 tb.GetEvent ("FOO");
1258         }
1259
1260         [Test]
1261         public void TestGetEventComplete () {
1262                 TypeBuilder tb = module.DefineType (genTypeName ());
1263
1264                 MethodBuilder onclickMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1265                         typeof(void), new Type[] { typeof(Object) });
1266                 onclickMethod.GetILGenerator ().Emit (OpCodes.Ret);
1267
1268                 EventBuilder eventbuilder = tb.DefineEvent ("Change", EventAttributes.None,
1269                         typeof(ResolveEventHandler));
1270                 eventbuilder.SetRaiseMethod (onclickMethod);
1271
1272                 Type emittedType = tb.CreateType ();
1273
1274                 AssertNotNull (tb.GetEvent ("Change"));
1275                 AssertEquals (tb.GetEvent ("Change"), emittedType.GetEvent ("Change"));
1276                 AssertNull (tb.GetEvent ("NotChange"));
1277                 AssertEquals (tb.GetEvent ("NotChange"), emittedType.GetEvent ("NotChange"));
1278         }
1279
1280         [Test]
1281         [ExpectedException (typeof(NotSupportedException))]
1282         [Ignore("mcs depends on this")]
1283         public void TestGetEventFlagsIncomplete () {
1284                 TypeBuilder tb = module.DefineType (genTypeName ());
1285                 tb.GetEvent ("FOO", BindingFlags.Public);
1286         }
1287
1288         [Test]
1289         public void TestGetEventFlagsComplete () {
1290                 TypeBuilder tb = module.DefineType (genTypeName ());
1291
1292                 MethodBuilder onclickMethod = tb.DefineMethod ("OnChange", MethodAttributes.Public,
1293                         typeof(void), new Type[] { typeof(Object) });
1294                 onclickMethod.GetILGenerator ().Emit (OpCodes.Ret);
1295
1296                 EventBuilder eventbuilder = tb.DefineEvent ("Change", EventAttributes.None,
1297                         typeof(ResolveEventHandler));
1298                 eventbuilder.SetRaiseMethod (onclickMethod);
1299
1300                 Type emittedType = tb.CreateType ();
1301
1302                 AssertNotNull (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.Public));
1303                 AssertEquals (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.Public),
1304                         emittedType.GetEvent ("Change", BindingFlags.Instance | BindingFlags.Public));
1305                 AssertNull (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.NonPublic));
1306                 AssertEquals (tb.GetEvent ("Change", BindingFlags.Instance | BindingFlags.NonPublic),
1307                         emittedType.GetEvent ("Change", BindingFlags.Instance | BindingFlags.NonPublic));
1308         }
1309
1310         [Test]
1311         [ExpectedException (typeof(NotSupportedException))]
1312         [Ignore("mcs depends on this")]
1313         public void TestGetFieldsIncomplete () {
1314                 TypeBuilder tb = module.DefineType (genTypeName ());
1315                 tb.GetFields ();
1316         }
1317
1318         [Test]
1319         public void TestGetFieldsComplete () {
1320                 TypeBuilder tb = module.DefineType (genTypeName ());
1321                 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1322
1323                 Type emittedType = tb.CreateType ();
1324
1325                 AssertEquals (1, tb.GetFields ().Length);
1326                 AssertEquals (tb.GetFields ().Length, emittedType.GetFields().Length);
1327         }
1328
1329         [Test]
1330         [ExpectedException (typeof(NotSupportedException))]
1331         [Ignore("mcs depends on this")]
1332         public void TestGetFieldsFlagsIncomplete () {
1333                 TypeBuilder tb = module.DefineType (genTypeName ());
1334                 tb.GetFields (BindingFlags.Instance | BindingFlags.Public);
1335         }
1336
1337         [Test]
1338         public void TestGetFieldsFlagsComplete () {
1339                 TypeBuilder tb = module.DefineType (genTypeName ());
1340                 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1341
1342                 Type emittedType = tb.CreateType ();
1343
1344                 AssertEquals (1, tb.GetFields (BindingFlags.Instance | BindingFlags.Public).Length);
1345                 AssertEquals (tb.GetFields (BindingFlags.Instance | BindingFlags.Public).Length, 
1346                         emittedType.GetFields (BindingFlags.Instance | BindingFlags.Public).Length);
1347                 AssertEquals (0, tb.GetFields (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1348                 AssertEquals (tb.GetFields (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1349                         emittedType.GetFields (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1350         }
1351
1352         [Test]
1353         [ExpectedException (typeof(NotSupportedException))]
1354         [Ignore("mcs depends on this")]
1355         public void TestGetFieldIncomplete () {
1356                 TypeBuilder tb = module.DefineType (genTypeName ());
1357                 tb.GetField ("test");
1358         }
1359
1360         [Test]
1361         public void TestGetFieldComplete () {
1362                 TypeBuilder tb = module.DefineType (genTypeName ());
1363                 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1364
1365                 Type emittedType = tb.CreateType ();
1366
1367                 AssertNotNull (tb.GetField ("TestField"));
1368                 AssertEquals (tb.GetField ("TestField").Name, emittedType.GetField ("TestField").Name);
1369                 //AssertNull (tb.GetField ("TestOtherField"));
1370                 //AssertEquals (tb.GetField ("TestOtherField").Name, 
1371                 //      emittedType.GetField ("TestOtherField").Name);
1372         }
1373
1374         [Test]
1375         [ExpectedException (typeof(NotSupportedException))]
1376         [Ignore("mcs depends on this")]
1377         public void TestGetFieldFlagsIncomplete () {
1378                 TypeBuilder tb = module.DefineType (genTypeName ());
1379                 tb.GetField ("test", BindingFlags.Public);
1380         }
1381
1382         [Test]
1383         public void TestGetFieldFlagsComplete () {
1384                 TypeBuilder tb = module.DefineType (genTypeName ());
1385                 tb.DefineField ("TestField", typeof(int), FieldAttributes.Public);
1386
1387                 Type emittedType = tb.CreateType ();
1388
1389                 AssertNotNull (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.Public));
1390                 AssertEquals (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.Public).Name,
1391                         emittedType.GetField ("TestField", BindingFlags.Instance | BindingFlags.Public).Name);
1392                 AssertNull (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.NonPublic));
1393                 AssertEquals (tb.GetField ("TestField", BindingFlags.Instance | BindingFlags.NonPublic),
1394                         emittedType.GetField ("TestField", BindingFlags.Instance | BindingFlags.NonPublic));
1395         }
1396
1397         [Test]
1398         [ExpectedException (typeof(NotSupportedException))]
1399         [Ignore("mcs depends on this")]
1400         public void TestGetPropertiesIncomplete () {
1401                 TypeBuilder tb = module.DefineType (genTypeName ());
1402                 tb.GetProperties ();
1403         }
1404
1405         [Test]
1406         public void TestGetPropertiesComplete () {
1407                 TypeBuilder tb = module.DefineType (genTypeName ());
1408                 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1409
1410                 Type emittedType = tb.CreateType ();
1411
1412                 AssertEquals (1, tb.GetProperties ().Length);
1413                 AssertEquals (tb.GetProperties ().Length, emittedType.GetProperties ().Length);
1414         }
1415
1416         [Test]
1417         [ExpectedException (typeof(NotSupportedException))]
1418         [Ignore("mcs depends on this")]
1419         public void TestGetPropertiesFlagsIncomplete () {
1420                 TypeBuilder tb = module.DefineType (genTypeName ());
1421                 tb.GetProperties (BindingFlags.Public);
1422         }
1423
1424         [Test]
1425         public void TestGetPropertiesFlagsComplete () {
1426                 TypeBuilder tb = module.DefineType (genTypeName ());
1427                 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1428
1429                 Type emittedType = tb.CreateType ();
1430
1431                 AssertEquals (1, tb.GetProperties (BindingFlags.Instance | BindingFlags.Public).Length);
1432                 AssertEquals (tb.GetProperties (BindingFlags.Instance | BindingFlags.Public).Length,
1433                         emittedType.GetProperties (BindingFlags.Instance | BindingFlags.Public).Length);
1434                 AssertEquals (0, tb.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1435                 AssertEquals (tb.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1436                         emittedType.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1437         }
1438
1439         [Test]
1440         [ExpectedException (typeof(NotSupportedException))]
1441         public void TestGetPropertyIncomplete () {
1442                 TypeBuilder tb = module.DefineType (genTypeName ());
1443                 tb.GetProperty ("test");
1444         }
1445
1446         [Test]
1447         public void TestGetPropertyComplete () {
1448                 TypeBuilder tb = module.DefineType (genTypeName ());
1449                 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1450
1451                 Type emittedType = tb.CreateType ();
1452
1453                 AssertNotNull (emittedType.GetProperty ("CustomerName"));
1454                 AssertNull (emittedType.GetProperty ("OtherCustomerName"));
1455
1456                 try {
1457                         tb.GetProperty ("CustomerName");
1458                         Fail ();
1459                 } catch (NotSupportedException) {}
1460         }
1461
1462         [Test]
1463         [ExpectedException (typeof(NotSupportedException))]
1464         public void TestGetPropertyFlagsIncomplete () {
1465                 TypeBuilder tb = module.DefineType (genTypeName ());
1466                 tb.GetProperty ("test", BindingFlags.Public);
1467         }
1468
1469         [Test]
1470         public void TestGetPropertyFlagsComplete () {
1471                 TypeBuilder tb = module.DefineType (genTypeName ());
1472                 DefineStringProperty (tb, "CustomerName", "customerName", MethodAttributes.Public);
1473
1474                 Type emittedType = tb.CreateType ();
1475
1476                 AssertNotNull (emittedType.GetProperty ("CustomerName", BindingFlags.Instance | 
1477                         BindingFlags.Public));
1478                 AssertNull (emittedType.GetProperty ("CustomerName", BindingFlags.Instance |
1479                         BindingFlags.NonPublic));
1480
1481                 try {
1482                         tb.GetProperty ("CustomerName", BindingFlags.Instance | BindingFlags.Public);
1483                         Fail ();
1484                 }
1485                 catch (NotSupportedException) { }
1486         }
1487
1488         [Test]
1489         [ExpectedException (typeof(NotSupportedException))]
1490         [Ignore("mcs depends on this")]
1491         public void TestGetMethodsIncomplete () {
1492                 TypeBuilder tb = module.DefineType (genTypeName ());
1493                 tb.GetMethods ();
1494         }
1495
1496         [Test]
1497         [Category("NotWorking")]
1498         public void TestGetMethodsComplete () {
1499                 TypeBuilder tb = module.DefineType (genTypeName ());
1500                 MethodBuilder helloMethod = tb.DefineMethod ("HelloMethod", 
1501                         MethodAttributes.Public, typeof(string), new Type[0]);
1502                 ILGenerator helloMethodIL = helloMethod.GetILGenerator ();
1503                 helloMethodIL.Emit (OpCodes.Ldstr, "Hi! ");
1504                 helloMethodIL.Emit (OpCodes.Ldarg_1);
1505                 MethodInfo infoMethod = typeof(string).GetMethod ("Concat", 
1506                         new Type[] { typeof(string), typeof(string) });
1507                 helloMethodIL.Emit (OpCodes.Call, infoMethod);
1508                 helloMethodIL.Emit (OpCodes.Ret);
1509
1510                 Type emittedType = tb.CreateType ();
1511
1512                 AssertEquals (typeof(object).GetMethods (BindingFlags.Public | BindingFlags.Instance).Length + 1, 
1513                         tb.GetMethods ().Length);
1514                 AssertEquals (tb.GetMethods ().Length, emittedType.GetMethods ().Length);
1515         }
1516
1517         [Test]
1518         [ExpectedException (typeof(NotSupportedException))]
1519         [Ignore("mcs depends on this")]
1520         public void TestGetMethodsFlagsIncomplete () {
1521                 TypeBuilder tb = module.DefineType (genTypeName ());
1522                 tb.GetMethods (BindingFlags.Public);
1523         }
1524
1525         [Test]
1526         public void TestGetMethodsFlagsComplete () {
1527                 TypeBuilder tb = module.DefineType (genTypeName ());
1528                 MethodBuilder helloMethod = tb.DefineMethod ("HelloMethod",
1529                         MethodAttributes.Public, typeof(string), new Type[0]);
1530                 ILGenerator helloMethodIL = helloMethod.GetILGenerator ();
1531                 helloMethodIL.Emit (OpCodes.Ldstr, "Hi! ");
1532                 helloMethodIL.Emit (OpCodes.Ldarg_1);
1533                 MethodInfo infoMethod = typeof(string).GetMethod ("Concat", 
1534                         new Type[] { typeof(string), typeof(string) });
1535                 helloMethodIL.Emit (OpCodes.Call, infoMethod);
1536                 helloMethodIL.Emit (OpCodes.Ret);
1537
1538                 Type emittedType = tb.CreateType ();
1539
1540                 AssertEquals (1, tb.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).Length);
1541                 AssertEquals (tb.GetMethods (BindingFlags.Instance | BindingFlags.Public).Length,
1542                         emittedType.GetMethods (BindingFlags.Instance | BindingFlags.Public).Length);
1543                 AssertEquals (0, tb.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).Length);
1544                 AssertEquals (tb.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1545                         emittedType.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1546         }
1547
1548         [Test]
1549         [ExpectedException (typeof(NotSupportedException))]
1550         public void TestGetMemberIncomplete () {
1551                 TypeBuilder tb = module.DefineType (genTypeName ());
1552                 tb.GetMember ("FOO", MemberTypes.All, BindingFlags.Public);
1553         }
1554
1555         [Test]
1556         public void TestGetMemberComplete () {
1557                 TypeBuilder tb = module.DefineType (genTypeName ());
1558                 tb.DefineField ("FOO", typeof(int), FieldAttributes.Private);
1559
1560                 Type emittedType = tb.CreateType ();
1561
1562                 AssertEquals (1, tb.GetMember ("FOO", MemberTypes.Field, BindingFlags.Instance | BindingFlags.NonPublic).Length);
1563                 AssertEquals (0, tb.GetMember ("FOO", MemberTypes.Field, BindingFlags.Instance | BindingFlags.Public).Length);
1564         }
1565
1566         [Test]
1567         [ExpectedException (typeof(NotSupportedException))]
1568         public void TestGetMembersIncomplete () {
1569                 TypeBuilder tb = module.DefineType (genTypeName ());
1570                 tb.GetMembers ();
1571         }
1572
1573         [Test]
1574         public void TestGetMembersComplete () {
1575                 TypeBuilder tb = module.DefineType (genTypeName ());
1576                 Type emittedType = tb.CreateType ();
1577
1578                 AssertEquals (tb.GetMembers ().Length, emittedType.GetMembers ().Length);
1579         }
1580
1581         [Test]
1582         [ExpectedException (typeof(NotSupportedException))]
1583         public void TestGetMembersFlagsIncomplete () {
1584                 TypeBuilder tb = module.DefineType (genTypeName ());
1585                 tb.GetMembers (BindingFlags.Public);
1586         }
1587
1588         [Test]
1589         public void TestGetMembersFlagsComplete () {
1590                 TypeBuilder tb = module.DefineType (genTypeName ());
1591                 tb.DefineField ("FOO", typeof(int), FieldAttributes.Public);
1592
1593                 Type emittedType = tb.CreateType ();
1594
1595                 Assert (tb.GetMembers (BindingFlags.Instance | BindingFlags.Public).Length != 0);
1596                 AssertEquals (tb.GetMembers (BindingFlags.Instance | BindingFlags.Public).Length,
1597                         emittedType.GetMembers (BindingFlags.Instance | BindingFlags.Public).Length);
1598                 AssertEquals (tb.GetMembers (BindingFlags.Instance | BindingFlags.NonPublic).Length,
1599                         emittedType.GetMembers (BindingFlags.Instance | BindingFlags.NonPublic).Length);
1600         }
1601
1602         [Test]
1603         [ExpectedException (typeof(NotSupportedException))]
1604         public void TestGetInterfaceIncomplete () {
1605                 TypeBuilder tb = module.DefineType (genTypeName ());
1606                 tb.GetInterface ("FOO", true);
1607         }
1608
1609         [Test]
1610         public void TestGetInterfaces () {
1611                 TypeBuilder tb = module.DefineType (genTypeName ());
1612                 Type[] interfaces = tb.GetInterfaces ();
1613                 AssertEquals (0, interfaces.Length);
1614
1615                 TypeBuilder tbInterface = module.DefineType (genTypeName (), TypeAttributes.Public | TypeAttributes.Interface | TypeAttributes.Abstract);
1616                 Type emittedInterface = tbInterface.CreateType ();
1617
1618                 tb = module.DefineType (genTypeName (), TypeAttributes.Public, typeof(object), new Type[] { emittedInterface });
1619                 interfaces = tb.GetInterfaces ();
1620                 AssertEquals (1, interfaces.Length);
1621         }
1622
1623         [Test]
1624         [ExpectedException (typeof (InvalidOperationException))]
1625         public void TestAddDeclarativeSecurityAlreadyCreated () {
1626                 TypeBuilder tb = module.DefineType (genTypeName ());
1627                 tb.CreateType ();
1628
1629                 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
1630                 tb.AddDeclarativeSecurity (SecurityAction.Demand, set);
1631         }
1632
1633         [Test]
1634         [ExpectedException (typeof (ArgumentNullException))]
1635         public void TestAddDeclarativeSecurityNullPermissionSet () {
1636                 TypeBuilder tb = module.DefineType (genTypeName ());
1637
1638                 tb.AddDeclarativeSecurity (SecurityAction.Demand, null);
1639         }
1640
1641         [Test]
1642         public void TestAddDeclarativeSecurityInvalidAction () {
1643                 TypeBuilder tb = module.DefineType (genTypeName ());
1644
1645                 SecurityAction[] actions = new SecurityAction [] { 
1646                         SecurityAction.RequestMinimum,
1647                         SecurityAction.RequestOptional,
1648                         SecurityAction.RequestRefuse };
1649                 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
1650
1651                 foreach (SecurityAction action in actions) {
1652                         try {
1653                                 tb.AddDeclarativeSecurity (action, set);
1654                                 Fail ();
1655                         } catch (ArgumentOutOfRangeException) {
1656                         }
1657                 }
1658         }
1659
1660         [Test]
1661         [ExpectedException (typeof (InvalidOperationException))]
1662         public void TestAddDeclarativeSecurityDuplicateAction () {
1663                 TypeBuilder tb = module.DefineType (genTypeName ());
1664
1665                 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
1666                 tb.AddDeclarativeSecurity (SecurityAction.Demand, set);
1667                 tb.AddDeclarativeSecurity (SecurityAction.Demand, set);
1668         }
1669
1670         [Test]
1671         public void TestEnums () {
1672                 TypeAttributes typeAttrs = TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed;            
1673                 TypeBuilder enumToCreate = module.DefineType(genTypeName (), typeAttrs, 
1674                                                                                                          typeof(Enum));
1675                 enumToCreate.SetCustomAttribute (new CustomAttributeBuilder (typeof (FlagsAttribute).GetConstructors ()[0], new Type [0]));
1676                 // add value__ field, see DefineEnum method of ModuleBuilder
1677                 enumToCreate.DefineField("value__", typeof(Int32), 
1678                         FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
1679
1680                 // add enum entries
1681                 FieldBuilder fb = enumToCreate.DefineField("A", enumToCreate, 
1682                         FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
1683                 fb.SetConstant((Int32) 0);
1684
1685                 fb = enumToCreate.DefineField("B", enumToCreate, 
1686                         FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
1687                 fb.SetConstant((Int32) 1);
1688
1689                 fb = enumToCreate.DefineField("C", enumToCreate, 
1690                         FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
1691                 fb.SetConstant((Int32) 2);
1692
1693                 Type enumType = enumToCreate.CreateType();
1694
1695                 object enumVal = Enum.ToObject(enumType, (Int32) 3);
1696
1697                 AssertEquals ("B, C", enumVal.ToString ());
1698                 AssertEquals (3, (Int32)enumVal);
1699         }
1700
1701         [Test]
1702         public void DefineEnum () {
1703                 TypeBuilder typeBuilder = module.DefineType (genTypeName (),
1704                                                                                                          TypeAttributes.Public);
1705                 EnumBuilder enumBuilder = module.DefineEnum (genTypeName (),
1706                                                                                                          TypeAttributes.Public, typeof(int));
1707                 typeBuilder.DefineField ("myField", enumBuilder, FieldAttributes.Private);
1708                 enumBuilder.CreateType();
1709                 typeBuilder.CreateType();
1710         }
1711
1712         [Test]
1713         [ExpectedException(typeof(TypeLoadException))]
1714         [Category("NotWorking")]
1715         public void DefineEnumThrowIfTypeBuilderCalledBeforeEnumBuilder () {
1716                 TypeBuilder typeBuilder = module.DefineType (genTypeName (),
1717                                                                                                          TypeAttributes.Public);
1718                 EnumBuilder enumBuilder = module.DefineEnum (genTypeName (),
1719                                                                                                          TypeAttributes.Public, typeof(int));
1720                 typeBuilder.DefineField ("myField", enumBuilder, FieldAttributes.Private);
1721                 typeBuilder.CreateType();
1722                 enumBuilder.CreateType();
1723         }
1724
1725         private void DefineStringProperty (TypeBuilder tb, string propertyName, string fieldName, MethodAttributes methodAttribs) {
1726                 // define the field holding the property value
1727                 FieldBuilder fieldBuilder = tb.DefineField (fieldName,
1728                         typeof(string), FieldAttributes.Private);
1729
1730                 PropertyBuilder propertyBuilder = tb.DefineProperty (
1731                         propertyName, PropertyAttributes.HasDefault, typeof(string),
1732                         new Type[] { typeof(string) });
1733
1734                 // First, we'll define the behavior of the "get" property for CustomerName as a method.
1735                 MethodBuilder getMethodBuilder = tb.DefineMethod ("Get" + propertyName,
1736                                                                 methodAttribs,
1737                                                                 typeof(string),
1738                                                                 new Type[] { });
1739
1740                 ILGenerator getIL = getMethodBuilder.GetILGenerator ();
1741
1742                 getIL.Emit (OpCodes.Ldarg_0);
1743                 getIL.Emit (OpCodes.Ldfld, fieldBuilder);
1744                 getIL.Emit (OpCodes.Ret);
1745
1746                 // Now, we'll define the behavior of the "set" property for CustomerName.
1747                 MethodBuilder setMethodBuilder = tb.DefineMethod ("Set" + propertyName,
1748                                                                 methodAttribs,
1749                                                                 null,
1750                                                                 new Type[] { typeof(string) });
1751
1752                 ILGenerator setIL = setMethodBuilder.GetILGenerator ();
1753
1754                 setIL.Emit (OpCodes.Ldarg_0);
1755                 setIL.Emit (OpCodes.Ldarg_1);
1756                 setIL.Emit (OpCodes.Stfld, fieldBuilder);
1757                 setIL.Emit (OpCodes.Ret);
1758
1759                 // Last, we must map the two methods created above to our PropertyBuilder to 
1760                 // their corresponding behaviors, "get" and "set" respectively. 
1761                 propertyBuilder.SetGetMethod (getMethodBuilder);
1762                 propertyBuilder.SetSetMethod (setMethodBuilder);
1763         }
1764
1765         static int handler_called = 0;
1766
1767         [Test]
1768         public void TestTypeResolve () {
1769                 string typeName = genTypeName ();
1770
1771                 ResolveEventHandler handler = new ResolveEventHandler (TypeResolve);
1772         AppDomain.CurrentDomain.TypeResolve += handler;
1773                 handler_called = 0;
1774                 Type t = Type.GetType (typeName);
1775                 AssertEquals (typeName, t.Name);
1776                 AssertEquals (1, handler_called);
1777         AppDomain.CurrentDomain.TypeResolve -= handler;
1778         }
1779     
1780     Assembly TypeResolve (object sender, ResolveEventArgs args) {
1781                 TypeBuilder tb = module.DefineType (args.Name, TypeAttributes.Public);
1782                 tb.CreateType ();
1783                 handler_called ++;
1784                 return tb.Assembly;
1785         }
1786
1787         [Test]
1788         public void TestIsAssignableTo () {
1789                 Type icomparable = typeof (IComparable);
1790
1791                 TypeBuilder tb = module.DefineType (genTypeName (),
1792                                                                                         TypeAttributes.Public, null, new Type[] { icomparable, typeof (Bar) });
1793
1794                 Assert ("01", icomparable.IsAssignableFrom (tb));
1795                 Assert ("02", !tb.IsAssignableFrom (icomparable));
1796
1797                 Assert ("03", typeof (Bar).IsAssignableFrom (tb));
1798                 Assert ("04", !typeof (Baz).IsAssignableFrom (tb));
1799
1800                 Assert ("05", tb.IsAssignableFrom (tb));
1801
1802                 Assert ("06", !tb.IsAssignableFrom (typeof (IDisposable)));
1803                 tb.AddInterfaceImplementation (typeof (IDisposable));
1804
1805                 // Fails under .net, so we don't support it either
1806                 //Assert ("07", tb.IsAssignableFrom (typeof (IDisposable)));
1807         }
1808
1809         [Test]
1810         [Category("NotDotNet")]
1811         public void TestIsAssignableTo_NotDotNet () {
1812                 Type icomparable = typeof (IComparable);
1813
1814                 TypeBuilder tb = module.DefineType (genTypeName (),
1815                                                                                         TypeAttributes.Public, null, new Type[] { icomparable, typeof (Bar) });
1816
1817                 Assert ("01", typeof (Foo).IsAssignableFrom (tb));
1818
1819                 tb.AddInterfaceImplementation (typeof (IDisposable));
1820
1821                 // bug #73469
1822                 Assert ("02", typeof (Bar[]).IsAssignableFrom (module.GetType (tb.FullName + "[]")));
1823         }
1824
1825         [Test]
1826         [ExpectedException (typeof (InvalidOperationException))]
1827         public void EmptyMethodBody () {
1828                 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
1829
1830                 tb.DefineMethod ("foo", MethodAttributes.Public, typeof (void), new Type [] {});
1831                 tb.CreateType ();
1832         }
1833
1834         [Test]
1835         [ExpectedException (typeof (InvalidOperationException))]
1836         public void EmptyCtorBody () {
1837                 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
1838
1839                 tb.DefineConstructor (0, CallingConventions.Standard, null);
1840                 tb.CreateType ();
1841         }
1842
1843 #if NET_2_0
1844         [Test]
1845         public void GenericType ()
1846         {
1847                 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
1848                 tb.DefineGenericParameters ("T");
1849
1850                 Assert ("01", tb.IsGenericType);
1851                 Assert ("02", tb.IsGenericTypeDefinition);
1852                 Assert ("03", tb.ContainsGenericParameters);
1853                 Assert ("04", !tb.IsGenericParameter);
1854
1855                 Type[] args = tb.GetGenericArguments ();
1856                 Assert ("a01", !args [0].IsGenericType);
1857                 Assert ("a02", !args [0].IsGenericTypeDefinition);
1858                 Assert ("a03", args [0].ContainsGenericParameters);
1859                 Assert ("a04", args [0].IsGenericParameter);
1860         }
1861 #endif
1862 }
1863 }