New tests.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / DerivedTypesTest.cs
1 //
2 // DerivedType.cs - NUnit Test Cases for types derived from TypeBuilder
3 //
4 // Rodrigo Kumpera <rkumpera@novell.com>
5 //
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
7 //
8
9 using System;
10 using System.Collections;
11 using System.Threading;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.IO;
15 using System.Security;
16 using System.Security.Permissions;
17 using System.Runtime.InteropServices;
18 using NUnit.Framework;
19 using System.Runtime.CompilerServices;
20
21 #if NET_2_0
22 using System.Collections.Generic;
23 #endif
24
25 namespace MonoTests.System.Reflection.Emit
26 {
27 #if NET_2_0
28         [TestFixture]
29         public class PointerTypeTest
30         {
31                 AssemblyBuilder assembly;
32                 ModuleBuilder module;
33                 int typeCount;
34                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
35
36                 string MakeName ()
37                 {
38                         return "internal__type"+ typeCount++;
39                 }
40
41                 [SetUp]
42                 protected void SetUp ()
43                 {
44                         AssemblyName assemblyName = new AssemblyName ();
45                         assemblyName.Name = ASSEMBLY_NAME;
46
47                         assembly =
48                                 Thread.GetDomain ().DefineDynamicAssembly (
49                                         assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
50
51                         module = assembly.DefineDynamicModule ("module1");
52                         typeCount = 0;
53                 }
54
55                 [Test]
56                 public void PropertiesValue ()
57                 {
58                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
59                         Type ptr = tb.MakePointerType ();
60
61                         Assert.AreEqual (assembly, ptr.Assembly, "#1");
62                         Assert.AreEqual ("ns.type*, " + assembly.FullName, ptr.AssemblyQualifiedName, "#2");
63                         Assert.AreEqual ("ns.type*", ptr.FullName, "#4");
64                         Assert.AreEqual (module, ptr.Module, "#5");
65                         Assert.AreEqual ("ns", ptr.Namespace, "#6");
66                         Assert.AreEqual (ptr, ptr.UnderlyingSystemType, "#7");
67                         Assert.AreEqual ("type*", ptr.Name, "#8");
68
69                         try {
70                                 object x = ptr.GUID;
71                                 Assert.Fail ("#9");
72                         } catch (NotSupportedException) {}
73
74                         try {
75                                 object x = ptr.TypeHandle;
76                                 Assert.Fail ("#10");
77                         } catch (NotSupportedException) {}
78                 }       
79
80                 [Test]
81                 public void Methods ()
82                 {
83                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
84                         Type ptr = tb.MakePointerType ();
85
86                         try {
87                                 ptr.GetInterface ("foo", true);
88                                 Assert.Fail ("#1");
89                         } catch (NotSupportedException) {
90
91                         }
92         
93                         try {
94                                 ptr.GetInterfaces ();
95                                 Assert.Fail ("#2");
96                         } catch (NotSupportedException) {
97
98                         }
99         
100                         Assert.AreEqual (tb, ptr.GetElementType ());
101         
102                         try {
103                                 ptr.GetEvent ("foo", BindingFlags.Public);
104                                 Assert.Fail ("#4");
105                         } catch (NotSupportedException) {
106
107                         }
108         
109                         try {
110                                 ptr.GetEvents (BindingFlags.Public);
111                                 Assert.Fail ("#5");
112                         } catch (NotSupportedException) {
113
114                         }
115         
116                         try {
117                                 ptr.GetField ("foo", BindingFlags.Public);
118                                 Assert.Fail ("#6");
119                         } catch (NotSupportedException) {
120
121                         }
122         
123                         try {
124                                 ptr.GetFields (BindingFlags.Public);
125                                 Assert.Fail ("#7");
126                         } catch (NotSupportedException) {
127
128                         }
129         
130                         try {
131                                 ptr.GetMembers (BindingFlags.Public);
132                                 Assert.Fail ("#8");
133                         } catch (NotSupportedException) {
134
135                         }
136         
137                         try {
138                                 ptr.GetMethod ("Sort");
139                                 Assert.Fail ("#9");
140                         } catch (NotSupportedException) {
141
142                         }
143         
144                         try {
145                                 ptr.GetMethods (BindingFlags.Public);
146                                 Assert.Fail ("#9");
147                         } catch (NotSupportedException) {
148
149                         }
150         
151                         try {
152                                 ptr.GetNestedType ("bla", BindingFlags.Public);
153                                 Assert.Fail ("#10");
154                         } catch (NotSupportedException) {
155
156                         }
157         
158                         try {
159                                 ptr.GetNestedTypes (BindingFlags.Public);
160                                 Assert.Fail ("#11");
161                         } catch (NotSupportedException) {
162
163                         }
164         
165                         try {
166                                 ptr.GetProperties (BindingFlags.Public);
167                                 Assert.Fail ("#12");
168                         } catch (NotSupportedException) {
169
170                         }       
171         
172                         try {
173                                 ptr.GetProperty ("Length");
174                                 Assert.Fail ("#13");
175                         } catch (NotSupportedException) {
176
177                         }
178         
179                         try {
180                                 ptr.GetConstructor (new Type[] { typeof (int) });
181                                 Assert.Fail ("#14");
182                         } catch (NotSupportedException) {
183
184                         }
185         
186                         TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
187                         Assert.AreEqual (attr, ptr.Attributes, "#15");
188
189                         Assert.IsTrue (ptr.HasElementType, "#16");
190                         Assert.IsFalse (ptr.IsArray, "#17");
191                         Assert.IsFalse (ptr.IsByRef, "#18");
192                         Assert.IsFalse (ptr.IsCOMObject, "#19");
193                         Assert.IsTrue (ptr.IsPointer, "#20");
194                         Assert.IsFalse (ptr.IsPrimitive, "#21");
195
196                         try {
197                                 ptr.GetConstructors (BindingFlags.Public);
198                                 Assert.Fail ("#22");
199                         } catch (NotSupportedException) {
200
201                         }
202
203                         try {
204                                 ptr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
205                                 Assert.Fail ("#23");
206                         } catch (NotSupportedException) {
207
208                         }
209
210                         try {
211                                 ptr.GetArrayRank ();
212                                 Assert.Fail ("#23");
213                         } catch (NotSupportedException) {
214
215                         }
216                 }
217
218                 [Test]
219                 public void GenericTypeMembers ()
220                 {
221                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
222                         Type arr = tb.MakeArrayType ();
223
224                         try {
225                                 arr.GetGenericArguments ();
226                                 Assert.Fail ("#1");
227                         } catch (NotSupportedException) {}
228
229                         try {
230                                 arr.GetGenericParameterConstraints ();
231                                 Assert.Fail ("#2");
232                         } catch (InvalidOperationException) {}
233
234                         try {
235                                 arr.GetGenericTypeDefinition ();
236                                 Assert.Fail ("#3");
237                         } catch (NotSupportedException) {}
238                 
239                         Assert.IsFalse (arr.ContainsGenericParameters, "#4");
240                         try {
241                                 var x = arr.GenericParameterAttributes;
242                                 Assert.Fail ("#5");
243                         } catch (NotSupportedException) {}
244
245                         try {
246                                 var x = arr.GenericParameterPosition;
247                                 Assert.Fail ("#6");
248                         } catch (InvalidOperationException) {}
249
250                         Assert.IsFalse (arr.ContainsGenericParameters, "#7");
251
252                         Assert.IsFalse (arr.IsGenericParameter, "#8");
253                         Assert.IsFalse (arr.IsGenericType, "#9");
254                         Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
255                 }
256
257                 [Test]
258                 public void AttributeValues ()
259                 {
260                                 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
261                                 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
262
263                                 tb = module.DefineType (MakeName (), TypeAttributes.Public);
264                                 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
265
266                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
267                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
268
269                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
270                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
271                 }
272
273                 [Test]
274                 public void AsParamType ()
275                 {
276
277                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
278                         Type ptr = tb.MakePointerType ();
279         
280                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { ptr });
281                         ILGenerator ilgen = mb.GetILGenerator ();
282                         ilgen.Emit (OpCodes.Ret);
283         
284                         Type res = tb.CreateType ();    
285         
286                         object o = Activator.CreateInstance (res);
287                         //FIXME this crashes the runtime
288                         //res.GetMethod ("Test").Invoke (o, new object[1] { null });
289                 }
290
291                 [Test]
292                 public void AsLocalVariable ()
293                 {
294
295                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
296                         Type ptr = tb.MakePointerType ();
297         
298                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
299                         ILGenerator ilgen = mb.GetILGenerator ();
300                         ilgen.DeclareLocal (ptr);
301                         ilgen.Emit (OpCodes.Ret);
302         
303                         Type res = tb.CreateType ();    
304         
305                         object o = Activator.CreateInstance (res);
306                         res.GetMethod ("Test").Invoke (o, null);
307                 }
308
309                 [Test]
310                 public void TestEquals ()
311                 {
312                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
313                         Type ptr = tb.MakePointerType ();
314                         Type ptr2 = tb.MakePointerType ();
315                         Assert.IsTrue (ptr.Equals (ptr), "#2");
316                 }
317
318                 [Test]
319                 [Category ("NotWorking")] //two stage type creation makes this fail
320                 public void TestEquals2 ()
321                 {
322                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
323                         Type ptr = tb.MakePointerType ();
324                         Type ptr2 = tb.MakePointerType ();
325                         Assert.IsFalse (ptr.Equals (ptr2), "#1");
326                 }
327
328                 [Test]
329                 public void IsSubclassOf ()
330                 {
331                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
332                         Type ptr = tb.MakePointerType ();
333                         Assert.IsFalse (ptr.IsSubclassOf (tb), "#1");
334                         Assert.IsFalse (ptr.IsSubclassOf (typeof (object[])), "#2");
335                 }
336
337                 [Test]
338                 public void IsAssignableFrom ()
339                 {
340                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
341                         Type ptr = tb.MakePointerType ();
342                         Assert.IsFalse (ptr.IsAssignableFrom (tb), "#1");
343                         Assert.IsFalse (ptr.IsAssignableFrom (typeof (object[])), "#2");
344                 }
345
346                 [Test]
347                 [Category ("NotWorking")] //two stage type creation makes this fail
348                 public void IsAssignableFrom2 ()
349                 {
350                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
351                         Type ptr = tb.MakePointerType ();
352                         Assert.IsFalse (typeof (object[]).IsAssignableFrom (ptr), "#1");
353                         Assert.IsFalse (typeof (object).IsAssignableFrom (ptr), "#2");
354                 }
355
356
357                 [Test]
358                 public void GetInterfaceMap ()
359                 {
360                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
361                         Type ptr = tb.MakePointerType ();
362                         try {
363                                 ptr.GetInterfaceMap (typeof (IEnumerable));
364                                 Assert.Fail ("#1");
365                         } catch (NotSupportedException) {
366
367                         }
368                 }
369
370                 [Test]
371                 public void IsInstanceOfType ()
372                 {
373                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
374                         Type ptr = tb.MakePointerType ();
375                         Assert.IsFalse (ptr.IsInstanceOfType (tb), "#1");
376                         Assert.IsFalse (ptr.IsInstanceOfType (null), "#2");
377                         Assert.IsFalse (ptr.IsInstanceOfType (new object [1]), "#3");
378
379                         Type t = tb.CreateType ();
380                         object obj = Activator.CreateInstance (t);
381                         Assert.IsFalse (ptr.IsInstanceOfType (obj), "#4");
382                 }
383
384                 [Test]
385                 public void IsGenericTypeDefinition ()
386                 {
387                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
388                         Type ptr = tb.MakePointerType ();
389                         Assert.IsFalse (ptr.IsGenericTypeDefinition, "#1");
390                 }
391
392                 [Test]
393                 public void IsGenericType ()
394                 {
395                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
396                         Type ptr = tb.MakePointerType ();
397                         Assert.IsFalse (ptr.IsGenericType, "#1");
398                 }
399
400                 [Test]
401                 public void MakeGenericType ()
402                 {
403                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
404                         Type ptr = tb.MakePointerType ();
405                         try {
406                                 ptr.MakeGenericType (new Type[] { typeof (string) });
407                                 Assert.Fail ("#1");
408                         } catch (NotSupportedException) {}
409                 }
410
411                 [Test]
412                 public void GenericParameterPosition ()
413                 {
414                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
415                         Type ptr = tb.MakePointerType ();
416                         try {
417                                 int pos = ptr.GenericParameterPosition;
418                                 Assert.Fail ("#1");
419                         } catch (InvalidOperationException) {}
420                 }
421
422                 [Test]
423                 public void GenericParameterAttributes ()
424                 {
425                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
426                         Type ptr = tb.MakePointerType ();
427                         try {
428                                 object attr = ptr.GenericParameterAttributes;
429                                 Assert.Fail ("#1");
430                         } catch (NotSupportedException) {}
431                 }
432
433                 [Test]
434                 public void GetGenericParameterConstraints ()
435                 {
436                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
437                         Type ptr = tb.MakePointerType ();
438                         try {
439                                 ptr.GetGenericParameterConstraints ();
440                                 Assert.Fail ("#1");
441                         } catch (InvalidOperationException) {}
442                 }
443
444                 [Test]
445                 public void MakeArrayType ()
446                 {
447                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
448                         Type ptr = tb.MakePointerType ();
449                         Type res = ptr.MakeArrayType ();
450                         Assert.IsNotNull (res, "#1");
451                         Assert.IsTrue (res.IsArray, "#2");
452
453                         res = ptr.MakeArrayType (2);
454                         Assert.IsNotNull (res, "#3");
455                         Assert.IsTrue (res.IsArray, "#4");
456                 }
457
458                 [Test]
459                 public void MakeByRefType ()
460                 {
461                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
462                         Type ptr = tb.MakePointerType ();
463                         Type res = ptr.MakeByRefType ();
464
465                         Assert.IsNotNull (res, "#1");
466                         Assert.IsTrue (res.IsByRef, "#2");
467                 }
468
469                 [Test]
470                 public void MakePointerType ()
471                 {
472                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
473                         Type ptr = tb.MakePointerType ();
474                         Type res = ptr.MakePointerType ();
475
476                         Assert.IsNotNull (res, "#1");
477                         Assert.IsTrue (res.IsPointer, "#2");
478                 }
479
480                 [Test]
481                 public void StructLayoutAttribute ()
482                 {
483                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
484                         Type ptr = tb.MakePointerType ();
485                         try {
486                                 object x = ptr.StructLayoutAttribute;
487                                 Assert.Fail ("#1");
488                         } catch (NotSupportedException) {}
489                 }
490
491                 [Test]
492                 public void ByRefOfGenericTypeParameter ()
493                 {
494                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
495                         var gparam = tb.DefineGenericParameters ("F")[0];
496                         Type ptr = gparam.MakePointerType ();
497
498                         try {
499                                 ptr.GetGenericArguments ();
500                                 Assert.Fail ("#1");
501                         } catch (NotSupportedException) {}
502
503                         try {
504                                 ptr.GetGenericParameterConstraints ();
505                                 Assert.Fail ("#2");
506                         } catch (InvalidOperationException) {}
507
508                         try {
509                                 ptr.GetGenericTypeDefinition ();
510                                 Assert.Fail ("#3");
511                         } catch (NotSupportedException) {}
512                 
513                         Assert.IsTrue (ptr.ContainsGenericParameters, "#4");
514                         try {
515                                 var x = ptr.GenericParameterAttributes;
516                                 Assert.Fail ("#5");
517                         } catch (NotSupportedException) {}
518
519                         try {
520                                 var x = ptr.GenericParameterPosition;
521                                 Assert.Fail ("#6");
522                         } catch (InvalidOperationException) {}
523
524
525                         Assert.IsFalse (ptr.IsGenericParameter, "#8");
526                         Assert.IsFalse (ptr.IsGenericType, "#9");
527                         Assert.IsFalse (ptr.IsGenericTypeDefinition, "#10");
528
529 #if NET_4_0
530                         Assert.AreEqual (TypeAttributes.Public, ptr.Attributes, "#11");
531 #else
532                         try {
533                                 var x = ptr.Attributes; //This is because GenericTypeParameterBuilder doesn't support Attributes 
534                                 Assert.Fail ("#11");
535                         } catch (NotSupportedException) {}
536 #endif
537
538                         Assert.IsTrue (ptr.HasElementType, "#12");
539                         Assert.IsTrue (ptr.IsPointer, "#13");
540
541                         Assert.AreEqual (assembly, ptr.Assembly, "#14");
542                         Assert.AreEqual (null, ptr.AssemblyQualifiedName, "#15");
543                         //XXX LAMEIMPL this passes on MS even thou it's pretty much very wrong. 
544                         Assert.AreEqual (typeof (Array), ptr.BaseType, "#16");
545                         Assert.AreEqual (null, ptr.FullName, "#17");
546                         Assert.AreEqual (module, ptr.Module, "#18");
547                         Assert.AreEqual (null, ptr.Namespace, "#19");
548                         Assert.AreEqual (ptr, ptr.UnderlyingSystemType, "#20");
549                         Assert.AreEqual ("F*", ptr.Name, "#21");
550
551                         Assert.AreEqual (gparam, ptr.GetElementType (), "#22");
552                 }
553         }
554
555
556         [TestFixture]
557         public class ByrefTypeTest
558         {
559                 AssemblyBuilder assembly;
560                 ModuleBuilder module;
561                 int typeCount;
562                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
563
564                 string MakeName ()
565                 {
566                         return "internal__type"+ typeCount++;
567                 }
568
569                 [SetUp]
570                 protected void SetUp ()
571                 {
572                         SetUp (AssemblyBuilderAccess.RunAndSave);
573                 }
574
575                 protected void SetUp (AssemblyBuilderAccess mode)
576                 {
577                         AssemblyName assemblyName = new AssemblyName ();
578                         assemblyName.Name = ASSEMBLY_NAME;
579
580                         assembly =
581                                 Thread.GetDomain ().DefineDynamicAssembly (
582                                         assemblyName, mode, Path.GetTempPath ());
583
584                         module = assembly.DefineDynamicModule ("module1");
585                         typeCount = 0;
586                 }
587
588                 [Test]
589                 public void PropertiesValue ()
590                 {
591                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
592                         Type byref = tb.MakeByRefType ();
593
594                         Assert.AreEqual (assembly, byref.Assembly, "#1");
595                         Assert.AreEqual ("ns.type&, " + assembly.FullName, byref.AssemblyQualifiedName, "#2");
596                         Assert.AreEqual ("ns.type&", byref.FullName, "#4");
597                         Assert.AreEqual (module, byref.Module, "#5");
598                         Assert.AreEqual ("ns", byref.Namespace, "#6");
599                         Assert.AreEqual (byref, byref.UnderlyingSystemType, "#7");
600                         Assert.AreEqual ("type&", byref.Name, "#8");
601
602                         try {
603                                 object x = byref.GUID;
604                                 Assert.Fail ("#9");
605                         } catch (NotSupportedException) {}
606
607                         try {
608                                 object x = byref.TypeHandle;
609                                 Assert.Fail ("#10");
610                         } catch (NotSupportedException) {}
611                 }       
612
613                 [Test]
614                 public void Methods ()
615                 {
616                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
617                         Type byref = tb.MakeByRefType ();
618
619                         try {
620                                 byref.GetInterface ("foo", true);
621                                 Assert.Fail ("#1");
622                         } catch (NotSupportedException) {
623
624                         }
625         
626                         try {
627                                 byref.GetInterfaces ();
628                                 Assert.Fail ("#2");
629                         } catch (NotSupportedException) {
630
631                         }
632         
633                         Assert.AreEqual (tb, byref.GetElementType ());
634         
635                         try {
636                                 byref.GetEvent ("foo", BindingFlags.Public);
637                                 Assert.Fail ("#4");
638                         } catch (NotSupportedException) {
639
640                         }
641         
642                         try {
643                                 byref.GetEvents (BindingFlags.Public);
644                                 Assert.Fail ("#5");
645                         } catch (NotSupportedException) {
646
647                         }
648         
649                         try {
650                                 byref.GetField ("foo", BindingFlags.Public);
651                                 Assert.Fail ("#6");
652                         } catch (NotSupportedException) {
653
654                         }
655         
656                         try {
657                                 byref.GetFields (BindingFlags.Public);
658                                 Assert.Fail ("#7");
659                         } catch (NotSupportedException) {
660
661                         }
662         
663                         try {
664                                 byref.GetMembers (BindingFlags.Public);
665                                 Assert.Fail ("#8");
666                         } catch (NotSupportedException) {
667
668                         }
669         
670                         try {
671                                 byref.GetMethod ("Sort");
672                                 Assert.Fail ("#9");
673                         } catch (NotSupportedException) {
674
675                         }
676         
677                         try {
678                                 byref.GetMethods (BindingFlags.Public);
679                                 Assert.Fail ("#9");
680                         } catch (NotSupportedException) {
681
682                         }
683         
684                         try {
685                                 byref.GetNestedType ("bla", BindingFlags.Public);
686                                 Assert.Fail ("#10");
687                         } catch (NotSupportedException) {
688
689                         }
690         
691                         try {
692                                 byref.GetNestedTypes (BindingFlags.Public);
693                                 Assert.Fail ("#11");
694                         } catch (NotSupportedException) {
695
696                         }
697         
698                         try {
699                                 byref.GetProperties (BindingFlags.Public);
700                                 Assert.Fail ("#12");
701                         } catch (NotSupportedException) {
702
703                         }       
704         
705                         try {
706                                 byref.GetProperty ("Length");
707                                 Assert.Fail ("#13");
708                         } catch (NotSupportedException) {
709
710                         }
711         
712                         try {
713                                 byref.GetConstructor (new Type[] { typeof (int) });
714                                 Assert.Fail ("#14");
715                         } catch (NotSupportedException) {
716
717                         }
718         
719                         TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
720                         Assert.AreEqual (attr, byref.Attributes, "#15");
721
722                         Assert.IsTrue (byref.HasElementType, "#16");
723                         Assert.IsFalse (byref.IsArray, "#17");
724                         Assert.IsTrue (byref.IsByRef, "#18");
725                         Assert.IsFalse (byref.IsCOMObject, "#19");
726                         Assert.IsFalse (byref.IsPointer, "#20");
727                         Assert.IsFalse (byref.IsPrimitive, "#21");
728
729                         try {
730                                 byref.GetConstructors (BindingFlags.Public);
731                                 Assert.Fail ("#22");
732                         } catch (NotSupportedException) {
733
734                         }
735
736                         try {
737                                 byref.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
738                                 Assert.Fail ("#23");
739                         } catch (NotSupportedException) {
740
741                         }
742
743                         try {
744                                 byref.GetArrayRank ();
745                                 Assert.Fail ("#23");
746                         } catch (NotSupportedException) {
747
748                         }
749                 }
750
751                 [Test]
752                 public void AttributeValues ()
753                 {
754                                 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
755                                 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
756
757                                 tb = module.DefineType (MakeName (), TypeAttributes.Public);
758                                 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
759
760                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
761                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
762
763                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
764                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
765                 }
766
767                 [Test]
768                 public void AsParamType ()
769                 {
770
771                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
772                         Type byref = tb.MakeByRefType ();
773         
774                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { byref });
775                         ILGenerator ilgen = mb.GetILGenerator ();
776                         ilgen.Emit (OpCodes.Ret);
777         
778                         Type res = tb.CreateType ();    
779         
780                         object o = Activator.CreateInstance (res);
781                         res.GetMethod ("Test").Invoke (o, new object[1] { null });
782                 }
783
784                 [Test]
785                 public void AsLocalVariable ()
786                 {
787
788                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
789                         Type byref = tb.MakeByRefType ();
790         
791                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
792                         ILGenerator ilgen = mb.GetILGenerator ();
793                         ilgen.DeclareLocal (byref);
794                         ilgen.Emit (OpCodes.Ret);
795         
796                         Type res = tb.CreateType ();    
797         
798                         object o = Activator.CreateInstance (res);
799                         res.GetMethod ("Test").Invoke (o, null);
800                 }
801
802                 [Test]
803                 public void TestEquals ()
804                 {
805                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
806                         Type byref = tb.MakeByRefType ();
807                         Type byref2 = tb.MakeByRefType ();
808                         Assert.IsTrue (byref.Equals (byref), "#1");
809                 }
810
811                 [Test]
812                 [Category ("NotWorking")] //two stage type creation makes this fail
813                 public void TestEquals2 ()
814                 {
815                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
816                         Type byref = tb.MakeByRefType ();
817                         Type byref2 = tb.MakeByRefType ();
818                         Assert.IsFalse (byref.Equals (byref2), "#1");
819                 }
820
821                 [Test]
822                 public void IsSubclassOf ()
823                 {
824                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
825                         Type byref = tb.MakeByRefType ();
826                         Assert.IsFalse (byref.IsSubclassOf (tb), "#1");
827                         Assert.IsFalse (byref.IsSubclassOf (typeof (object[])), "#2");
828                 }
829
830                 [Test]
831                 public void IsAssignableFrom ()
832                 {
833                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
834                         Type byref = tb.MakeByRefType ();
835                         Assert.IsFalse (byref.IsAssignableFrom (tb), "#1");
836                         Assert.IsFalse (byref.IsAssignableFrom (typeof (object[])), "#2");
837                 }
838
839                 [Test]
840                 [Category ("NotWorking")] //two stage type creation makes this fail
841                 public void IsAssignableFrom2 ()
842                 {
843                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
844                         Type byref = tb.MakeByRefType ();
845                         Assert.IsFalse (typeof (object[]).IsAssignableFrom (byref), "#1");
846                         Assert.IsFalse (typeof (object).IsAssignableFrom (byref), "#2");
847                 }
848
849                 [Test]
850                 public void GetInterfaceMap ()
851                 {
852                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
853                         Type byref = tb.MakeByRefType ();
854                         try {
855                                 byref.GetInterfaceMap (typeof (IEnumerable));
856                                 Assert.Fail ("#1");
857                         } catch (NotSupportedException) {
858
859                         }
860                 }
861
862                 [Test]
863                 public void IsInstanceOfType ()
864                 {
865                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
866                         Type byref = tb.MakeByRefType ();
867                         Assert.IsFalse (byref.IsInstanceOfType (tb), "#1");
868                         Assert.IsFalse (byref.IsInstanceOfType (null), "#2");
869                         Assert.IsFalse (byref.IsInstanceOfType (new object [1]), "#3");
870
871                         Type t = tb.CreateType ();
872                         object obj = Activator.CreateInstance (t);
873                         Assert.IsFalse (byref.IsInstanceOfType (obj), "#4");
874                 }
875
876                 [Test]
877                 public void IsGenericTypeDefinition ()
878                 {
879                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
880                         Type byref = tb.MakeByRefType ();
881                         Assert.IsFalse (byref.IsGenericTypeDefinition, "#1");
882                 }
883
884                 [Test]
885                 public void IsGenericType ()
886                 {
887                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
888                         Type byref = tb.MakeByRefType ();
889                         Assert.IsFalse (byref.IsGenericType, "#1");
890                 }
891
892                 [Test]
893                 public void MakeGenericType ()
894                 {
895                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
896                         Type byref = tb.MakeByRefType ();
897                         try {
898                                 byref.MakeGenericType (new Type[] { typeof (string) });
899                                 Assert.Fail ("#1");
900                         } catch (NotSupportedException) {}
901                 }
902
903                 [Test]
904                 public void GenericParameterPosition ()
905                 {
906                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
907                         Type byref = tb.MakeByRefType ();
908                         try {
909                                 int pos = byref.GenericParameterPosition;
910                                 Assert.Fail ("#1");
911                         } catch (InvalidOperationException) {}
912                 }
913
914                 [Test]
915                 public void GenericParameterAttributes ()
916                 {
917                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
918                         Type byref = tb.MakeByRefType ();
919                         try {
920                                 object attr = byref.GenericParameterAttributes;
921                                 Assert.Fail ("#1");
922                         } catch (NotSupportedException) {}
923                 }
924
925                 [Test]
926                 public void GetGenericParameterConstraints ()
927                 {
928                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
929                         Type byref = tb.MakeByRefType ();
930                         try {
931                                 byref.GetGenericParameterConstraints ();
932                                 Assert.Fail ("#1");
933                         } catch (InvalidOperationException) {}
934                 }
935
936                 [Test]
937                 public void MakeArrayType ()
938                 {
939                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
940                         Type byref = tb.MakeByRefType ();
941                         try {
942                                 byref.MakeArrayType ();
943                                 Assert.Fail ("#1");
944                         } catch (ArgumentException) {};
945                         try {
946                                 byref.MakeArrayType (2);
947                                 Assert.Fail ("#2");
948                         } catch (ArgumentException) {};
949                 }
950
951                 [Test]
952                 public void MakeByRefType ()
953                 {
954                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
955                         Type byref = tb.MakeByRefType ();
956                         try {
957                                 byref.MakeByRefType ();
958                                 Assert.Fail ("#1");
959                         } catch (ArgumentException) {}
960                 }
961
962                 [Test]
963                 public void MakePointerType ()
964                 {
965                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
966                         Type byref = tb.MakeByRefType ();
967                         try {
968                                 byref.MakePointerType ();
969                                 Assert.Fail ("#1");
970                         } catch (ArgumentException) {}
971                 }
972
973                 [Test]
974                 public void StructLayoutAttribute ()
975                 {
976                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
977                         Type byref = tb.MakeByRefType ();
978                         try {
979                                 object x = byref.StructLayoutAttribute;
980                                 Assert.Fail ("#1");
981                         } catch (NotSupportedException) {}
982                 }
983
984                 [Test]
985                 [Category ("NotDotNet")]
986                 public void ByRefOfAttriburesUnderCompilerContext ()
987                 {
988                         SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
989
990                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
991                         var gparam = tb.DefineGenericParameters ("F")[0];
992                         Type byref = gparam.MakeByRefType ();
993
994                         tb = module.DefineType (MakeName (), TypeAttributes.Public);
995                         Assert.AreEqual (TypeAttributes.Public , byref.Attributes, "#1");
996
997                 }
998
999                 [Test]
1000                 public void ByRefOfGenericTypeParameter ()
1001                 {
1002                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1003                         var gparam = tb.DefineGenericParameters ("F")[0];
1004                         Type byref = gparam.MakeByRefType ();
1005
1006                         try {
1007                                 byref.GetGenericArguments ();
1008                                 Assert.Fail ("#1");
1009                         } catch (NotSupportedException) {}
1010
1011                         try {
1012                                 byref.GetGenericParameterConstraints ();
1013                                 Assert.Fail ("#2");
1014                         } catch (InvalidOperationException) {}
1015
1016                         try {
1017                                 byref.GetGenericTypeDefinition ();
1018                                 Assert.Fail ("#3");
1019                         } catch (NotSupportedException) {}
1020                 
1021                         Assert.IsTrue (byref.ContainsGenericParameters, "#4");
1022                         try {
1023                                 var x = byref.GenericParameterAttributes;
1024                                 Assert.Fail ("#5");
1025                         } catch (NotSupportedException) {}
1026
1027                         try {
1028                                 var x = byref.GenericParameterPosition;
1029                                 Assert.Fail ("#6");
1030                         } catch (InvalidOperationException) {}
1031
1032
1033                         Assert.IsFalse (byref.IsGenericParameter, "#8");
1034                         Assert.IsFalse (byref.IsGenericType, "#9");
1035                         Assert.IsFalse (byref.IsGenericTypeDefinition, "#10");
1036
1037
1038 #if NET_4_0
1039                         Assert.AreEqual (TypeAttributes.Public, byref.Attributes, "#11");
1040 #else
1041                         try {
1042                                 var x = byref.Attributes; //This is because GenericTypeParameterBuilder doesn't support Attributes 
1043                                 Assert.Fail ("#11");
1044                         } catch (NotSupportedException) {}
1045 #endif
1046
1047                         Assert.IsTrue (byref.HasElementType, "#12");
1048                         Assert.IsTrue (byref.IsByRef, "#13");
1049
1050                         Assert.AreEqual (assembly, byref.Assembly, "#14");
1051                         Assert.AreEqual (null, byref.AssemblyQualifiedName, "#15");
1052                         //XXX LAMEIMPL this passes on MS even thou it's pretty much very wrong. 
1053                         Assert.AreEqual (typeof (Array), byref.BaseType, "#16");
1054                         Assert.AreEqual (null, byref.FullName, "#17");
1055                         Assert.AreEqual (module, byref.Module, "#18");
1056                         Assert.AreEqual (null, byref.Namespace, "#19");
1057                         Assert.AreEqual (byref, byref.UnderlyingSystemType, "#20");
1058                         Assert.AreEqual ("F&", byref.Name, "#21");
1059
1060                         Assert.AreEqual (gparam, byref.GetElementType (), "#22");
1061                 }
1062         }
1063
1064         [TestFixture]
1065         public class ArrayTypeTest
1066         {
1067                 AssemblyBuilder assembly;
1068                 ModuleBuilder module;
1069                 int typeCount;
1070                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
1071
1072                 string MakeName ()
1073                 {
1074                         return "internal__type"+ typeCount++;
1075                 }
1076
1077
1078                 [SetUp]
1079                 protected void SetUp ()
1080                 {
1081                         SetUp (AssemblyBuilderAccess.RunAndSave);
1082                 }
1083
1084                 protected void SetUp (AssemblyBuilderAccess mode)
1085                 {
1086                         AssemblyName assemblyName = new AssemblyName ();
1087                         assemblyName.Name = ASSEMBLY_NAME;
1088
1089                         assembly =
1090                                 Thread.GetDomain ().DefineDynamicAssembly (
1091                                         assemblyName, mode, Path.GetTempPath ());
1092
1093                         module = assembly.DefineDynamicModule ("module1");
1094                         typeCount = 0;
1095                 }
1096
1097                 [Test]
1098                 public void OneDimMultiDimentionArray ()
1099                 {
1100                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
1101
1102                         Type arr1 = tb.MakeArrayType ();
1103                         Type arr2 = tb.MakeArrayType (1);
1104                         Type arr3 = arr1.MakeArrayType (1);
1105
1106                         Assert.AreEqual ("type[]", arr1.Name, "#1");
1107                         Assert.AreEqual ("type[*]", arr2.Name, "#2");
1108                         Assert.AreEqual ("type[][*]", arr3.Name, "#3");
1109
1110                         var gparam = tb.DefineGenericParameters ("F")[0];
1111                         Type arr4 = gparam.MakeArrayType ();
1112                         Type arr5 = gparam.MakeArrayType (1);
1113
1114                         Assert.AreEqual ("F[]", arr4.Name, "#4");
1115                         Assert.AreEqual ("F[*]", arr5.Name, "#5");
1116
1117                         var eb = module.DefineEnum ("enum", TypeAttributes.Public, tb);
1118                         Type arr6 = eb.MakeArrayType ();
1119                         Type arr7 = eb.MakeArrayType (1);
1120
1121                         Assert.AreEqual ("enum[]", arr6.Name, "#6");
1122                         Assert.AreEqual ("enum[*]", arr7.Name, "#7");
1123                 }
1124
1125                 [Test]
1126                 public void PropertiesValue ()
1127                 {
1128                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
1129                         Type arr = tb.MakeArrayType ();
1130
1131                         Assert.AreEqual (assembly, arr.Assembly, "#1");
1132                         Assert.AreEqual ("ns.type[], " + assembly.FullName, arr.AssemblyQualifiedName, "#2");
1133                         Assert.AreEqual (typeof (Array), arr.BaseType, "#3");
1134                         Assert.AreEqual ("ns.type[]", arr.FullName, "#4");
1135                         Assert.AreEqual (module, arr.Module, "#5");
1136                         Assert.AreEqual ("ns", arr.Namespace, "#6");
1137                         Assert.AreEqual (arr, arr.UnderlyingSystemType, "#7");
1138                         Assert.AreEqual ("type[]", arr.Name, "#8");
1139
1140                         try {
1141                                 object x = arr.GUID;
1142                                 Assert.Fail ("#9");
1143                         } catch (NotSupportedException) {}
1144
1145                         try {
1146                                 object x = arr.TypeHandle;
1147                                 Assert.Fail ("#10");
1148                         } catch (NotSupportedException) {}
1149                 }       
1150
1151                 [Test]
1152                 public void Methods ()
1153                 {
1154                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
1155                         Type arr = tb.MakeArrayType ();
1156
1157                         try {
1158                                 arr.GetInterface ("foo", true);
1159                                 Assert.Fail ("#1");
1160                         } catch (NotSupportedException) {
1161
1162                         }
1163         
1164                         try {
1165                                 arr.GetInterfaces ();
1166                                 Assert.Fail ("#2");
1167                         } catch (NotSupportedException) {
1168
1169                         }
1170         
1171                         Assert.AreEqual (tb, arr.GetElementType ());
1172         
1173                         try {
1174                                 arr.GetEvent ("foo", BindingFlags.Public);
1175                                 Assert.Fail ("#4");
1176                         } catch (NotSupportedException) {
1177
1178                         }
1179         
1180                         try {
1181                                 arr.GetEvents (BindingFlags.Public);
1182                                 Assert.Fail ("#5");
1183                         } catch (NotSupportedException) {
1184
1185                         }
1186         
1187                         try {
1188                                 arr.GetField ("foo", BindingFlags.Public);
1189                                 Assert.Fail ("#6");
1190                         } catch (NotSupportedException) {
1191
1192                         }
1193         
1194                         try {
1195                                 arr.GetFields (BindingFlags.Public);
1196                                 Assert.Fail ("#7");
1197                         } catch (NotSupportedException) {
1198
1199                         }
1200         
1201                         try {
1202                                 arr.GetMembers (BindingFlags.Public);
1203                                 Assert.Fail ("#8");
1204                         } catch (NotSupportedException) {
1205
1206                         }
1207         
1208                         try {
1209                                 arr.GetMethod ("Sort");
1210                                 Assert.Fail ("#9");
1211                         } catch (NotSupportedException) {
1212
1213                         }
1214         
1215                         try {
1216                                 arr.GetMethods (BindingFlags.Public);
1217                                 Assert.Fail ("#9");
1218                         } catch (NotSupportedException) {
1219
1220                         }
1221         
1222                         try {
1223                                 arr.GetNestedType ("bla", BindingFlags.Public);
1224                                 Assert.Fail ("#10");
1225                         } catch (NotSupportedException) {
1226
1227                         }
1228         
1229                         try {
1230                                 arr.GetNestedTypes (BindingFlags.Public);
1231                                 Assert.Fail ("#11");
1232                         } catch (NotSupportedException) {
1233
1234                         }
1235         
1236                         try {
1237                                 arr.GetProperties (BindingFlags.Public);
1238                                 Assert.Fail ("#12");
1239                         } catch (NotSupportedException) {
1240
1241                         }       
1242         
1243                         try {
1244                                 arr.GetProperty ("Length");
1245                                 Assert.Fail ("#13");
1246                         } catch (NotSupportedException) {
1247
1248                         }
1249         
1250                         try {
1251                                 arr.GetConstructor (new Type[] { typeof (int) });
1252                                 Assert.Fail ("#14");
1253                         } catch (NotSupportedException) {
1254
1255                         }
1256         
1257                         TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
1258                         Assert.AreEqual (attr, arr.Attributes, "#15");
1259
1260                         Assert.IsTrue (arr.HasElementType, "#16");
1261                         Assert.IsTrue (arr.IsArray, "#17");
1262                         Assert.IsFalse (arr.IsByRef, "#18");
1263                         Assert.IsFalse (arr.IsCOMObject, "#19");
1264                         Assert.IsFalse (arr.IsPointer, "#20");
1265                         Assert.IsFalse (arr.IsPrimitive, "#21");
1266
1267                         try {
1268                                 arr.GetConstructors (BindingFlags.Public);
1269                                 Assert.Fail ("#22");
1270                         } catch (NotSupportedException) {
1271
1272                         }
1273
1274                         try {
1275                                 arr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
1276                                 Assert.Fail ("#23");
1277                         } catch (NotSupportedException) {
1278
1279                         }
1280                 }
1281
1282                 [Test]
1283                 public void AttributeValues ()
1284                 {
1285                         TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
1286                         Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
1287
1288                         tb = module.DefineType (MakeName (), TypeAttributes.Public);
1289                         Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
1290
1291                         tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
1292                         Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
1293
1294                         tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
1295                         Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
1296                 }
1297
1298                 [Test]
1299                 [Category ("NotDotNet")]
1300                 public void AttributeValuesUnderCompilerContext ()
1301                 {
1302                         SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
1303
1304                         TypeAttributes arrayAttr = TypeAttributes.Sealed | TypeAttributes.Serializable;
1305
1306                         TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
1307                         Assert.AreEqual (TypeAttributes.NotPublic | arrayAttr, tb.MakeArrayType ().Attributes, "#1");
1308
1309                         tb = module.DefineType (MakeName (), TypeAttributes.Public);
1310                         Assert.AreEqual (TypeAttributes.Public | arrayAttr, tb.MakeArrayType ().Attributes, "#2");
1311
1312                         tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
1313                         Assert.AreEqual (TypeAttributes.Public | arrayAttr, tb.MakeArrayType ().Attributes, "#3");
1314
1315                         tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
1316                         Assert.AreEqual (TypeAttributes.Public | arrayAttr, tb.MakeArrayType ().Attributes, "$4");
1317                 }
1318
1319                 [Test]
1320                 public void AsReturnType ()
1321                 {
1322
1323                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1324                         Type arr = tb.MakeArrayType ();
1325         
1326                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1327                         ILGenerator ilgen = mb.GetILGenerator ();
1328                         ilgen.Emit (OpCodes.Ldnull);
1329                         ilgen.Emit (OpCodes.Ret);
1330         
1331                         Type res = tb.CreateType ();    
1332         
1333                         object o = Activator.CreateInstance (res);
1334                         res.GetMethod ("Test").Invoke (o, null);
1335                 }
1336
1337                 [Test]
1338                 public void AsParamType ()
1339                 {
1340
1341                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1342                         Type arr = tb.MakeArrayType ();
1343         
1344                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { arr });
1345                         ILGenerator ilgen = mb.GetILGenerator ();
1346                         ilgen.Emit (OpCodes.Ret);
1347         
1348                         Type res = tb.CreateType ();    
1349         
1350                         object o = Activator.CreateInstance (res);
1351                         res.GetMethod ("Test").Invoke (o, new object[1] { null });
1352                 }
1353
1354                 [Test]
1355                 public void AsLocalVariable ()
1356                 {
1357
1358                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1359                         Type arr = tb.MakeArrayType ();
1360         
1361                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1362                         ILGenerator ilgen = mb.GetILGenerator ();
1363                         ilgen.DeclareLocal (arr);
1364                         ilgen.Emit (OpCodes.Ldnull);
1365                         ilgen.Emit (OpCodes.Stloc_0);
1366                         ilgen.Emit (OpCodes.Ldloc_0);
1367                         ilgen.Emit (OpCodes.Ret);
1368         
1369                         Type res = tb.CreateType ();    
1370         
1371                         object o = Activator.CreateInstance (res);
1372                         res.GetMethod ("Test").Invoke (o, null);
1373                 }
1374
1375                 [Test]
1376                 public void TestEquals ()
1377                 {
1378                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1379                         Type arr = tb.MakeArrayType ();
1380                         Type arr2 = tb.MakeArrayType ();
1381                         Assert.IsTrue (arr.Equals (arr), "#1");
1382                 }
1383
1384                 [Test]
1385                 [Category ("NotWorking")] //two stage type creation makes this fail
1386                 public void TestEquals2 ()
1387                 {
1388                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1389                         Type arr = tb.MakeArrayType ();
1390                         Type arr2 = tb.MakeArrayType ();
1391                         Assert.IsFalse (arr.Equals (arr2), "#1");
1392                 }
1393
1394                 [Test]
1395                 public void IsSubclassOf ()
1396                 {
1397                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1398                         Type arr = tb.MakeArrayType ();
1399                         Assert.IsFalse (arr.IsSubclassOf (tb), "#1");
1400                         Assert.IsFalse (arr.IsSubclassOf (typeof (object[])), "#2");
1401                 }
1402
1403                 [Test]
1404                 public void IsAssignableFrom ()
1405                 {
1406                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1407                         Type arr = tb.MakeArrayType ();
1408                         Assert.IsFalse (arr.IsAssignableFrom (tb), "#1");
1409                         Assert.IsFalse (arr.IsAssignableFrom (typeof (object[])), "#2");
1410                 }
1411
1412                 [Test]
1413                 [Category ("NotWorking")] //two stage type creation makes this fail
1414                 public void IsAssignableFrom2 ()
1415                 {
1416                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1417                         Type arr = tb.MakeArrayType ();
1418                         Assert.IsFalse (typeof (object[]).IsAssignableFrom (arr), "#1");
1419                         Assert.IsFalse (typeof (object).IsAssignableFrom (arr), "#2");
1420                 }
1421
1422                 [Test]
1423                 public void GetInterfaceMap ()
1424                 {
1425                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1426                         Type arr = tb.MakeArrayType ();
1427                         try {
1428                                 arr.GetInterfaceMap (typeof (IEnumerable));
1429                                 Assert.Fail ("#1");
1430                         } catch (NotSupportedException) {
1431
1432                         }
1433                 }
1434
1435                 [Test]
1436                 public void IsInstanceOfType ()
1437                 {
1438                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1439                         Type arr = tb.MakeArrayType ();
1440                         Assert.IsFalse (arr.IsInstanceOfType (tb), "#1");
1441                         Assert.IsFalse (arr.IsInstanceOfType (null), "#2");
1442                         Assert.IsFalse (arr.IsInstanceOfType (new object [1]), "#3");
1443
1444                         Type t = tb.CreateType ();
1445                         object obj = Array.CreateInstance (t, 10);
1446                         Assert.IsFalse (arr.IsInstanceOfType (obj), "#4");
1447                 }
1448
1449                 [Test]
1450                 public void IsGenericTypeDefinition ()
1451                 {
1452                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1453                         Type arr = tb.MakeArrayType ();
1454                         Assert.IsFalse (arr.IsGenericTypeDefinition, "#1");
1455                 }
1456
1457                 [Test]
1458                 public void IsGenericType ()
1459                 {
1460                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1461                         Type arr = tb.MakeArrayType ();
1462                         Assert.IsFalse (arr.IsGenericType, "#1");
1463                 }
1464
1465                 [Test]
1466                 public void MakeGenericType ()
1467                 {
1468                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1469                         Type arr = tb.MakeArrayType ();
1470                         try {
1471                                 arr.MakeGenericType (new Type[] { typeof (string) });
1472                                 Assert.Fail ("#1");
1473                         } catch (NotSupportedException) {}
1474                 }
1475
1476                 [Test]
1477                 public void GenericParameterPosition ()
1478                 {
1479                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1480                         Type arr = tb.MakeArrayType ();
1481                         try {
1482                                 int pos = arr.GenericParameterPosition;
1483                                 Assert.Fail ("#1");
1484                         } catch (InvalidOperationException) {}
1485                 }
1486
1487                 [Test]
1488                 public void GenericParameterAttributes ()
1489                 {
1490                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1491                         Type arr = tb.MakeArrayType ();
1492                         try {
1493                                 object attr = arr.GenericParameterAttributes;
1494                                 Assert.Fail ("#1");
1495                         } catch (NotSupportedException) {}
1496                 }
1497
1498                 [Test]
1499                 public void GetGenericParameterConstraints ()
1500                 {
1501                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1502                         Type arr = tb.MakeArrayType ();
1503                         try {
1504                                 arr.GetGenericParameterConstraints ();
1505                                 Assert.Fail ("#1");
1506                         } catch (InvalidOperationException) {}
1507                 }
1508
1509                 [Test]
1510                 public void MakeArrayType ()
1511                 {
1512                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1513                         Type arr = tb.MakeArrayType ();
1514                         Type res = arr.MakeArrayType ();
1515                         Assert.IsNotNull (res, "#1");
1516                         Assert.IsTrue (res.IsArray, "#2");
1517
1518                         res = arr.MakeArrayType (2);
1519                         Assert.IsNotNull (res, "#3");
1520                         Assert.IsTrue (res.IsArray, "#4");
1521                 }
1522
1523                 [Test]
1524                 public void MakeByRefType ()
1525                 {
1526                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1527                         Type arr = tb.MakeArrayType ();
1528                         Type res = arr.MakeByRefType ();
1529
1530                         Assert.IsNotNull (res, "#1");
1531                         Assert.IsTrue (res.IsByRef, "#2");
1532                 }
1533
1534                 [Test]
1535                 public void MakePointerType ()
1536                 {
1537                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1538                         Type arr = tb.MakeArrayType ();
1539                         Type res = arr.MakePointerType ();
1540
1541                         Assert.IsNotNull (res, "#1");
1542                         Assert.IsTrue (res.IsPointer, "#2");
1543                 }
1544
1545                 [Test]
1546                 public void StructLayoutAttribute ()
1547                 {
1548                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1549                         Type arr = tb.MakeArrayType ();
1550                         try {
1551                                 object x = arr.StructLayoutAttribute;
1552                                 Assert.Fail ("#1");
1553                         } catch (NotSupportedException) {}
1554                 }
1555
1556                 [Test]
1557                 public void GetArrayRank ()
1558                 {
1559                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1560
1561                         Assert.AreEqual (1, tb.MakeArrayType ().GetArrayRank (), "#1");
1562                         Assert.AreEqual (2, tb.MakeArrayType (2).GetArrayRank (), "#2");
1563                 }
1564
1565                 [Test]
1566                 public void GenericTypeMembers ()
1567                 {
1568                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1569                         Type arr = tb.MakeArrayType ();
1570
1571                         try {
1572                                 arr.GetGenericArguments ();
1573                                 Assert.Fail ("#1");
1574                         } catch (NotSupportedException) {}
1575
1576                         try {
1577                                 arr.GetGenericParameterConstraints ();
1578                                 Assert.Fail ("#2");
1579                         } catch (InvalidOperationException) {}
1580
1581                         try {
1582                                 arr.GetGenericTypeDefinition ();
1583                                 Assert.Fail ("#3");
1584                         } catch (NotSupportedException) {}
1585                 
1586                         Assert.IsFalse (arr.ContainsGenericParameters, "#4");
1587                         try {
1588                                 var x = arr.GenericParameterAttributes;
1589                                 Assert.Fail ("#5");
1590                         } catch (NotSupportedException) {}
1591
1592                         try {
1593                                 var x = arr.GenericParameterPosition;
1594                                 Assert.Fail ("#6");
1595                         } catch (InvalidOperationException) {}
1596
1597                         Assert.IsFalse (arr.ContainsGenericParameters, "#7");
1598
1599                         Assert.IsFalse (arr.IsGenericParameter, "#8");
1600                         Assert.IsFalse (arr.IsGenericType, "#9");
1601                         Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
1602                 }
1603
1604                 [Test]
1605                 public void ArrayAsGenericArgumentOfNonSreType ()
1606                 {
1607                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1608
1609                         Type arr = tb.MakeArrayType ();
1610                         Type inst = typeof (Foo<>).MakeGenericType (arr);
1611                         
1612                         MethodBuilder mb = tb.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (object), Type.EmptyTypes);
1613         
1614                         ILGenerator ilgen = mb.GetILGenerator ();
1615                         ilgen.Emit (OpCodes.Ldtoken, inst);
1616                         ilgen.Emit (OpCodes.Call, typeof (Type).GetMethod ("GetTypeFromHandle"));
1617                         ilgen.Emit (OpCodes.Ret);
1618         
1619                         Type res = tb.CreateType ();
1620                         Type expected = typeof (Foo<>).MakeGenericType (res.MakeArrayType ());
1621         
1622                         Assert.AreEqual (expected, res.GetMethod ("Main").Invoke (null, null), "#1");
1623                         Assert.IsNotNull (Activator.CreateInstance (expected), "#2");
1624                 }
1625
1626
1627                 [Test]
1628                 public void GenericTypeMembersOfGenericTypeParam ()
1629                 {
1630                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1631                         var gparam = tb.DefineGenericParameters ("F")[0];
1632                         Type arr = gparam.MakeArrayType ();
1633
1634                         try {
1635                                 arr.GetGenericArguments ();
1636                                 Assert.Fail ("#1");
1637                         } catch (NotSupportedException) {}
1638
1639                         try {
1640                                 arr.GetGenericParameterConstraints ();
1641                                 Assert.Fail ("#2");
1642                         } catch (InvalidOperationException) {}
1643
1644                         try {
1645                                 arr.GetGenericTypeDefinition ();
1646                                 Assert.Fail ("#3");
1647                         } catch (NotSupportedException) {}
1648                 
1649                         Assert.IsTrue (arr.ContainsGenericParameters, "#4");
1650                         try {
1651                                 var x = arr.GenericParameterAttributes;
1652                                 Assert.Fail ("#5");
1653                         } catch (NotSupportedException) {}
1654
1655                         try {
1656                                 var x = arr.GenericParameterPosition;
1657                                 Assert.Fail ("#6");
1658                         } catch (InvalidOperationException) {}
1659
1660
1661                         Assert.IsFalse (arr.IsGenericParameter, "#8");
1662                         Assert.IsFalse (arr.IsGenericType, "#9");
1663                         Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
1664
1665 #if NET_4_0
1666                         Assert.AreEqual (TypeAttributes.Public, arr.Attributes, "#11");
1667 #else
1668                         try {
1669                                 var x = arr.Attributes; //This is because GenericTypeParameterBuilder doesn't support Attributes 
1670                                 Assert.Fail ("#11");
1671                         } catch (NotSupportedException) {}
1672 #endif
1673
1674                         Assert.IsTrue (arr.HasElementType, "#12");
1675                         Assert.IsTrue (arr.IsArray, "#13");
1676
1677                         Assert.AreEqual (assembly, arr.Assembly, "#14");
1678                         Assert.AreEqual (null, arr.AssemblyQualifiedName, "#15");
1679                         Assert.AreEqual (typeof (Array), arr.BaseType, "#16");
1680                         Assert.AreEqual (null, arr.FullName, "#17");
1681                         Assert.AreEqual (module, arr.Module, "#18");
1682                         Assert.AreEqual (null, arr.Namespace, "#19");
1683                         Assert.AreEqual (arr, arr.UnderlyingSystemType, "#20");
1684                         Assert.AreEqual ("F[]", arr.Name, "#21");
1685
1686                         Assert.AreEqual (gparam, arr.GetElementType (), "#22");
1687                 }
1688
1689                 [Test]
1690                 public void GenericTypeMembersOfEnum ()
1691                 {
1692                         var eb = module.DefineEnum ("dd.enum", TypeAttributes.Public, typeof (int));
1693                         Type arr = eb.MakeArrayType ();
1694
1695                         try {
1696                                 arr.GetGenericArguments ();
1697                                 Assert.Fail ("#1");
1698                         } catch (NotSupportedException) {}
1699
1700                         try {
1701                                 arr.GetGenericParameterConstraints ();
1702                                 Assert.Fail ("#2");
1703                         } catch (InvalidOperationException) {}
1704
1705                         try {
1706                                 arr.GetGenericTypeDefinition ();
1707                                 Assert.Fail ("#3");
1708                         } catch (NotSupportedException) {}
1709                 
1710                         Assert.IsFalse (arr.ContainsGenericParameters, "#4");
1711                         try {
1712                                 var x = arr.GenericParameterAttributes;
1713                                 Assert.Fail ("#5");
1714                         } catch (NotSupportedException) {}
1715
1716                         try {
1717                                 var x = arr.GenericParameterPosition;
1718                                 Assert.Fail ("#6");
1719                         } catch (InvalidOperationException) {}
1720
1721
1722                         Assert.IsFalse (arr.IsGenericParameter, "#8");
1723                         Assert.IsFalse (arr.IsGenericType, "#9");
1724                         Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
1725
1726                         Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Sealed, arr.Attributes, "#11");
1727
1728                         Assert.IsTrue (arr.HasElementType, "#12");
1729                         Assert.IsTrue (arr.IsArray, "#13");
1730
1731                         Assert.AreEqual (assembly, arr.Assembly, "#14");
1732                         Assert.AreEqual ("dd.enum[], MonoTests.System.Reflection.Emit.TypeBuilderTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", arr.AssemblyQualifiedName, "#15");
1733                         Assert.AreEqual (typeof (Array), arr.BaseType, "#16");
1734                         Assert.AreEqual ("dd.enum[]", arr.FullName, "#17");
1735                         Assert.AreEqual (module, arr.Module, "#18");
1736                         Assert.AreEqual ("dd", arr.Namespace, "#19");
1737                         Assert.AreEqual (arr, arr.UnderlyingSystemType, "#20");
1738                         Assert.AreEqual ("enum[]", arr.Name, "#21");
1739
1740                         Assert.AreEqual (eb, arr.GetElementType (), "#22");
1741                 }
1742
1743         }
1744 #endif
1745 }