2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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                 [Category ("NotDotNet")]
57                 public void NonStandardPropertiesValues ()
58                 {
59                         /*
60                         Those are tests for properties that return non-sense values on MS.
61                         */
62                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
63                         Type ptr = tb.MakePointerType ();
64                         //a ptr doesn't have any base type
65                         Assert.AreEqual (null, ptr.BaseType, "#1");
66                 }
67
68                 [Test]
69                 public void PropertiesValue ()
70                 {
71                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
72                         Type ptr = tb.MakePointerType ();
73
74                         Assert.AreEqual (assembly, ptr.Assembly, "#1");
75                         Assert.AreEqual ("ns.type*, " + assembly.FullName, ptr.AssemblyQualifiedName, "#2");
76                         Assert.AreEqual ("ns.type*", ptr.FullName, "#4");
77                         Assert.AreEqual (module, ptr.Module, "#5");
78                         Assert.AreEqual ("ns", ptr.Namespace, "#6");
79                         Assert.AreEqual (ptr, ptr.UnderlyingSystemType, "#7");
80                         Assert.AreEqual ("type*", ptr.Name, "#8");
81
82                         try {
83                                 object x = ptr.GUID;
84                                 Assert.Fail ("#9");
85                         } catch (NotSupportedException) {}
86
87                         try {
88                                 object x = ptr.TypeHandle;
89                                 Assert.Fail ("#10");
90                         } catch (NotSupportedException) {}
91                 }       
92
93                 [Test]
94                 public void Methods ()
95                 {
96                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
97                         Type ptr = tb.MakePointerType ();
98
99                         try {
100                                 ptr.GetInterface ("foo", true);
101                                 Assert.Fail ("#1");
102                         } catch (NotSupportedException) {
103
104                         }
105         
106                         try {
107                                 ptr.GetInterfaces ();
108                                 Assert.Fail ("#2");
109                         } catch (NotSupportedException) {
110
111                         }
112         
113                         Assert.AreEqual (tb, ptr.GetElementType ());
114         
115                         try {
116                                 ptr.GetEvent ("foo", BindingFlags.Public);
117                                 Assert.Fail ("#4");
118                         } catch (NotSupportedException) {
119
120                         }
121         
122                         try {
123                                 ptr.GetEvents (BindingFlags.Public);
124                                 Assert.Fail ("#5");
125                         } catch (NotSupportedException) {
126
127                         }
128         
129                         try {
130                                 ptr.GetField ("foo", BindingFlags.Public);
131                                 Assert.Fail ("#6");
132                         } catch (NotSupportedException) {
133
134                         }
135         
136                         try {
137                                 ptr.GetFields (BindingFlags.Public);
138                                 Assert.Fail ("#7");
139                         } catch (NotSupportedException) {
140
141                         }
142         
143                         try {
144                                 ptr.GetMembers (BindingFlags.Public);
145                                 Assert.Fail ("#8");
146                         } catch (NotSupportedException) {
147
148                         }
149         
150                         try {
151                                 ptr.GetMethod ("Sort");
152                                 Assert.Fail ("#9");
153                         } catch (NotSupportedException) {
154
155                         }
156         
157                         try {
158                                 ptr.GetMethods (BindingFlags.Public);
159                                 Assert.Fail ("#9");
160                         } catch (NotSupportedException) {
161
162                         }
163         
164                         try {
165                                 ptr.GetNestedType ("bla", BindingFlags.Public);
166                                 Assert.Fail ("#10");
167                         } catch (NotSupportedException) {
168
169                         }
170         
171                         try {
172                                 ptr.GetNestedTypes (BindingFlags.Public);
173                                 Assert.Fail ("#11");
174                         } catch (NotSupportedException) {
175
176                         }
177         
178                         try {
179                                 ptr.GetProperties (BindingFlags.Public);
180                                 Assert.Fail ("#12");
181                         } catch (NotSupportedException) {
182
183                         }       
184         
185                         try {
186                                 ptr.GetProperty ("Length");
187                                 Assert.Fail ("#13");
188                         } catch (NotSupportedException) {
189
190                         }
191         
192                         try {
193                                 ptr.GetConstructor (new Type[] { typeof (int) });
194                                 Assert.Fail ("#14");
195                         } catch (NotSupportedException) {
196
197                         }
198         
199                         TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
200                         Assert.AreEqual (attr, ptr.Attributes, "#15");
201
202                         Assert.IsTrue (ptr.HasElementType, "#16");
203                         Assert.IsFalse (ptr.IsArray, "#17");
204                         Assert.IsFalse (ptr.IsByRef, "#18");
205                         Assert.IsFalse (ptr.IsCOMObject, "#19");
206                         Assert.IsTrue (ptr.IsPointer, "#20");
207                         Assert.IsFalse (ptr.IsPrimitive, "#21");
208
209                         try {
210                                 ptr.GetConstructors (BindingFlags.Public);
211                                 Assert.Fail ("#22");
212                         } catch (NotSupportedException) {
213
214                         }
215
216                         try {
217                                 ptr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
218                                 Assert.Fail ("#23");
219                         } catch (NotSupportedException) {
220
221                         }
222
223                         try {
224                                 ptr.GetArrayRank ();
225                                 Assert.Fail ("#23");
226                         } catch (NotSupportedException) {
227
228                         }
229                 }
230
231                 [Test]
232                 public void AttributeValues ()
233                 {
234                                 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
235                                 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
236
237                                 tb = module.DefineType (MakeName (), TypeAttributes.Public);
238                                 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
239
240                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
241                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
242
243                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
244                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
245                 }
246
247                 [Test]
248                 public void AsParamType ()
249                 {
250
251                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
252                         Type ptr = tb.MakePointerType ();
253         
254                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { ptr });
255                         ILGenerator ilgen = mb.GetILGenerator ();
256                         ilgen.Emit (OpCodes.Ret);
257         
258                         Type res = tb.CreateType ();    
259         
260                         object o = Activator.CreateInstance (res);
261                         //FIXME this crashes the runtime
262                         //res.GetMethod ("Test").Invoke (o, new object[1] { null });
263                 }
264
265                 [Test]
266                 public void AsLocalVariable ()
267                 {
268
269                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
270                         Type ptr = tb.MakePointerType ();
271         
272                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
273                         ILGenerator ilgen = mb.GetILGenerator ();
274                         ilgen.DeclareLocal (ptr);
275                         ilgen.Emit (OpCodes.Ret);
276         
277                         Type res = tb.CreateType ();    
278         
279                         object o = Activator.CreateInstance (res);
280                         res.GetMethod ("Test").Invoke (o, null);
281                 }
282
283                 [Test]
284                 public void TestEquals ()
285                 {
286                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
287                         Type ptr = tb.MakePointerType ();
288                         Type ptr2 = tb.MakePointerType ();
289                         Assert.IsFalse (ptr.Equals (ptr2), "#1");
290                         Assert.IsTrue (ptr.Equals (ptr), "#2");
291                 }
292
293                 [Test]
294                 public void IsSubclassOf ()
295                 {
296                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
297                         Type ptr = tb.MakePointerType ();
298                         Assert.IsFalse (ptr.IsSubclassOf (tb), "#1");
299                         Assert.IsFalse (ptr.IsSubclassOf (typeof (object[])), "#2");
300                 }
301
302                 [Test]
303                 public void IsAssignableFrom ()
304                 {
305                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
306                         Type ptr = tb.MakePointerType ();
307                         Assert.IsFalse (ptr.IsAssignableFrom (tb), "#1");
308                         Assert.IsFalse (ptr.IsAssignableFrom (typeof (object[])), "#2");
309                         Assert.IsFalse (typeof (object[]).IsAssignableFrom (ptr), "#3");
310                         Assert.IsFalse (typeof (object).IsAssignableFrom (ptr), "#4");
311                 }
312
313                 [Test]
314                 public void GetInterfaceMap ()
315                 {
316                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
317                         Type ptr = tb.MakePointerType ();
318                         try {
319                                 ptr.GetInterfaceMap (typeof (IEnumerable));
320                                 Assert.Fail ("#1");
321                         } catch (NotSupportedException) {
322
323                         }
324                 }
325
326                 [Test]
327                 public void IsInstanceOfType ()
328                 {
329                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
330                         Type ptr = tb.MakePointerType ();
331                         Assert.IsFalse (ptr.IsInstanceOfType (tb), "#1");
332                         Assert.IsFalse (ptr.IsInstanceOfType (null), "#2");
333                         Assert.IsFalse (ptr.IsInstanceOfType (new object [1]), "#3");
334
335                         Type t = tb.CreateType ();
336                         object obj = Activator.CreateInstance (t);
337                         Assert.IsFalse (ptr.IsInstanceOfType (obj), "#4");
338                 }
339
340                 [Test]
341                 public void IsGenericTypeDefinition ()
342                 {
343                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
344                         Type ptr = tb.MakePointerType ();
345                         Assert.IsFalse (ptr.IsGenericTypeDefinition, "#1");
346                 }
347
348                 [Test]
349                 public void IsGenericType ()
350                 {
351                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
352                         Type ptr = tb.MakePointerType ();
353                         Assert.IsFalse (ptr.IsGenericType, "#1");
354                 }
355
356                 [Test]
357                 public void MakeGenericType ()
358                 {
359                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
360                         Type ptr = tb.MakePointerType ();
361                         try {
362                                 ptr.MakeGenericType (new Type[] { typeof (string) });
363                                 Assert.Fail ("#1");
364                         } catch (NotSupportedException) {}
365                 }
366
367                 [Test]
368                 public void GenericParameterPosition ()
369                 {
370                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
371                         Type ptr = tb.MakePointerType ();
372                         try {
373                                 int pos = ptr.GenericParameterPosition;
374                                 Assert.Fail ("#1");
375                         } catch (InvalidOperationException) {}
376                 }
377
378                 [Test]
379                 public void GenericParameterAttributes ()
380                 {
381                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
382                         Type ptr = tb.MakePointerType ();
383                         try {
384                                 object attr = ptr.GenericParameterAttributes;
385                                 Assert.Fail ("#1");
386                         } catch (NotSupportedException) {}
387                 }
388
389                 [Test]
390                 public void GetGenericParameterConstraints ()
391                 {
392                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
393                         Type ptr = tb.MakePointerType ();
394                         try {
395                                 ptr.GetGenericParameterConstraints ();
396                                 Assert.Fail ("#1");
397                         } catch (InvalidOperationException) {}
398                 }
399
400                 [Test]
401                 public void MakeArrayType ()
402                 {
403                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
404                         Type ptr = tb.MakePointerType ();
405                         Type res = ptr.MakeArrayType ();
406                         Assert.IsNotNull (res, "#1");
407                         Assert.IsTrue (res.IsArray, "#2");
408
409                         res = ptr.MakeArrayType (2);
410                         Assert.IsNotNull (res, "#3");
411                         Assert.IsTrue (res.IsArray, "#4");
412                 }
413
414                 [Test]
415                 public void MakeByRefType ()
416                 {
417                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
418                         Type ptr = tb.MakePointerType ();
419                         Type res = ptr.MakeByRefType ();
420
421                         Assert.IsNotNull (res, "#1");
422                         Assert.IsTrue (res.IsByRef, "#2");
423                 }
424
425                 [Test]
426                 public void MakePointerType ()
427                 {
428                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
429                         Type ptr = tb.MakePointerType ();
430                         Type res = ptr.MakePointerType ();
431
432                         Assert.IsNotNull (res, "#1");
433                         Assert.IsTrue (res.IsPointer, "#2");
434                 }
435
436                 [Test]
437                 public void StructLayoutAttribute ()
438                 {
439                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
440                         Type ptr = tb.MakePointerType ();
441                         try {
442                                 object x = ptr.StructLayoutAttribute;
443                                 Assert.Fail ("#1");
444                         } catch (NotSupportedException) {}
445                 }
446         }
447
448
449         [TestFixture]
450         public class ByrefTypeTest
451         {
452                 AssemblyBuilder assembly;
453                 ModuleBuilder module;
454                 int typeCount;
455                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
456
457                 string MakeName ()
458                 {
459                         return "internal__type"+ typeCount++;
460                 }
461
462                 [SetUp]
463                 protected void SetUp ()
464                 {
465                         AssemblyName assemblyName = new AssemblyName ();
466                         assemblyName.Name = ASSEMBLY_NAME;
467
468                         assembly =
469                                 Thread.GetDomain ().DefineDynamicAssembly (
470                                         assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
471
472                         module = assembly.DefineDynamicModule ("module1");
473                         typeCount = 0;
474                 }
475
476                 [Test]
477                 [Category ("NotDotNet")]
478                 public void NonStandardPropertiesValues ()
479                 {
480                         /*
481                         Those are tests for properties that return non-sense values on MS.
482                         */
483                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
484                         Type byref = tb.MakeByRefType ();
485                         //a byref doesn't have any base type
486                         Assert.AreEqual (null, byref.BaseType, "#1");
487                 }
488
489                 [Test]
490                 public void PropertiesValue ()
491                 {
492                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
493                         Type byref = tb.MakeByRefType ();
494
495                         Assert.AreEqual (assembly, byref.Assembly, "#1");
496                         Assert.AreEqual ("ns.type&, " + assembly.FullName, byref.AssemblyQualifiedName, "#2");
497                         Assert.AreEqual ("ns.type&", byref.FullName, "#4");
498                         Assert.AreEqual (module, byref.Module, "#5");
499                         Assert.AreEqual ("ns", byref.Namespace, "#6");
500                         Assert.AreEqual (byref, byref.UnderlyingSystemType, "#7");
501                         Assert.AreEqual ("type&", byref.Name, "#8");
502
503                         try {
504                                 object x = byref.GUID;
505                                 Assert.Fail ("#9");
506                         } catch (NotSupportedException) {}
507
508                         try {
509                                 object x = byref.TypeHandle;
510                                 Assert.Fail ("#10");
511                         } catch (NotSupportedException) {}
512                 }       
513
514                 [Test]
515                 public void Methods ()
516                 {
517                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
518                         Type byref = tb.MakeByRefType ();
519
520                         try {
521                                 byref.GetInterface ("foo", true);
522                                 Assert.Fail ("#1");
523                         } catch (NotSupportedException) {
524
525                         }
526         
527                         try {
528                                 byref.GetInterfaces ();
529                                 Assert.Fail ("#2");
530                         } catch (NotSupportedException) {
531
532                         }
533         
534                         Assert.AreEqual (tb, byref.GetElementType ());
535         
536                         try {
537                                 byref.GetEvent ("foo", BindingFlags.Public);
538                                 Assert.Fail ("#4");
539                         } catch (NotSupportedException) {
540
541                         }
542         
543                         try {
544                                 byref.GetEvents (BindingFlags.Public);
545                                 Assert.Fail ("#5");
546                         } catch (NotSupportedException) {
547
548                         }
549         
550                         try {
551                                 byref.GetField ("foo", BindingFlags.Public);
552                                 Assert.Fail ("#6");
553                         } catch (NotSupportedException) {
554
555                         }
556         
557                         try {
558                                 byref.GetFields (BindingFlags.Public);
559                                 Assert.Fail ("#7");
560                         } catch (NotSupportedException) {
561
562                         }
563         
564                         try {
565                                 byref.GetMembers (BindingFlags.Public);
566                                 Assert.Fail ("#8");
567                         } catch (NotSupportedException) {
568
569                         }
570         
571                         try {
572                                 byref.GetMethod ("Sort");
573                                 Assert.Fail ("#9");
574                         } catch (NotSupportedException) {
575
576                         }
577         
578                         try {
579                                 byref.GetMethods (BindingFlags.Public);
580                                 Assert.Fail ("#9");
581                         } catch (NotSupportedException) {
582
583                         }
584         
585                         try {
586                                 byref.GetNestedType ("bla", BindingFlags.Public);
587                                 Assert.Fail ("#10");
588                         } catch (NotSupportedException) {
589
590                         }
591         
592                         try {
593                                 byref.GetNestedTypes (BindingFlags.Public);
594                                 Assert.Fail ("#11");
595                         } catch (NotSupportedException) {
596
597                         }
598         
599                         try {
600                                 byref.GetProperties (BindingFlags.Public);
601                                 Assert.Fail ("#12");
602                         } catch (NotSupportedException) {
603
604                         }       
605         
606                         try {
607                                 byref.GetProperty ("Length");
608                                 Assert.Fail ("#13");
609                         } catch (NotSupportedException) {
610
611                         }
612         
613                         try {
614                                 byref.GetConstructor (new Type[] { typeof (int) });
615                                 Assert.Fail ("#14");
616                         } catch (NotSupportedException) {
617
618                         }
619         
620                         TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
621                         Assert.AreEqual (attr, byref.Attributes, "#15");
622
623                         Assert.IsTrue (byref.HasElementType, "#16");
624                         Assert.IsFalse (byref.IsArray, "#17");
625                         Assert.IsTrue (byref.IsByRef, "#18");
626                         Assert.IsFalse (byref.IsCOMObject, "#19");
627                         Assert.IsFalse (byref.IsPointer, "#20");
628                         Assert.IsFalse (byref.IsPrimitive, "#21");
629
630                         try {
631                                 byref.GetConstructors (BindingFlags.Public);
632                                 Assert.Fail ("#22");
633                         } catch (NotSupportedException) {
634
635                         }
636
637                         try {
638                                 byref.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
639                                 Assert.Fail ("#23");
640                         } catch (NotSupportedException) {
641
642                         }
643
644                         try {
645                                 byref.GetArrayRank ();
646                                 Assert.Fail ("#23");
647                         } catch (NotSupportedException) {
648
649                         }
650                 }
651
652                 [Test]
653                 public void AttributeValues ()
654                 {
655                                 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
656                                 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
657
658                                 tb = module.DefineType (MakeName (), TypeAttributes.Public);
659                                 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
660
661                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
662                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
663
664                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
665                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
666                 }
667
668                 [Test]
669                 public void AsParamType ()
670                 {
671
672                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
673                         Type byref = tb.MakeByRefType ();
674         
675                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { byref });
676                         ILGenerator ilgen = mb.GetILGenerator ();
677                         ilgen.Emit (OpCodes.Ret);
678         
679                         Type res = tb.CreateType ();    
680         
681                         object o = Activator.CreateInstance (res);
682                         res.GetMethod ("Test").Invoke (o, new object[1] { null });
683                 }
684
685                 [Test]
686                 public void AsLocalVariable ()
687                 {
688
689                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
690                         Type byref = tb.MakeByRefType ();
691         
692                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
693                         ILGenerator ilgen = mb.GetILGenerator ();
694                         ilgen.DeclareLocal (byref);
695                         ilgen.Emit (OpCodes.Ret);
696         
697                         Type res = tb.CreateType ();    
698         
699                         object o = Activator.CreateInstance (res);
700                         res.GetMethod ("Test").Invoke (o, null);
701                 }
702
703                 [Test]
704                 public void TestEquals ()
705                 {
706                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
707                         Type byref = tb.MakeByRefType ();
708                         Type byref2 = tb.MakeByRefType ();
709                         Assert.IsFalse (byref.Equals (byref2), "#1");
710                         Assert.IsTrue (byref.Equals (byref), "#2");
711                 }
712
713                 [Test]
714                 public void IsSubclassOf ()
715                 {
716                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
717                         Type byref = tb.MakeByRefType ();
718                         Assert.IsFalse (byref.IsSubclassOf (tb), "#1");
719                         Assert.IsFalse (byref.IsSubclassOf (typeof (object[])), "#2");
720                 }
721
722                 [Test]
723                 public void IsAssignableFrom ()
724                 {
725                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
726                         Type byref = tb.MakeByRefType ();
727                         Assert.IsFalse (byref.IsAssignableFrom (tb), "#1");
728                         Assert.IsFalse (byref.IsAssignableFrom (typeof (object[])), "#2");
729                         Assert.IsFalse (typeof (object[]).IsAssignableFrom (byref), "#3");
730                         Assert.IsFalse (typeof (object).IsAssignableFrom (byref), "#4");
731                 }
732
733                 [Test]
734                 public void GetInterfaceMap ()
735                 {
736                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
737                         Type byref = tb.MakeByRefType ();
738                         try {
739                                 byref.GetInterfaceMap (typeof (IEnumerable));
740                                 Assert.Fail ("#1");
741                         } catch (NotSupportedException) {
742
743                         }
744                 }
745
746                 [Test]
747                 public void IsInstanceOfType ()
748                 {
749                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
750                         Type byref = tb.MakeByRefType ();
751                         Assert.IsFalse (byref.IsInstanceOfType (tb), "#1");
752                         Assert.IsFalse (byref.IsInstanceOfType (null), "#2");
753                         Assert.IsFalse (byref.IsInstanceOfType (new object [1]), "#3");
754
755                         Type t = tb.CreateType ();
756                         object obj = Activator.CreateInstance (t);
757                         Assert.IsFalse (byref.IsInstanceOfType (obj), "#4");
758                 }
759
760                 [Test]
761                 public void IsGenericTypeDefinition ()
762                 {
763                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
764                         Type byref = tb.MakeByRefType ();
765                         Assert.IsFalse (byref.IsGenericTypeDefinition, "#1");
766                 }
767
768                 [Test]
769                 public void IsGenericType ()
770                 {
771                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
772                         Type byref = tb.MakeByRefType ();
773                         Assert.IsFalse (byref.IsGenericType, "#1");
774                 }
775
776                 [Test]
777                 public void MakeGenericType ()
778                 {
779                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
780                         Type byref = tb.MakeByRefType ();
781                         try {
782                                 byref.MakeGenericType (new Type[] { typeof (string) });
783                                 Assert.Fail ("#1");
784                         } catch (NotSupportedException) {}
785                 }
786
787                 [Test]
788                 public void GenericParameterPosition ()
789                 {
790                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
791                         Type byref = tb.MakeByRefType ();
792                         try {
793                                 int pos = byref.GenericParameterPosition;
794                                 Assert.Fail ("#1");
795                         } catch (InvalidOperationException) {}
796                 }
797
798                 [Test]
799                 public void GenericParameterAttributes ()
800                 {
801                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
802                         Type byref = tb.MakeByRefType ();
803                         try {
804                                 object attr = byref.GenericParameterAttributes;
805                                 Assert.Fail ("#1");
806                         } catch (NotSupportedException) {}
807                 }
808
809                 [Test]
810                 public void GetGenericParameterConstraints ()
811                 {
812                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
813                         Type byref = tb.MakeByRefType ();
814                         try {
815                                 byref.GetGenericParameterConstraints ();
816                                 Assert.Fail ("#1");
817                         } catch (InvalidOperationException) {}
818                 }
819
820                 [Test]
821                 public void MakeArrayType ()
822                 {
823                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
824                         Type byref = tb.MakeByRefType ();
825                         try {
826                                 byref.MakeArrayType ();
827                                 Assert.Fail ("#1");
828                         } catch (ArgumentException) {};
829                         try {
830                                 byref.MakeArrayType (2);
831                                 Assert.Fail ("#2");
832                         } catch (ArgumentException) {};
833                 }
834
835                 [Test]
836                 public void MakeByRefType ()
837                 {
838                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
839                         Type byref = tb.MakeByRefType ();
840                         try {
841                                 byref.MakeByRefType ();
842                                 Assert.Fail ("#1");
843                         } catch (ArgumentException) {}
844                 }
845
846                 [Test]
847                 public void MakePointerType ()
848                 {
849                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
850                         Type byref = tb.MakeByRefType ();
851                         try {
852                                 byref.MakePointerType ();
853                                 Assert.Fail ("#1");
854                         } catch (ArgumentException) {}
855                 }
856
857                 [Test]
858                 public void StructLayoutAttribute ()
859                 {
860                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
861                         Type byref = tb.MakeByRefType ();
862                         try {
863                                 object x = byref.StructLayoutAttribute;
864                                 Assert.Fail ("#1");
865                         } catch (NotSupportedException) {}
866                 }
867         }
868
869         [TestFixture]
870         public class ArrayTypeTest
871         {
872                 AssemblyBuilder assembly;
873                 ModuleBuilder module;
874                 int typeCount;
875                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
876
877                 string MakeName ()
878                 {
879                         return "internal__type"+ typeCount++;
880                 }
881
882                 [SetUp]
883                 protected void SetUp ()
884                 {
885                         AssemblyName assemblyName = new AssemblyName ();
886                         assemblyName.Name = ASSEMBLY_NAME;
887
888                         assembly =
889                                 Thread.GetDomain ().DefineDynamicAssembly (
890                                         assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
891
892                         module = assembly.DefineDynamicModule ("module1");
893                         typeCount = 0;
894                 }
895
896                 [Test]
897                 public void PropertiesValue ()
898                 {
899                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
900                         Type arr = tb.MakeArrayType ();
901
902                         Assert.AreEqual (assembly, arr.Assembly, "#1");
903                         Assert.AreEqual ("ns.type[], " + assembly.FullName, arr.AssemblyQualifiedName, "#2");
904                         Assert.AreEqual (typeof (Array), arr.BaseType, "#3");
905                         Assert.AreEqual ("ns.type[]", arr.FullName, "#4");
906                         Assert.AreEqual (module, arr.Module, "#5");
907                         Assert.AreEqual ("ns", arr.Namespace, "#6");
908                         Assert.AreEqual (arr, arr.UnderlyingSystemType, "#7");
909                         Assert.AreEqual ("type[]", arr.Name, "#8");
910
911                         try {
912                                 object x = arr.GUID;
913                                 Assert.Fail ("#9");
914                         } catch (NotSupportedException) {}
915
916                         try {
917                                 object x = arr.TypeHandle;
918                                 Assert.Fail ("#10");
919                         } catch (NotSupportedException) {}
920                 }       
921
922                 [Test]
923                 public void Methods ()
924                 {
925                         TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
926                         Type arr = tb.MakeArrayType ();
927
928                         try {
929                                 arr.GetInterface ("foo", true);
930                                 Assert.Fail ("#1");
931                         } catch (NotSupportedException) {
932
933                         }
934         
935                         try {
936                                 arr.GetInterfaces ();
937                                 Assert.Fail ("#2");
938                         } catch (NotSupportedException) {
939
940                         }
941         
942                         Assert.AreEqual (tb, arr.GetElementType ());
943         
944                         try {
945                                 arr.GetEvent ("foo", BindingFlags.Public);
946                                 Assert.Fail ("#4");
947                         } catch (NotSupportedException) {
948
949                         }
950         
951                         try {
952                                 arr.GetEvents (BindingFlags.Public);
953                                 Assert.Fail ("#5");
954                         } catch (NotSupportedException) {
955
956                         }
957         
958                         try {
959                                 arr.GetField ("foo", BindingFlags.Public);
960                                 Assert.Fail ("#6");
961                         } catch (NotSupportedException) {
962
963                         }
964         
965                         try {
966                                 arr.GetFields (BindingFlags.Public);
967                                 Assert.Fail ("#7");
968                         } catch (NotSupportedException) {
969
970                         }
971         
972                         try {
973                                 arr.GetMembers (BindingFlags.Public);
974                                 Assert.Fail ("#8");
975                         } catch (NotSupportedException) {
976
977                         }
978         
979                         try {
980                                 arr.GetMethod ("Sort");
981                                 Assert.Fail ("#9");
982                         } catch (NotSupportedException) {
983
984                         }
985         
986                         try {
987                                 arr.GetMethods (BindingFlags.Public);
988                                 Assert.Fail ("#9");
989                         } catch (NotSupportedException) {
990
991                         }
992         
993                         try {
994                                 arr.GetNestedType ("bla", BindingFlags.Public);
995                                 Assert.Fail ("#10");
996                         } catch (NotSupportedException) {
997
998                         }
999         
1000                         try {
1001                                 arr.GetNestedTypes (BindingFlags.Public);
1002                                 Assert.Fail ("#11");
1003                         } catch (NotSupportedException) {
1004
1005                         }
1006         
1007                         try {
1008                                 arr.GetProperties (BindingFlags.Public);
1009                                 Assert.Fail ("#12");
1010                         } catch (NotSupportedException) {
1011
1012                         }       
1013         
1014                         try {
1015                                 arr.GetProperty ("Length");
1016                                 Assert.Fail ("#13");
1017                         } catch (NotSupportedException) {
1018
1019                         }
1020         
1021                         try {
1022                                 arr.GetConstructor (new Type[] { typeof (int) });
1023                                 Assert.Fail ("#14");
1024                         } catch (NotSupportedException) {
1025
1026                         }
1027         
1028                         TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
1029                         Assert.AreEqual (attr, arr.Attributes, "#15");
1030
1031                         Assert.IsTrue (arr.HasElementType, "#16");
1032                         Assert.IsTrue (arr.IsArray, "#17");
1033                         Assert.IsFalse (arr.IsByRef, "#18");
1034                         Assert.IsFalse (arr.IsCOMObject, "#19");
1035                         Assert.IsFalse (arr.IsPointer, "#20");
1036                         Assert.IsFalse (arr.IsPrimitive, "#21");
1037
1038                         try {
1039                                 arr.GetConstructors (BindingFlags.Public);
1040                                 Assert.Fail ("#22");
1041                         } catch (NotSupportedException) {
1042
1043                         }
1044
1045                         try {
1046                                 arr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
1047                                 Assert.Fail ("#23");
1048                         } catch (NotSupportedException) {
1049
1050                         }
1051                 }
1052
1053                 [Test]
1054                 public void AttributeValues ()
1055                 {
1056                                 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
1057                                 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
1058
1059                                 tb = module.DefineType (MakeName (), TypeAttributes.Public);
1060                                 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
1061
1062                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
1063                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
1064
1065                                 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
1066                                 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
1067                 }
1068
1069                 [Test]
1070                 public void AsReturnType ()
1071                 {
1072
1073                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1074                         Type arr = tb.MakeArrayType ();
1075         
1076                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1077                         ILGenerator ilgen = mb.GetILGenerator ();
1078                         ilgen.Emit (OpCodes.Ldnull);
1079                         ilgen.Emit (OpCodes.Ret);
1080         
1081                         Type res = tb.CreateType ();    
1082         
1083                         object o = Activator.CreateInstance (res);
1084                         res.GetMethod ("Test").Invoke (o, null);
1085                 }
1086
1087                 [Test]
1088                 public void AsParamType ()
1089                 {
1090
1091                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1092                         Type arr = tb.MakeArrayType ();
1093         
1094                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { arr });
1095                         ILGenerator ilgen = mb.GetILGenerator ();
1096                         ilgen.Emit (OpCodes.Ret);
1097         
1098                         Type res = tb.CreateType ();    
1099         
1100                         object o = Activator.CreateInstance (res);
1101                         res.GetMethod ("Test").Invoke (o, new object[1] { null });
1102                 }
1103
1104                 [Test]
1105                 public void AsLocalVariable ()
1106                 {
1107
1108                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1109                         Type arr = tb.MakeArrayType ();
1110         
1111                         MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1112                         ILGenerator ilgen = mb.GetILGenerator ();
1113                         ilgen.DeclareLocal (arr);
1114                         ilgen.Emit (OpCodes.Ldnull);
1115                         ilgen.Emit (OpCodes.Stloc_0);
1116                         ilgen.Emit (OpCodes.Ldloc_0);
1117                         ilgen.Emit (OpCodes.Ret);
1118         
1119                         Type res = tb.CreateType ();    
1120         
1121                         object o = Activator.CreateInstance (res);
1122                         res.GetMethod ("Test").Invoke (o, null);
1123                 }
1124
1125                 [Test]
1126                 public void TestEquals ()
1127                 {
1128                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1129                         Type arr = tb.MakeArrayType ();
1130                         Type arr2 = tb.MakeArrayType ();
1131                         Assert.IsFalse (arr.Equals (arr2), "#1");
1132                         Assert.IsTrue (arr.Equals (arr), "#2");
1133                 }
1134
1135                 [Test]
1136                 public void IsSubclassOf ()
1137                 {
1138                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1139                         Type arr = tb.MakeArrayType ();
1140                         Assert.IsFalse (arr.IsSubclassOf (tb), "#1");
1141                         Assert.IsFalse (arr.IsSubclassOf (typeof (object[])), "#2");
1142                 }
1143
1144                 [Test]
1145                 public void IsAssignableFrom ()
1146                 {
1147                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1148                         Type arr = tb.MakeArrayType ();
1149                         Assert.IsFalse (arr.IsAssignableFrom (tb), "#1");
1150                         Assert.IsFalse (arr.IsAssignableFrom (typeof (object[])), "#2");
1151                         Assert.IsFalse (typeof (object[]).IsAssignableFrom (arr), "#3");
1152                         Assert.IsFalse (typeof (object).IsAssignableFrom (arr), "#4");
1153                 }
1154
1155                 [Test]
1156                 public void GetInterfaceMap ()
1157                 {
1158                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1159                         Type arr = tb.MakeArrayType ();
1160                         try {
1161                                 arr.GetInterfaceMap (typeof (IEnumerable));
1162                                 Assert.Fail ("#1");
1163                         } catch (NotSupportedException) {
1164
1165                         }
1166                 }
1167
1168                 [Test]
1169                 public void IsInstanceOfType ()
1170                 {
1171                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1172                         Type arr = tb.MakeArrayType ();
1173                         Assert.IsFalse (arr.IsInstanceOfType (tb), "#1");
1174                         Assert.IsFalse (arr.IsInstanceOfType (null), "#2");
1175                         Assert.IsFalse (arr.IsInstanceOfType (new object [1]), "#3");
1176
1177                         Type t = tb.CreateType ();
1178                         object obj = Array.CreateInstance (t, 10);
1179                         Assert.IsFalse (arr.IsInstanceOfType (obj), "#4");
1180                 }
1181
1182                 [Test]
1183                 public void IsGenericTypeDefinition ()
1184                 {
1185                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1186                         Type arr = tb.MakeArrayType ();
1187                         Assert.IsFalse (arr.IsGenericTypeDefinition, "#1");
1188                 }
1189
1190                 [Test]
1191                 public void IsGenericType ()
1192                 {
1193                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1194                         Type arr = tb.MakeArrayType ();
1195                         Assert.IsFalse (arr.IsGenericType, "#1");
1196                 }
1197
1198                 [Test]
1199                 public void MakeGenericType ()
1200                 {
1201                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1202                         Type arr = tb.MakeArrayType ();
1203                         try {
1204                                 arr.MakeGenericType (new Type[] { typeof (string) });
1205                                 Assert.Fail ("#1");
1206                         } catch (NotSupportedException) {}
1207                 }
1208
1209                 [Test]
1210                 public void GenericParameterPosition ()
1211                 {
1212                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1213                         Type arr = tb.MakeArrayType ();
1214                         try {
1215                                 int pos = arr.GenericParameterPosition;
1216                                 Assert.Fail ("#1");
1217                         } catch (InvalidOperationException) {}
1218                 }
1219
1220                 [Test]
1221                 public void GenericParameterAttributes ()
1222                 {
1223                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1224                         Type arr = tb.MakeArrayType ();
1225                         try {
1226                                 object attr = arr.GenericParameterAttributes;
1227                                 Assert.Fail ("#1");
1228                         } catch (NotSupportedException) {}
1229                 }
1230
1231                 [Test]
1232                 public void GetGenericParameterConstraints ()
1233                 {
1234                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1235                         Type arr = tb.MakeArrayType ();
1236                         try {
1237                                 arr.GetGenericParameterConstraints ();
1238                                 Assert.Fail ("#1");
1239                         } catch (InvalidOperationException) {}
1240                 }
1241
1242                 [Test]
1243                 public void MakeArrayType ()
1244                 {
1245                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1246                         Type arr = tb.MakeArrayType ();
1247                         Type res = arr.MakeArrayType ();
1248                         Assert.IsNotNull (res, "#1");
1249                         Assert.IsTrue (res.IsArray, "#2");
1250
1251                         res = arr.MakeArrayType (2);
1252                         Assert.IsNotNull (res, "#3");
1253                         Assert.IsTrue (res.IsArray, "#4");
1254                 }
1255
1256                 [Test]
1257                 public void MakeByRefType ()
1258                 {
1259                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1260                         Type arr = tb.MakeArrayType ();
1261                         Type res = arr.MakeByRefType ();
1262
1263                         Assert.IsNotNull (res, "#1");
1264                         Assert.IsTrue (res.IsByRef, "#2");
1265                 }
1266
1267                 [Test]
1268                 public void MakePointerType ()
1269                 {
1270                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1271                         Type arr = tb.MakeArrayType ();
1272                         Type res = arr.MakePointerType ();
1273
1274                         Assert.IsNotNull (res, "#1");
1275                         Assert.IsTrue (res.IsPointer, "#2");
1276                 }
1277
1278                 [Test]
1279                 public void StructLayoutAttribute ()
1280                 {
1281                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1282                         Type arr = tb.MakeArrayType ();
1283                         try {
1284                                 object x = arr.StructLayoutAttribute;
1285                                 Assert.Fail ("#1");
1286                         } catch (NotSupportedException) {}
1287                 }
1288
1289                 [Test]
1290                 public void GetArrayRank ()
1291                 {
1292                         TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1293                         Type arr = tb.MakeArrayType ();
1294
1295                         Assert.AreEqual (1, tb.MakeArrayType ().GetArrayRank (), "#1");
1296                         Assert.AreEqual (2, tb.MakeArrayType (2).GetArrayRank (), "#2");
1297                 }
1298         }
1299 #endif
1300 }