Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / generic_type_definition.2.cs
1 using System;
2 using System.Threading;
3 using System.Reflection;
4 using System.Reflection.Emit;
5
6 namespace TestApp
7 {
8         public class A<T> {
9                 public T fld;
10         }
11
12     class Program
13     {
14                 static AssemblyBuilder assembly;
15                 static ModuleBuilder module;
16                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
17
18                 static void SetUp ()
19                 {
20                         AssemblyName assemblyName = new AssemblyName ();
21                         assemblyName.Name = ASSEMBLY_NAME;
22
23                         assembly =
24                                 Thread.GetDomain ().DefineDynamicAssembly (
25                                         assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
26
27                         module = assembly.DefineDynamicModule ("module1", "Module1.dll");
28                 }
29
30                 static int Main()
31                 {
32                         Type gtd = typeof (A<>);
33                         Type oi = gtd.MakeGenericType (gtd.GetGenericArguments ());
34
35                         if (oi != gtd) {
36                                 Console.WriteLine ("fully open instantiation of static type not the same of the generic type definition");
37                                 return 1;
38                         }
39
40                         SetUp ();
41                         TypeBuilder tb = module.DefineType ("Nullable`1", TypeAttributes.Public);
42                         Type[] args = tb.DefineGenericParameters ("T");
43                         Type type = tb.MakeGenericType (args);
44
45                         if (type == tb) {
46                                 Console.WriteLine ("fully open instantiation of TypeBuilder is the same of the TypeBuilder");
47                                 return 2;
48                         }
49                 
50                         Type res = tb.CreateType ();
51                         Type oires = res.MakeGenericType (res.GetGenericArguments ());
52
53                         if (res != oires) {
54                                 Console.WriteLine ("fully open instantiation not the same of the generic type definition for the TypeBuilder created type");
55                                 return 3;
56                         }
57
58                         try {
59                                 oires.GetConstructors ();
60                         } catch (Exception e) {
61                                 Console.WriteLine ("fully open instantiation of the TypeBuilder created type must have GetConstructors working {0}", e);
62                                 return 5;
63                         }
64
65                         return 0;
66                 }
67         }
68 }